fix: add postgres.dump_db()
function
This commit is contained in:
parent
c2e18d854c
commit
34459c008f
|
@ -152,3 +152,34 @@ def drop_db(c, name, checkfirst=True):
|
|||
"""
|
||||
if not checkfirst or db_exists(c, name):
|
||||
c.sudo(f'dropdb {name}', user='postgres')
|
||||
|
||||
|
||||
def dump_db(c, name):
|
||||
"""
|
||||
Dump a PostgreSQL database to file.
|
||||
|
||||
This uses the ``pg_dump`` and ``gzip`` commands to produce a
|
||||
compressed SQL dump. The filename returned is based on the
|
||||
``name`` provided, e.g. ``mydbname.sql.gz``.
|
||||
|
||||
:param c: Fabric connection.
|
||||
|
||||
:param name: Name of the database to dump.
|
||||
|
||||
:returns: Base name of the output file. We only return the
|
||||
filename and not the path, since the file is expected to exist
|
||||
in the connected user's home folder.
|
||||
"""
|
||||
sql_name = f'{name}.sql'
|
||||
gz_name = f'{sql_name}.gz'
|
||||
tmp_name = f'/tmp/{gz_name}'
|
||||
|
||||
# TODO: when pg_dump fails the command still succeeds! (would this work?)
|
||||
#cmd = f'set -e && pg_dump {name} | gzip -c > {tmp_name}'
|
||||
cmd = f'pg_dump {name} | gzip -c > {tmp_name}'
|
||||
|
||||
c.sudo(cmd, user='postgres')
|
||||
c.run(f"cp {tmp_name} {gz_name}")
|
||||
c.run(f"rm {tmp_name}")
|
||||
|
||||
return gz_name
|
||||
|
|
|
@ -122,3 +122,13 @@ class TestDropDb(TestCase):
|
|||
mod.drop_db(c, 'foo')
|
||||
db_exists.assert_called_once_with(c, 'foo')
|
||||
c.sudo.assert_not_called()
|
||||
|
||||
|
||||
class TestDumpDb(TestCase):
|
||||
|
||||
def test_basic(self):
|
||||
c = MagicMock()
|
||||
result = mod.dump_db(c, 'foo')
|
||||
self.assertEqual(result, 'foo.sql.gz')
|
||||
c.sudo.assert_called_once_with('pg_dump foo | gzip -c > /tmp/foo.sql.gz', user='postgres')
|
||||
c.run.assert_called_with('rm /tmp/foo.sql.gz')
|
||||
|
|
Loading…
Reference in a new issue