- Notifications
You must be signed in to change notification settings - Fork35
Add pg_upgrade option#97
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
version: '3.8' | ||
services: | ||
tests: | ||
build: . |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -127,20 +127,23 @@ def __repr__(self): | ||
class PostgresNode(object): | ||
def __init__(self, name=None, port=None, base_dir=None, conn_params: ConnectionParams = ConnectionParams(), bin_dir=None, prefix=None): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Do you really need to change anything with respect to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Prefix is used as parameter for this structure | ||
""" | ||
PostgresNode constructor. | ||
Args: | ||
name: node's application name. | ||
port: port to accept connections. | ||
base_dir: path to node's data directory. | ||
bin_dir: path to node's binary directory. | ||
""" | ||
# private | ||
self._pg_version = PgVer(get_pg_version(bin_dir)) | ||
self._should_free_port = port is None | ||
self._base_dir = base_dir | ||
self._bin_dir = bin_dir | ||
self._prefix = prefix | ||
self._logger = None | ||
self._master = None | ||
@@ -281,14 +284,20 @@ def master(self): | ||
@property | ||
def base_dir(self): | ||
if not self._base_dir: | ||
self._base_dir = self.os_ops.mkdtemp(prefix=self._prefix orTMP_NODE) | ||
# NOTE: it's safe to create a new dir | ||
if not self.os_ops.path_exists(self._base_dir): | ||
self.os_ops.makedirs(self._base_dir) | ||
return self._base_dir | ||
@property | ||
def bin_dir(self): | ||
if not self._bin_dir: | ||
self._bin_dir = os.path.dirname(get_bin_path("pg_config")) | ||
return self._bin_dir | ||
@property | ||
def logs_dir(self): | ||
path = os.path.join(self.base_dir, LOGS_DIR) | ||
@@ -441,7 +450,7 @@ def _collect_special_files(self): | ||
return result | ||
def init(self, initdb_params=None,cached=True,**kwargs): | ||
""" | ||
Perform initdb for this node. | ||
@@ -460,7 +469,9 @@ def init(self, initdb_params=None, **kwargs): | ||
data_dir=self.data_dir, | ||
logfile=self.utils_log_file, | ||
os_ops=self.os_ops, | ||
params=initdb_params, | ||
bin_path=self.bin_dir, | ||
cached=False) | ||
# initialize default config files | ||
self.default_conf(**kwargs) | ||
@@ -619,7 +630,7 @@ def status(self): | ||
try: | ||
_params = [ | ||
self._get_bin_path('pg_ctl'), | ||
"-D", self.data_dir, | ||
"status" | ||
] # yapf: disable | ||
@@ -645,7 +656,7 @@ def get_control_data(self): | ||
""" | ||
# this one is tricky (blame PG 9.4) | ||
_params = [self._get_bin_path("pg_controldata")] | ||
_params += ["-D"] if self._pg_version >= PgVer('9.5') else [] | ||
_params += [self.data_dir] | ||
@@ -708,7 +719,7 @@ def start(self, params=[], wait=True): | ||
return self | ||
_params = [ | ||
self._get_bin_path("pg_ctl"), | ||
"-D", self.data_dir, | ||
"-l", self.pg_log_file, | ||
"-w" if wait else '-W', # --wait or --no-wait | ||
@@ -742,7 +753,7 @@ def stop(self, params=[], wait=True): | ||
return self | ||
_params = [ | ||
self._get_bin_path("pg_ctl"), | ||
"-D", self.data_dir, | ||
"-w" if wait else '-W', # --wait or --no-wait | ||
"stop" | ||
@@ -782,7 +793,7 @@ def restart(self, params=[]): | ||
""" | ||
_params = [ | ||
self._get_bin_path("pg_ctl"), | ||
"-D", self.data_dir, | ||
"-l", self.pg_log_file, | ||
"-w", # wait | ||
@@ -814,7 +825,7 @@ def reload(self, params=[]): | ||
""" | ||
_params = [ | ||
self._get_bin_path("pg_ctl"), | ||
"-D", self.data_dir, | ||
"reload" | ||
] + params # yapf: disable | ||
@@ -835,7 +846,7 @@ def promote(self, dbname=None, username=None): | ||
""" | ||
_params = [ | ||
self._get_bin_path("pg_ctl"), | ||
"-D", self.data_dir, | ||
"-w", # wait | ||
"promote" | ||
@@ -871,7 +882,7 @@ def pg_ctl(self, params): | ||
""" | ||
_params = [ | ||
self._get_bin_path("pg_ctl"), | ||
"-D", self.data_dir, | ||
"-w" # wait | ||
] + params # yapf: disable | ||
@@ -945,7 +956,7 @@ def psql(self, | ||
username = username or default_username() | ||
psql_params = [ | ||
self._get_bin_path("psql"), | ||
"-p", str(self.port), | ||
"-h", self.host, | ||
"-U", username, | ||
@@ -1066,7 +1077,7 @@ def tmpfile(): | ||
filename = filename or tmpfile() | ||
_params = [ | ||
self._get_bin_path("pg_dump"), | ||
"-p", str(self.port), | ||
"-h", self.host, | ||
"-f", filename, | ||
@@ -1094,7 +1105,7 @@ def restore(self, filename, dbname=None, username=None): | ||
username = username or default_username() | ||
_params = [ | ||
self._get_bin_path("pg_restore"), | ||
"-p", str(self.port), | ||
"-h", self.host, | ||
"-U", username, | ||
@@ -1364,7 +1375,7 @@ def pgbench(self, | ||
username = username or default_username() | ||
_params = [ | ||
self._get_bin_path("pgbench"), | ||
"-p", str(self.port), | ||
"-h", self.host, | ||
"-U", username, | ||
@@ -1416,7 +1427,7 @@ def pgbench_run(self, dbname=None, username=None, options=[], **kwargs): | ||
username = username or default_username() | ||
_params = [ | ||
self._get_bin_path("pgbench"), | ||
"-p", str(self.port), | ||
"-h", self.host, | ||
"-U", username, | ||
@@ -1587,6 +1598,43 @@ def set_auto_conf(self, options, config='postgresql.auto.conf', rm_options={}): | ||
self.os_ops.write(path, auto_conf, truncate=True) | ||
def upgrade_from(self, old_node): | ||
""" | ||
Upgrade this node from an old node using pg_upgrade. | ||
Args: | ||
old_node: An instance of PostgresNode representing the old node. | ||
""" | ||
if not os.path.exists(old_node.data_dir): | ||
raise Exception("Old node must be initialized") | ||
if not os.path.exists(self.data_dir): | ||
self.init() | ||
pg_upgrade_binary = self._get_bin_path("pg_upgrade") | ||
if not os.path.exists(pg_upgrade_binary): | ||
raise Exception("pg_upgrade does not exist in the new node's binary path") | ||
upgrade_command = [ | ||
pg_upgrade_binary, | ||
"--old-bindir", old_node.bin_dir, | ||
"--new-bindir", self.bin_dir, | ||
"--old-datadir", old_node.data_dir, | ||
"--new-datadir", self.data_dir, | ||
"--old-port", str(old_node.port), | ||
"--new-port", str(self.port), | ||
] | ||
return self.os_ops.exec_command(upgrade_command) | ||
def _get_bin_path(self, filename): | ||
if self.bin_dir: | ||
bin_path = os.path.join(self.bin_dir, filename) | ||
else: | ||
bin_path = get_bin_path(filename) | ||
return bin_path | ||
class NodeApp: | ||