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.
12 changes: 8 additions & 4 deletions testgres/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,8 @@ def cleanup(self, max_attempts=3):
def psql(self,
dbname,
query=None,
filename=None,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguments should not be moved around.

username=None,
filename=None,
input=None):
"""
Execute a query using psql.
Expand Down Expand Up @@ -658,7 +658,7 @@ def safe_psql(self, dbname, query, username=None, input=None):

return out

def dump(self, dbname, username=None, filename=None):
def dump(self, dbname, username=None, filename=None, formate=None):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should replace formate with format (I know, it's a function name, but who cares).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"""
Dump database into a file using pg_dump.
NOTE: the file is not removed automatically.
Expand All @@ -667,13 +667,15 @@ def dump(self, dbname, username=None, filename=None):
dbname: database name to connect to.
username: database user name.
filename: output file.
formate: format argument p/c/d/t

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

# Set default arguments
username = username or _default_username()
formate = formate or "p"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"p" should be replaced with a const defined in consts.py

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

f, filename = filename or tempfile.mkstemp()
os.close(f)

Expand All @@ -684,7 +686,8 @@ def dump(self, dbname, username=None, filename=None):
"-h", self.host,
"-f", filename,
"-U", username,
"-d", dbname
"-d", dbname,
"-F", formate
]

_execute_utility(_params, self.utils_log_name)
Expand All @@ -697,10 +700,11 @@ def restore(self, dbname, filename, username=None):

Args:
dbname: database name to connect to.
username: database user name.
filename: database dump taken by pg_dump.
"""

self.psql(dbname=dbname, filename=filename, username=username)
self.psql(dbname=dbname, username=username, filename=filename)

def poll_query_until(self,
dbname,
Expand Down
140 changes: 140 additions & 0 deletions tests/test_pgdump.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#!/usr/bin/env python
# coding: utf-8

import os
import subprocess
import tempfile
import testgres
import time
import unittest

import pdb

import logging.config

from distutils.version import LooseVersion

from testgres import \
InitNodeException, \
StartNodeException, \
ExecUtilException, \
BackupException, \
QueryException, \
CatchUpException, \
TimeoutException

from testgres import \
TestgresConfig, \
configure_testgres

from testgres import \
NodeStatus, \
IsolationLevel, \
get_new_node

from testgres import \
get_bin_path, \
get_pg_config

from testgres import bound_ports

# FIRST test from past
# with testgres.get_new_node('test') as node:
# node.init() # run initdb
# node.start() # start PostgreSQL
# #print(node.execute('postgres', 'select 1'))
# #print(node.psql('postgres', 'select 1'))
# print(node.connect('postgres', 'vis'))
# with node.connect() as con:
# con.begin('serializable')
# print(con.execute('select %s', 1))
# con.rollback()
# node.stop() # stop PostgreSQL


# test replication behavoiur on HOT_STANDBY_FEEDBACK
# def set_trace(con, command="pg_debug"):
# pid = con.execute("select pg_backend_pid()")[0][0]
# p = subprocess.Popen([command], stdin=subprocess.PIPE)
# p.communicate(str(pid).encode())

# with get_new_node() as master:
# master.init().start()
# with master.backup() as backup:
# with backup.spawn_replica() as replica:
# replica = replica.start()
# master.execute('postgres', 'create table test (val int4)')
# master.execute('postgres', 'insert into test values (0), (1), (2)')
# replica.catchup() # wait until changes are visible
# with replica.connect() as con1:
# set_trace(con1)
# import pdb; pdb.set_trace() # Важно,если последний идет pdb,то pass
# pass

# print(replica.execute('postgres', 'select count(*) from test'))
#print(replica.execute('postgres', ':gdb'))



# SECOND test dump new keys
with get_new_node('node1') as node1:
node1.init().start()

with node1.connect('postgres') as con:
con.begin()
con.execute('create table test (val int)')
con.execute('insert into test values (1), (2)')
con.commit()

# take a new dump plain format
dump = node1.dump('postgres')
# self.assertTrue(os.path.isfile(dump))
with get_new_node('node2') as node2:
node2.init().start().restore('postgres', dump)
res = node2.execute('postgres','select * from test order by val asc')
# self.assertListEqual(res, [(1, ), (2, )])
# finally, remove dump
os.remove(dump)

# take a new dump custom format
dump = node1.dump('postgres')
with get_new_node('node2') as node2:
node2.init().start().restore('postgres', dump)
res = node2.execute('postgres','select * from test order by val asc')
os.remove(dump)

# take a new dump directory format
dump = node1.dump('postgres')
with get_new_node('node2') as node2:
node2.init().start().restore('postgres', dump)
res = node2.execute('postgres','select * from test order by val asc')
os.remove(dump)

# take a new dump tar format
dump = node1.dump('postgres')
with get_new_node('node2') as node2:
node2.init().start().restore('postgres', dump)
res = node2.execute('postgres','select * from test order by val asc')
os.remove(dump)

# take a new dump tar format
dump = node1.dump('postgres')
with get_new_node('node2') as node2:
node2.init().start().restore('postgres', dump)
res = node2.execute('postgres','select * from test order by val asc')
os.remove(dump)

# 1) make dump to new place
# pg_dump mydb > db.sql
# 2) make dump to non default formate
# 2.5) Чтобы сформировать выгрузку в формате plain:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Russian comments is a big no-no.

# pg_dump -Fp mydb > db.dump
# 2.1) Чтобы сформировать выгрузку в формате custom:
# pg_dump -Fc mydb > db.dump
# 2.2) Чтобы сформировать выгрузку в формате directory:
# pg_dump -Fd mydb -f dumpdir
# 2.3) ? Чтобы сформировать выгрузку в формате directory в 5 параллельных потоков:
# pg_dump -Fd mydb -j 5 -f dumpdir
# 2.4) Чтобы сформировать выгрузку в формате tar:
# pg_dump -Ft mydb > db.dump