Run "psql" with "--no-psqlrc"

Some settings in user's .psqlrc, e.g. "linestyle unicode", may break the
CSV output. "--no-psqlrc" tells psql to not read startup file.

This is not necessary for the analyze_command and restore_command (with
all_databases), but it's generally recommended when running psql from a
script.
This commit is contained in:
Jakub Jirutka 2023-04-14 15:49:49 +02:00
parent 874fba7672
commit 19a00371f5
2 changed files with 14 additions and 10 deletions

View file

@ -61,7 +61,7 @@ def database_names_to_dump(database, extra_environment, log_prefix, dry_run):
psql_command = database.get('psql_command') or 'psql' psql_command = database.get('psql_command') or 'psql'
list_command = ( list_command = (
(psql_command, '--list', '--no-password', '--csv', '--tuples-only') (psql_command, '--list', '--no-password', '--no-psqlrc', '--csv', '--tuples-only')
+ (('--host', database['hostname']) if 'hostname' in database else ()) + (('--host', database['hostname']) if 'hostname' in database else ())
+ (('--port', str(database['port'])) if 'port' in database else ()) + (('--port', str(database['port'])) if 'port' in database else ())
+ (('--username', database['username']) if 'username' in database else ()) + (('--username', database['username']) if 'username' in database else ())
@ -205,7 +205,7 @@ def restore_database_dump(database_config, log_prefix, location_config, dry_run,
) )
psql_command = database.get('psql_command') or 'psql' psql_command = database.get('psql_command') or 'psql'
analyze_command = ( analyze_command = (
(psql_command, '--no-password', '--quiet') (psql_command, '--no-password', '--no-psqlrc', '--quiet')
+ (('--host', database['hostname']) if 'hostname' in database else ()) + (('--host', database['hostname']) if 'hostname' in database else ())
+ (('--port', str(database['port'])) if 'port' in database else ()) + (('--port', str(database['port'])) if 'port' in database else ())
+ (('--username', database['username']) if 'username' in database else ()) + (('--username', database['username']) if 'username' in database else ())
@ -219,7 +219,7 @@ def restore_database_dump(database_config, log_prefix, location_config, dry_run,
+ ( + (
('--if-exists', '--exit-on-error', '--clean', '--dbname', database['name']) ('--if-exists', '--exit-on-error', '--clean', '--dbname', database['name'])
if not all_databases if not all_databases
else () else ('--no-psqlrc',)
) )
+ (('--host', database['hostname']) if 'hostname' in database else ()) + (('--host', database['hostname']) if 'hostname' in database else ())
+ (('--port', str(database['port'])) if 'port' in database else ()) + (('--port', str(database['port'])) if 'port' in database else ())

View file

@ -56,6 +56,7 @@ def test_database_names_to_dump_with_all_and_format_lists_databases_with_hostnam
'psql', 'psql',
'--list', '--list',
'--no-password', '--no-password',
'--no-psqlrc',
'--csv', '--csv',
'--tuples-only', '--tuples-only',
'--host', '--host',
@ -75,7 +76,7 @@ def test_database_names_to_dump_with_all_and_format_lists_databases_with_hostnam
def test_database_names_to_dump_with_all_and_format_lists_databases_with_username(): def test_database_names_to_dump_with_all_and_format_lists_databases_with_username():
database = {'name': 'all', 'format': 'custom', 'username': 'postgres'} database = {'name': 'all', 'format': 'custom', 'username': 'postgres'}
flexmock(module).should_receive('execute_command_and_capture_output').with_args( flexmock(module).should_receive('execute_command_and_capture_output').with_args(
('psql', '--list', '--no-password', '--csv', '--tuples-only', '--username', 'postgres'), ('psql', '--list', '--no-password', '--no-psqlrc', '--csv', '--tuples-only', '--username', 'postgres'),
extra_environment=object, extra_environment=object,
).and_return('foo,test,\nbar,test,"stuff and such"') ).and_return('foo,test,\nbar,test,"stuff and such"')
@ -88,7 +89,7 @@ def test_database_names_to_dump_with_all_and_format_lists_databases_with_usernam
def test_database_names_to_dump_with_all_and_format_lists_databases_with_options(): def test_database_names_to_dump_with_all_and_format_lists_databases_with_options():
database = {'name': 'all', 'format': 'custom', 'list_options': '--harder'} database = {'name': 'all', 'format': 'custom', 'list_options': '--harder'}
flexmock(module).should_receive('execute_command_and_capture_output').with_args( flexmock(module).should_receive('execute_command_and_capture_output').with_args(
('psql', '--list', '--no-password', '--csv', '--tuples-only', '--harder'), ('psql', '--list', '--no-password', '--no-psqlrc', '--csv', '--tuples-only', '--harder'),
extra_environment=object, extra_environment=object,
).and_return('foo,test,\nbar,test,"stuff and such"') ).and_return('foo,test,\nbar,test,"stuff and such"')
@ -433,7 +434,7 @@ def test_restore_database_dump_runs_pg_restore():
extra_environment={'PGSSLMODE': 'disable'}, extra_environment={'PGSSLMODE': 'disable'},
).once() ).once()
flexmock(module).should_receive('execute_command').with_args( flexmock(module).should_receive('execute_command').with_args(
('psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'), ('psql', '--no-password', '--no-psqlrc', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
extra_environment={'PGSSLMODE': 'disable'}, extra_environment={'PGSSLMODE': 'disable'},
).once() ).once()
@ -487,6 +488,7 @@ def test_restore_database_dump_runs_pg_restore_with_hostname_and_port():
( (
'psql', 'psql',
'--no-password', '--no-password',
'--no-psqlrc',
'--quiet', '--quiet',
'--host', '--host',
'database.example.org', 'database.example.org',
@ -535,6 +537,7 @@ def test_restore_database_dump_runs_pg_restore_with_username_and_password():
( (
'psql', 'psql',
'--no-password', '--no-password',
'--no-psqlrc',
'--quiet', '--quiet',
'--username', '--username',
'postgres', 'postgres',
@ -580,6 +583,7 @@ def test_restore_database_dump_runs_pg_restore_with_options():
( (
'psql', 'psql',
'--no-password', '--no-password',
'--no-psqlrc',
'--quiet', '--quiet',
'--dbname', '--dbname',
'foo', 'foo',
@ -603,14 +607,14 @@ def test_restore_database_dump_runs_psql_for_all_database_dump():
flexmock(module).should_receive('make_dump_path') flexmock(module).should_receive('make_dump_path')
flexmock(module.dump).should_receive('make_database_dump_filename') flexmock(module.dump).should_receive('make_database_dump_filename')
flexmock(module).should_receive('execute_command_with_processes').with_args( flexmock(module).should_receive('execute_command_with_processes').with_args(
('psql', '--no-password'), ('psql', '--no-password', '--no-psqlrc'),
processes=[extract_process], processes=[extract_process],
output_log_level=logging.DEBUG, output_log_level=logging.DEBUG,
input_file=extract_process.stdout, input_file=extract_process.stdout,
extra_environment={'PGSSLMODE': 'disable'}, extra_environment={'PGSSLMODE': 'disable'},
).once() ).once()
flexmock(module).should_receive('execute_command').with_args( flexmock(module).should_receive('execute_command').with_args(
('psql', '--no-password', '--quiet', '--command', 'ANALYZE'), ('psql', '--no-password', '--no-psqlrc', '--quiet', '--command', 'ANALYZE'),
extra_environment={'PGSSLMODE': 'disable'}, extra_environment={'PGSSLMODE': 'disable'},
).once() ).once()
@ -644,7 +648,7 @@ def test_restore_database_dump_runs_non_default_pg_restore_and_psql():
extra_environment={'PGSSLMODE': 'disable'}, extra_environment={'PGSSLMODE': 'disable'},
).once() ).once()
flexmock(module).should_receive('execute_command').with_args( flexmock(module).should_receive('execute_command').with_args(
('special_psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'), ('special_psql', '--no-password', '--no-psqlrc', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
extra_environment={'PGSSLMODE': 'disable'}, extra_environment={'PGSSLMODE': 'disable'},
).once() ).once()
@ -689,7 +693,7 @@ def test_restore_database_dump_without_extract_process_restores_from_disk():
extra_environment={'PGSSLMODE': 'disable'}, extra_environment={'PGSSLMODE': 'disable'},
).once() ).once()
flexmock(module).should_receive('execute_command').with_args( flexmock(module).should_receive('execute_command').with_args(
('psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'), ('psql', '--no-password', '--no-psqlrc', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
extra_environment={'PGSSLMODE': 'disable'}, extra_environment={'PGSSLMODE': 'disable'},
).once() ).once()