19
19
20
20
from .config import testgres_config
21
21
22
- from .connection import \
23
- NodeConnection , \
24
- InternalError , \
25
- ProgrammingError
22
+ from .connection import NodeConnection
26
23
27
24
from .consts import \
28
25
DATA_DIR , \
@@ -541,10 +538,10 @@ def append_conf(self, line='', filename=PG_CONF_FILE, **kwargs):
541
538
This instance of :class:`.PostgresNode`.
542
539
543
540
Examples:
544
- append_conf(fsync=False)
545
- append_conf('log_connections = yes')
546
- append_conf(random_page_cost=1.5, fsync=True, ...)
547
- append_conf('postgresql.conf', 'synchronous_commit = off')
541
+ >>> append_conf(fsync=False)
542
+ >>> append_conf('log_connections = yes')
543
+ >>> append_conf(random_page_cost=1.5, fsync=True, ...)
544
+ >>> append_conf('postgresql.conf', 'synchronous_commit = off')
548
545
"""
549
546
550
547
lines = [line ]
@@ -815,7 +812,8 @@ def psql(self,
815
812
filename = None ,
816
813
dbname = None ,
817
814
username = None ,
818
- input = None ):
815
+ input = None ,
816
+ ** variables ):
819
817
"""
820
818
Execute a query using psql.
821
819
@@ -825,9 +823,15 @@ def psql(self,
825
823
dbname: database name to connect to.
826
824
username: database user name.
827
825
input: raw input to be passed.
826
+ **variables: vars to be set before execution.
828
827
829
828
Returns:
830
829
A tuple of (code, stdout, stderr).
830
+
831
+ Examples:
832
+ >>> psql('select 1')
833
+ >>> psql('postgres', 'select 2')
834
+ >>> psql(query='select 3', ON_ERROR_STOP=1)
831
835
"""
832
836
833
837
# Set default arguments
@@ -846,6 +850,10 @@ def psql(self,
846
850
dbname
847
851
] # yapf: disable
848
852
853
+ # set variables before execution
854
+ for key , value in iteritems (variables ):
855
+ psql_params .extend (["--set" , '{}={}' .format (key , value )])
856
+
849
857
# select query source
850
858
if query :
851
859
psql_params .extend (("-c" , query ))
@@ -877,10 +885,15 @@ def safe_psql(self, query=None, **kwargs):
877
885
username: database user name.
878
886
input: raw input to be passed.
879
887
888
+ **kwargs are passed to psql().
889
+
880
890
Returns:
881
891
psql's output as str.
882
892
"""
883
893
894
+ # force this setting
895
+ kwargs ['ON_ERROR_STOP' ] = 1
896
+
884
897
ret , out , err = self .psql (query = query , ** kwargs )
885
898
if ret :
886
899
raise QueryException ((err or b'' ).decode ('utf-8' ), query )
@@ -980,8 +993,7 @@ def poll_query_until(self,
980
993
sleep_time = 1 ,
981
994
expected = True ,
982
995
commit = True ,
983
- raise_programming_error = True ,
984
- raise_internal_error = True ):
996
+ suppress = None ):
985
997
"""
986
998
Run a query once per second until it returns 'expected'.
987
999
Query should return a single value (1 row, 1 column).
@@ -994,13 +1006,13 @@ def poll_query_until(self,
994
1006
sleep_time: how much should we sleep after a failure?
995
1007
expected: what should be returned to break the cycle?
996
1008
commit: should (possible) changes be committed?
997
- raise_programming_error: enable ProgrammingError?
998
- raise_internal_error: enable InternalError?
1009
+ suppress: a collection of exceptions to be suppressed.
999
1010
1000
1011
Examples:
1001
- poll_query_until('select true')
1002
- poll_query_until('postgres', "select now() > '01.01.2018'")
1003
- poll_query_until('select false', expected=True, max_attempts=4)
1012
+ >>> poll_query_until('select true')
1013
+ >>> poll_query_until('postgres', "select now() > '01.01.2018'")
1014
+ >>> poll_query_until('select false', expected=True, max_attempts=4)
1015
+ >>> poll_query_until('select 1', suppress={testgres.OperationalError})
1004
1016
"""
1005
1017
1006
1018
# sanity checks
@@ -1032,13 +1044,8 @@ def poll_query_until(self,
1032
1044
elif expected is None :
1033
1045
return # done
1034
1046
1035
- except ProgrammingError as e :
1036
- if raise_programming_error :
1037
- raise e
1038
-
1039
- except InternalError as e :
1040
- if raise_internal_error :
1041
- raise e
1047
+ except tuple (suppress or []):
1048
+ pass # we're suppressing them
1042
1049
1043
1050
time .sleep (sleep_time )
1044
1051
attempts += 1
@@ -1229,13 +1236,14 @@ def pgbench_run(self, dbname=None, username=None, options=[], **kwargs):
1229
1236
options: additional options for pgbench (list).
1230
1237
1231
1238
**kwargs: named options for pgbench.
1232
- Examples:
1233
- pgbench_run(initialize=True, scale=2)
1234
- pgbench_run(time=10)
1235
1239
Run pgbench --help to learn more.
1236
1240
1237
1241
Returns:
1238
1242
Stdout produced by pgbench.
1243
+
1244
+ Examples:
1245
+ >>> pgbench_run(initialize=True, scale=2)
1246
+ >>> pgbench_run(time=10)
1239
1247
"""
1240
1248
1241
1249
# Set default arguments
0 commit comments