Skip to content

adding key -F to pg_dump #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
May 8, 2018
Empty file modified setup.py
100644 → 100755
Empty file.
11 changes: 11 additions & 0 deletions testgres/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,14 @@ def from_process(process):

# default
return ProcessType.Unknown


class DumpFormat(Enum):
"""
Available dump formats
"""

Plain = 'plain'
Custom = 'custom'
Directory = 'directory'
Tar = 'tar'
55 changes: 47 additions & 8 deletions testgres/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
from six import raise_from, iteritems
from tempfile import mkstemp, mkdtemp

from .enums import NodeStatus, ProcessType
from .enums import \
NodeStatus, \
ProcessType, \
DumpFormat

from .cache import cached_initdb

Expand Down Expand Up @@ -54,7 +57,8 @@
QueryException, \
StartNodeException, \
TimeoutException, \
TestgresException
TestgresException, \
BackupException

from .logger import TestgresLogger

Expand Down Expand Up @@ -803,7 +807,11 @@ def safe_psql(self, query=None, **kwargs):

return out

def dump(self, filename=None, dbname=None, username=None):
def dump(self,
filename=None,
dbname=None,
username=None,
format=DumpFormat.Plain):
"""
Dump database into a file using pg_dump.
NOTE: the file is not removed automatically.
Expand All @@ -812,14 +820,27 @@ def dump(self, filename=None, dbname=None, username=None):
filename: database dump taken by pg_dump.
dbname: database name to connect to.
username: database user name.
format: format argument plain/custom/directory/tar.

Returns:
Path to a file containing dump.
"""

# Check arguments
if not isinstance(format, DumpFormat):
try:
format = DumpFormat(format)
except ValueError:
msg = 'Invalid format "{}"'.format(format)
raise BackupException(msg)

# Generate tmpfile or tmpdir
def tmpfile():
fd, fname = mkstemp(prefix=TMP_DUMP)
os.close(fd)
if format == DumpFormat.Directory:
fname = mkdtemp(prefix=TMP_DUMP)
else:
fd, fname = mkstemp(prefix=TMP_DUMP)
os.close(fd)
return fname

# Set default arguments
Expand All @@ -833,7 +854,8 @@ def tmpfile():
"-h", self.host,
"-f", filename,
"-U", username,
"-d", dbname
"-d", dbname,
"-F", format.value
] # yapf: disable

execute_utility(_params, self.utils_log_file)
Expand All @@ -845,12 +867,29 @@ def restore(self, filename, dbname=None, username=None):
Restore database from pg_dump's file.

Args:
filename: database dump taken by pg_dump.
filename: database dump taken by pg_dump in custom/directory/tar formats.
dbname: database name to connect to.
username: database user name.
"""

self.psql(filename=filename, dbname=dbname, username=username)
# Set default arguments
dbname = dbname or default_dbname()
username = username or default_username()

_params = [
get_bin_path("pg_restore"),
"-p", str(self.port),
"-h", self.host,
"-U", username,
"-d", dbname,
filename
] # yapf: disable

# try pg_restore if dump is binary formate, and psql if not
try:
execute_utility(_params, self.utils_log_name)
except ExecUtilException:
self.psql(filename=filename, dbname=dbname, username=username)

@method_decorator(positional_args_hack(['dbname', 'query']))
def poll_query_until(self,
Expand Down
23 changes: 13 additions & 10 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def removing(f):
finally:
if os.path.isfile(f):
os.remove(f)
elif os.path.isdir(f):
rmtree(f, ignore_errors=True)


class TestgresTests(unittest.TestCase):
Expand Down Expand Up @@ -426,16 +428,17 @@ def test_dump(self):
with get_new_node().init().start() as node1:

node1.execute(query_create)

# take a new dump
with removing(node1.dump()) as dump:
with get_new_node().init().start() as node2:
# restore dump
self.assertTrue(os.path.isfile(dump))
node2.restore(filename=dump)

res = node2.execute(query_select)
self.assertListEqual(res, [(1, ), (2, )])
for format in ['plain', 'custom', 'directory', 'tar']:
with removing(node1.dump(format=format)) as dump:
with get_new_node().init().start() as node3:
if format == 'directory':
self.assertTrue(os.path.isdir(dump))
else:
self.assertTrue(os.path.isfile(dump))
# restore dump
node3.restore(filename=dump)
res = node3.execute(query_select)
self.assertListEqual(res, [(1, ), (2, )])

def test_users(self):
with get_new_node().init().start() as node:
Expand Down