Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a656ac1

Browse files
committed
pg_dump enum using cleanup
1 parent a4d1ab9 commit a656ac1

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

testgres/node.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ def dump(self,
810810
filename=None,
811811
dbname=None,
812812
username=None,
813-
format=DumpFormat.Plain.value):
813+
format=DumpFormat.Plain):
814814
"""
815815
Dump database into a file using pg_dump.
816816
NOTE: the file is not removed automatically.
@@ -825,8 +825,17 @@ def dump(self,
825825
Path to a file containing dump.
826826
"""
827827

828+
# Check arguments
829+
if not isinstance(format, DumpFormat):
830+
try:
831+
format = DumpFormat(format)
832+
except ValueError:
833+
msg = 'Invalid format "{}"'.format(format)
834+
raise BackupException(msg)
835+
836+
# Generate tmpfile or tmpdir
828837
def tmpfile():
829-
if format == DumpFormat.Directory.value:
838+
if format == DumpFormat.Directory:
830839
fname = mkdtemp(prefix=TMP_DUMP)
831840
else:
832841
fd, fname = mkstemp(prefix=TMP_DUMP)
@@ -845,7 +854,7 @@ def tmpfile():
845854
"-f", filename,
846855
"-U", username,
847856
"-d", dbname,
848-
"-F", format
857+
"-F", format.value
849858
] # yapf: disable
850859

851860
execute_utility(_params, self.utils_log_file)
@@ -872,7 +881,11 @@ def restore(self, filename, dbname=None, username=None):
872881
filename
873882
]
874883

875-
execute_utility(_params, self.utils_log_name)
884+
# try pg_restore if dump is binary formate, and psql if not
885+
try:
886+
execute_utility(_params, self.utils_log_name)
887+
except ExecUtilException as e:
888+
self.psql(filename=filename, dbname=dbname, username=username)
876889

877890
@method_decorator(positional_args_hack(['dbname', 'query']))
878891
def poll_query_until(self,

tests/test_simple.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,14 +432,14 @@ def test_dump(self):
432432
with get_new_node().init().start() as node2:
433433
# restore dump
434434
self.assertTrue(os.path.isfile(dump))
435-
node2.psql(filename=dump, dbname=None, username=None)
435+
node2.restore(filename=dump)
436436
res = node2.execute(query_select)
437437
self.assertListEqual(res, [(1, ), (2, )])
438438

439439
dump = node1.dump(format='plain')
440440
self.assertTrue(os.path.isfile(dump))
441441
with get_new_node().init().start() as node3:
442-
node3.psql(filename=dump, dbname=None, username=None)
442+
node3.restore(filename=dump)
443443
res = node3.execute(query_select)
444444
self.assertListEqual(res, [(1, ), (2, )])
445445
os.remove(dump)

0 commit comments

Comments
 (0)