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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
funbringer merged 13 commits intopostgrespro:masterfromFenimorkin:pg_dump
May 8, 2018
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modifiedsetup.py
100644 → 100755
View file
Open in desktop
Empty file.
11 changes: 11 additions & 0 deletionstestgres/enums.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionstestgres/node.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -54,7 +57,8 @@
QueryException, \
StartNodeException, \
TimeoutException, \
TestgresException
TestgresException, \
BackupException

from .logger import TestgresLogger

Expand DownExpand 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 deletionstests/test_simple.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp