Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
flags = ["--exclude-table={0}".format(t)
for t in exclude_tables]
flags.extend(["--exclude-table-data={0}".format(t)
for t in exclude_tables])
pg_dump_bin = os.path.join(self._bin_dir, 'pg_dump')
command = [pg_dump_bin,
'-a',
'--host', self._host,
'--port', self._port,
'-U', self._username,
db_name,
'-f', destination_path]
if table:
command += ['--table', table]
command.extend(flags)
run_shell(command)
def _fix_snapshot_ssh_db(tenant, orig, replace):
python_bin = '/opt/manager/env/bin/python'
dir_path = os.path.dirname(os.path.realpath(__file__))
script_path = os.path.join(dir_path, 'fix_snapshot_ssh_db.py')
command = [python_bin, script_path, tenant, orig, replace]
res = run(command)
if res and hasattr(res, 'aggr_stdout'):
ctx.logger.debug('Process result: \n{0}'
.format(res.aggr_stdout))
def dump_status_reporter_roles(self, tempdir):
ctx.logger.debug('Dumping status reporter users\' roles')
path = os.path.join(tempdir, 'status_reporter_roles.dump')
command = self.get_psql_command(self._db_name)
query = (
'select array_to_json(array_agg(row)) from ('
'select * from users_roles where user_id in ({0})'
') row;'.format(_STATUS_REPORTERS_IDS_QUERY_TUPLE)
)
command.extend([
'-c', query,
'-t', # Dump just the data, without extra headers, etc
'-o', path,
])
run_shell(command)
ctx.logger.debug('Dumped status reporter roles to {}'.format(path))
return path
def _restore_dump(self, dump_file, db_name, table=None):
"""Execute `psql` to restore an SQL dump into the DB
"""
ctx.logger.debug('Restoring db dump file: {0}'.format(dump_file))
command = self.get_psql_command(db_name)
command.extend([
'-v', 'ON_ERROR_STOP=1',
'--single-transaction',
'-f', dump_file
])
if table:
command += ['--table', table]
run_shell(command)
def _prepend_dump(dump_file, queries):
queries_str = '\n'.join(queries)
ctx.logger.debug('Adding to beginning of dump: {0}'
.format(queries_str))
pre_dump_file = '{0}.pre'.format(dump_file)
new_dump_file = '{0}.new'.format(dump_file)
with open(pre_dump_file, 'a') as f:
f.write('\n{0}\n'.format(queries_str))
# using cat command and output redirection
# to avoid reading file content into memory (for big dumps)
cat_content = 'cat {0} {1}'.format(pre_dump_file, dump_file)
run_shell(command=cat_content, redirect_output_path=new_dump_file)
return new_dump_file
def create_db(self):
ctx.logger.debug('Creating db')
create_db_bin = os.path.join(self._bin_dir, 'createdb')
command = [create_db_bin,
'--host', self._host,
'--port', self._port,
'-U', self._username,
'-T', 'template0',
self._db_name]
run_shell(command)