Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8fe7527

Browse files
committed
introduce NodeConnection(object) smart wrapper, small improvements
1 parenta41b308 commit8fe7527

File tree

1 file changed

+60
-23
lines changed

1 file changed

+60
-23
lines changed

‎testgres/testgres.py

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,43 @@
4747
pg_config_data= {}
4848

4949

50+
"""
51+
Predefined exceptions
52+
"""
5053
classClusterException(Exception):pass
5154
classQueryException(Exception):pass
5255

5356

57+
"""
58+
Transaction wrapper returned by Node
59+
"""
60+
classNodeConnection(object):
61+
def__init__(self,parent_node,dbname):
62+
self.parent_node=parent_node
63+
64+
self.connection=pglib.connect(
65+
database=dbname,
66+
user=get_username(),
67+
port=parent_node.port,
68+
host="127.0.0.1"
69+
)
70+
71+
self.cursor=self.connection.cursor()
72+
73+
def__enter__(self):
74+
returnself
75+
76+
def__exit__(self,type,value,tb):
77+
self.connection.close()
78+
79+
defexecute(self,query):
80+
self.cursor.execute(query)
81+
returnself.cursor.fetchall()
82+
83+
defclose(self):
84+
self.connection.close()
85+
86+
5487
classPostgresNode:
5588
def__init__(self,name,port):
5689
self.name=name
@@ -134,6 +167,8 @@ def init(self, allows_streaming=False):
134167

135168
self.set_replication_conf()
136169

170+
returnself
171+
137172
definit_from_backup(self,root_node,backup_name,has_streaming=False,hba_permit_replication=True):
138173
"""Initializes cluster from backup, made by another node"""
139174

@@ -175,7 +210,9 @@ def append_conf(self, filename, string):
175210
"""
176211
config_name="%s/%s"% (self.data_dir,filename)
177212
withopen(config_name,"a")asconf:
178-
conf.write(string)
213+
conf.write(''.join([string,'\n']))
214+
215+
returnself
179216

180217
defpg_ctl(self,command,params):
181218
"""Runs pg_ctl with specified params
@@ -192,12 +229,17 @@ def pg_ctl(self, command, params):
192229

193230
withopen(self.output_filename,"a")asfile_out, \
194231
open(self.error_filename,"a")asfile_err:
195-
returnsubprocess.call(
232+
233+
res=subprocess.call(
196234
arguments+ [command],
197235
stdout=file_out,
198236
stderr=file_err
199237
)
200238

239+
ifres>0:
240+
withopen(self.error_filename,"r")aserrfile:
241+
raiseClusterException(errfile.readlines()[-1])
242+
201243
defstart(self):
202244
""" Starts cluster """
203245
logfile=self.logs_dir+"/postgresql.log"
@@ -206,27 +248,30 @@ def start(self):
206248
"-w":None,
207249
"-l":logfile,
208250
}
209-
ifself.pg_ctl("start",params):
210-
raiseClusterException("Cluster startup failed")
251+
self.pg_ctl("start",params)
211252

212253
self.working=True
213254

255+
returnself
256+
214257
defstop(self):
215258
""" Stops cluster """
216259
params= {
217260
"-D":self.data_dir,
218261
"-w":None
219262
}
220-
ifself.pg_ctl("stop",params):
221-
raiseClusterException("Cluster stop failed")
263+
self.pg_ctl("stop",params)
222264

223265
self.working=False
224266

267+
returnself
268+
225269
defreload(self):
226270
"""Reloads config files"""
227271
params= {"-D":self.data_dir}
228-
ifself.pg_ctl("reload",params):
229-
raiseClusterException("Cluster reload failed")
272+
self.pg_ctl("reload",params)
273+
274+
returnself
230275

231276
defcleanup(self):
232277
"""Stops cluster if needed and removes the data directory"""
@@ -238,6 +283,8 @@ def cleanup(self):
238283
# remove data directory
239284
shutil.rmtree(self.data_dir)
240285

286+
returnself
287+
241288
defpsql(self,dbname,query):
242289
"""Executes a query by the psql
243290
@@ -291,21 +338,8 @@ def poll_query_until(self, dbname, query):
291338

292339
defexecute(self,dbname,query):
293340
"""Executes the query and returns all rows"""
294-
connection=pglib.connect(
295-
database=dbname,
296-
user=get_username(),
297-
port=self.port,
298-
host="127.0.0.1"
299-
)
300-
cur=connection.cursor()
301-
302-
cur.execute(query)
303-
res=cur.fetchall()
304-
305-
cur.close()
306-
connection.close()
307-
308-
returnres
341+
withself.connect(dbname)asnode_con:
342+
returnnode_con.execute(query)
309343

310344
defbackup(self,name):
311345
"""Performs pg_basebackup"""
@@ -325,6 +359,9 @@ def backup(self, name):
325359

326360
returnbackup_path
327361

362+
defconnect(self,dbname):
363+
returnNodeConnection(parent_node=self,dbname=dbname)
364+
328365

329366
defget_username():
330367
""" Returns current user name """

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp