diff --git a/borgmatic/actions/restore.py b/borgmatic/actions/restore.py index a59554a2..06fd1b87 100644 --- a/borgmatic/actions/restore.py +++ b/borgmatic/actions/restore.py @@ -109,8 +109,9 @@ def restore_single_database( # Run a single database restore, consuming the extract stdout (if any). borgmatic.hooks.dispatch.call_hooks( 'restore_database_dump', - {hook_name: [database]}, + config, repository['path'], + database['name'], borgmatic.hooks.dump.DATABASE_HOOK_NAMES, global_arguments.dry_run, extract_process, diff --git a/borgmatic/hooks/mongodb.py b/borgmatic/hooks/mongodb.py index 3c91a183..c94a084d 100644 --- a/borgmatic/hooks/mongodb.py +++ b/borgmatic/hooks/mongodb.py @@ -100,24 +100,32 @@ def make_database_dump_pattern(databases, config, log_prefix, name=None): # pra def restore_database_dump( - database_config, config, log_prefix, dry_run, extract_process, connection_params + databases_config, config, log_prefix, database_name, dry_run, extract_process, connection_params ): ''' - Restore the given MongoDB database from an extract stream. The database is supplied as a - one-element sequence containing a dict describing the database, as per the configuration schema. - Use the configuration dict to construct the destination path and the given log prefix in any log - entries. If this is a dry run, then don't actually restore anything. Trigger the given active - extract process (an instance of subprocess.Popen) to produce output to consume. + Restore the given MongoDB database from an extract stream. The databases are supplied as a + sequence containing one dict describing each database (as per the configuration schema), but + only the database corresponding to the given database name is restored. Use the configuration + dict to construct the destination path and the given log prefix in any log entries. If this is a + dry run, then don't actually restore anything. Trigger the given active extract process (an + instance of subprocess.Popen) to produce output to consume. If the extract process is None, then restore the dump from the filesystem rather than from an extract stream. ''' dry_run_label = ' (dry run; not actually restoring anything)' if dry_run else '' - if len(database_config) != 1: - raise ValueError('The database configuration value is invalid') + try: + database = next( + database_config + for database_config in databases_config + if database_config.get('name') == database_name + ) + except StopIteration: + raise ValueError( + f'A database named "{database_name}" could not be found in the configuration' + ) - database = database_config[0] dump_filename = dump.make_database_dump_filename( make_dump_path(config), database['name'], database.get('hostname') ) diff --git a/borgmatic/hooks/mysql.py b/borgmatic/hooks/mysql.py index 8ca2f6ba..a3b34f15 100644 --- a/borgmatic/hooks/mysql.py +++ b/borgmatic/hooks/mysql.py @@ -181,21 +181,27 @@ def make_database_dump_pattern(databases, config, log_prefix, name=None): # pra def restore_database_dump( - database_config, config, log_prefix, dry_run, extract_process, connection_params + databases_config, config, log_prefix, database_name, dry_run, extract_process, connection_params ): ''' - Restore the given MySQL/MariaDB database from an extract stream. The database is supplied as a - one-element sequence containing a dict describing the database, as per the configuration schema. - Use the given log prefix in any log entries. If this is a dry run, then don't actually restore - anything. Trigger the given active extract process (an instance of subprocess.Popen) to produce - output to consume. + Restore the given MySQL/MariaDB database from an extract stream. The databases are supplied as a + sequence containing one dict describing each database (as per the configuration schema), but + only the database corresponding to the given database name is restored. Use the given log + prefix in any log entries. If this is a dry run, then don't actually restore anything. Trigger + the given active extract process (an instance of subprocess.Popen) to produce output to consume. ''' dry_run_label = ' (dry run; not actually restoring anything)' if dry_run else '' - if len(database_config) != 1: - raise ValueError('The database configuration value is invalid') - - database = database_config[0] + try: + database = next( + database_config + for database_config in databases_config + if database_config.get('name') == database_name + ) + except StopIteration: + raise ValueError( + f'A database named "{database_name}" could not be found in the configuration' + ) hostname = connection_params['hostname'] or database.get( 'restore_hostname', database.get('hostname') diff --git a/borgmatic/hooks/postgresql.py b/borgmatic/hooks/postgresql.py index 7bef5a70..598b878c 100644 --- a/borgmatic/hooks/postgresql.py +++ b/borgmatic/hooks/postgresql.py @@ -202,14 +202,15 @@ def make_database_dump_pattern(databases, config, log_prefix, name=None): # pra def restore_database_dump( - database_config, config, log_prefix, dry_run, extract_process, connection_params + databases_config, config, log_prefix, database_name, dry_run, extract_process, connection_params ): ''' - Restore the given PostgreSQL database from an extract stream. The database is supplied as a - one-element sequence containing a dict describing the database, as per the configuration schema. - Use the given configuration dict to construct the destination path and the given log prefix in - any log entries. If this is a dry run, then don't actually restore anything. Trigger the given - active extract process (an instance of subprocess.Popen) to produce output to consume. + Restore the given PostgreSQL database from an extract stream. The databases are supplied as a + sequence containing one dict describing each database (as per the configuration schema), but + only the database corresponding to the given database name is restored. Use the given + configuration dict to construct the destination path and the given log prefix in any log + entries. If this is a dry run, then don't actually restore anything. Trigger the given active + extract process (an instance of subprocess.Popen) to produce output to consume. If the extract process is None, then restore the dump from the filesystem rather than from an extract stream. @@ -219,10 +220,16 @@ def restore_database_dump( ''' dry_run_label = ' (dry run; not actually restoring anything)' if dry_run else '' - if len(database_config) != 1: - raise ValueError('The database configuration value is invalid') - - database = database_config[0] + try: + database = next( + database_config + for database_config in databases_config + if database_config.get('name') == database_name + ) + except StopIteration: + raise ValueError( + f'A database named "{database_name}" could not be found in the configuration' + ) hostname = connection_params['hostname'] or database.get( 'restore_hostname', database.get('hostname') @@ -262,7 +269,7 @@ def restore_database_dump( + (() if extract_process else (dump_filename,)) + tuple( itertools.chain.from_iterable(('--schema', schema) for schema in database['schemas']) - if database['schemas'] + if database.get('schemas') else () ) ) diff --git a/borgmatic/hooks/sqlite.py b/borgmatic/hooks/sqlite.py index 109f253a..524318bc 100644 --- a/borgmatic/hooks/sqlite.py +++ b/borgmatic/hooks/sqlite.py @@ -84,22 +84,30 @@ def make_database_dump_pattern(databases, config, log_prefix, name=None): # pra def restore_database_dump( - database_config, config, log_prefix, dry_run, extract_process, connection_params + databases_config, config, log_prefix, database_name, dry_run, extract_process, connection_params ): ''' - Restore the given SQLite3 database from an extract stream. The database is supplied as a - one-element sequence containing a dict describing the database, as per the configuration schema. - Use the given log prefix in any log entries. If this is a dry run, then don't actually restore - anything. Trigger the given active extract process (an instance of subprocess.Popen) to produce - output to consume. + Restore the given SQLite3 database from an extract stream. The databases are supplied as a + sequence containing one dict describing each database (as per the configuration schema), but + only the database corresponding to the given database name is restored. Use the given log prefix + in any log entries. If this is a dry run, then don't actually restore anything. Trigger the + given active extract process (an instance of subprocess.Popen) to produce output to consume. ''' dry_run_label = ' (dry run; not actually restoring anything)' if dry_run else '' - if len(database_config) != 1: - raise ValueError('The database configuration value is invalid') + try: + database = next( + database_config + for database_config in databases_config + if database_config.get('name') == database_name + ) + except StopIteration: + raise ValueError( + f'A database named "{database_name}" could not be found in the configuration' + ) - database_path = connection_params['restore_path'] or database_config[0].get( - 'restore_path', database_config[0].get('path') + database_path = connection_params['restore_path'] or database.get( + 'restore_path', database.get('path') ) logger.debug(f'{log_prefix}: Restoring SQLite database at {database_path}{dry_run_label}') diff --git a/tests/unit/hooks/test_mongodb.py b/tests/unit/hooks/test_mongodb.py index 234201f4..a676b583 100644 --- a/tests/unit/hooks/test_mongodb.py +++ b/tests/unit/hooks/test_mongodb.py @@ -158,7 +158,7 @@ def test_dump_databases_runs_mongodumpall_for_all_databases(): def test_restore_database_dump_runs_mongorestore(): - database_config = [{'name': 'foo', 'schemas': None}] + databases_config = [{'name': 'foo', 'schemas': None}, {'name': 'bar'}] extract_process = flexmock(stdout=flexmock()) flexmock(module).should_receive('make_dump_path') @@ -171,9 +171,10 @@ def test_restore_database_dump_runs_mongorestore(): ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=extract_process, connection_params={ @@ -185,8 +186,8 @@ def test_restore_database_dump_runs_mongorestore(): ) -def test_restore_database_dump_errors_on_multiple_database_config(): - database_config = [{'name': 'foo'}, {'name': 'bar'}] +def test_restore_database_dump_errors_on_empty_databases_config(): + databases_config = [] flexmock(module).should_receive('make_dump_path') flexmock(module.dump).should_receive('make_database_dump_filename') @@ -195,9 +196,10 @@ def test_restore_database_dump_errors_on_multiple_database_config(): with pytest.raises(ValueError): module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=flexmock(), connection_params={ @@ -210,7 +212,7 @@ def test_restore_database_dump_errors_on_multiple_database_config(): def test_restore_database_dump_runs_mongorestore_with_hostname_and_port(): - database_config = [ + databases_config = [ {'name': 'foo', 'hostname': 'database.example.org', 'port': 5433, 'schemas': None} ] extract_process = flexmock(stdout=flexmock()) @@ -235,9 +237,10 @@ def test_restore_database_dump_runs_mongorestore_with_hostname_and_port(): ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=extract_process, connection_params={ @@ -250,7 +253,7 @@ def test_restore_database_dump_runs_mongorestore_with_hostname_and_port(): def test_restore_database_dump_runs_mongorestore_with_username_and_password(): - database_config = [ + databases_config = [ { 'name': 'foo', 'username': 'mongo', @@ -283,9 +286,10 @@ def test_restore_database_dump_runs_mongorestore_with_username_and_password(): ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=extract_process, connection_params={ @@ -298,7 +302,7 @@ def test_restore_database_dump_runs_mongorestore_with_username_and_password(): def test_restore_database_dump_with_connection_params_uses_connection_params_for_restore(): - database_config = [ + databases_config = [ { 'name': 'foo', 'username': 'mongo', @@ -339,9 +343,10 @@ def test_restore_database_dump_with_connection_params_uses_connection_params_for ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=extract_process, connection_params={ @@ -354,7 +359,7 @@ def test_restore_database_dump_with_connection_params_uses_connection_params_for def test_restore_database_dump_without_connection_params_uses_restore_params_in_config_for_restore(): - database_config = [ + databases_config = [ { 'name': 'foo', 'username': 'mongo', @@ -395,9 +400,10 @@ def test_restore_database_dump_without_connection_params_uses_restore_params_in_ ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=extract_process, connection_params={ @@ -410,7 +416,7 @@ def test_restore_database_dump_without_connection_params_uses_restore_params_in_ def test_restore_database_dump_runs_mongorestore_with_options(): - database_config = [{'name': 'foo', 'restore_options': '--harder', 'schemas': None}] + databases_config = [{'name': 'foo', 'restore_options': '--harder', 'schemas': None}] extract_process = flexmock(stdout=flexmock()) flexmock(module).should_receive('make_dump_path') @@ -423,9 +429,10 @@ def test_restore_database_dump_runs_mongorestore_with_options(): ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=extract_process, connection_params={ @@ -438,7 +445,7 @@ def test_restore_database_dump_runs_mongorestore_with_options(): def test_restore_databases_dump_runs_mongorestore_with_schemas(): - database_config = [{'name': 'foo', 'schemas': ['bar', 'baz']}] + databases_config = [{'name': 'foo', 'schemas': ['bar', 'baz']}] extract_process = flexmock(stdout=flexmock()) flexmock(module).should_receive('make_dump_path') @@ -461,9 +468,10 @@ def test_restore_databases_dump_runs_mongorestore_with_schemas(): ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=extract_process, connection_params={ @@ -476,7 +484,7 @@ def test_restore_databases_dump_runs_mongorestore_with_schemas(): def test_restore_database_dump_runs_psql_for_all_database_dump(): - database_config = [{'name': 'all', 'schemas': None}] + databases_config = [{'name': 'all', 'schemas': None}] extract_process = flexmock(stdout=flexmock()) flexmock(module).should_receive('make_dump_path') @@ -489,9 +497,10 @@ def test_restore_database_dump_runs_psql_for_all_database_dump(): ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='all', dry_run=False, extract_process=extract_process, connection_params={ @@ -504,16 +513,17 @@ def test_restore_database_dump_runs_psql_for_all_database_dump(): def test_restore_database_dump_with_dry_run_skips_restore(): - database_config = [{'name': 'foo', 'schemas': None}] + databases_config = [{'name': 'foo', 'schemas': None}] flexmock(module).should_receive('make_dump_path') flexmock(module.dump).should_receive('make_database_dump_filename') flexmock(module).should_receive('execute_command_with_processes').never() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=True, extract_process=flexmock(), connection_params={ @@ -526,7 +536,7 @@ def test_restore_database_dump_with_dry_run_skips_restore(): def test_restore_database_dump_without_extract_process_restores_from_disk(): - database_config = [{'name': 'foo', 'format': 'directory', 'schemas': None}] + databases_config = [{'name': 'foo', 'format': 'directory', 'schemas': None}] flexmock(module).should_receive('make_dump_path') flexmock(module.dump).should_receive('make_database_dump_filename').and_return('/dump/path') @@ -538,9 +548,10 @@ def test_restore_database_dump_without_extract_process_restores_from_disk(): ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=None, connection_params={ diff --git a/tests/unit/hooks/test_mysql.py b/tests/unit/hooks/test_mysql.py index 4b55be47..19ab3cc7 100644 --- a/tests/unit/hooks/test_mysql.py +++ b/tests/unit/hooks/test_mysql.py @@ -380,7 +380,7 @@ def test_dump_databases_does_not_error_for_missing_all_databases_with_dry_run(): def test_restore_database_dump_runs_mysql_to_restore(): - database_config = [{'name': 'foo'}] + databases_config = [{'name': 'foo'}, {'name': 'bar'}] extract_process = flexmock(stdout=flexmock()) flexmock(module).should_receive('execute_command_with_processes').with_args( @@ -392,9 +392,10 @@ def test_restore_database_dump_runs_mysql_to_restore(): ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=extract_process, connection_params={ @@ -406,30 +407,8 @@ def test_restore_database_dump_runs_mysql_to_restore(): ) -def test_restore_database_dump_errors_on_multiple_database_config(): - database_config = [{'name': 'foo'}, {'name': 'bar'}] - - flexmock(module).should_receive('execute_command_with_processes').never() - flexmock(module).should_receive('execute_command').never() - - with pytest.raises(ValueError): - module.restore_database_dump( - database_config, - {}, - 'test.yaml', - dry_run=False, - extract_process=flexmock(), - connection_params={ - 'hostname': None, - 'port': None, - 'username': None, - 'password': None, - }, - ) - - def test_restore_database_dump_runs_mysql_with_options(): - database_config = [{'name': 'foo', 'restore_options': '--harder'}] + databases_config = [{'name': 'foo', 'restore_options': '--harder'}] extract_process = flexmock(stdout=flexmock()) flexmock(module).should_receive('execute_command_with_processes').with_args( @@ -441,9 +420,10 @@ def test_restore_database_dump_runs_mysql_with_options(): ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=extract_process, connection_params={ @@ -456,7 +436,7 @@ def test_restore_database_dump_runs_mysql_with_options(): def test_restore_database_dump_runs_mysql_with_hostname_and_port(): - database_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}] + databases_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}] extract_process = flexmock(stdout=flexmock()) flexmock(module).should_receive('execute_command_with_processes').with_args( @@ -477,9 +457,10 @@ def test_restore_database_dump_runs_mysql_with_hostname_and_port(): ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=extract_process, connection_params={ @@ -492,7 +473,7 @@ def test_restore_database_dump_runs_mysql_with_hostname_and_port(): def test_restore_database_dump_runs_mysql_with_username_and_password(): - database_config = [{'name': 'foo', 'username': 'root', 'password': 'trustsome1'}] + databases_config = [{'name': 'foo', 'username': 'root', 'password': 'trustsome1'}] extract_process = flexmock(stdout=flexmock()) flexmock(module).should_receive('execute_command_with_processes').with_args( @@ -504,9 +485,10 @@ def test_restore_database_dump_runs_mysql_with_username_and_password(): ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=extract_process, connection_params={ @@ -519,7 +501,7 @@ def test_restore_database_dump_runs_mysql_with_username_and_password(): def test_restore_database_dump_with_connection_params_uses_connection_params_for_restore(): - database_config = [ + databases_config = [ { 'name': 'foo', 'username': 'root', @@ -552,9 +534,10 @@ def test_restore_database_dump_with_connection_params_uses_connection_params_for ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=extract_process, connection_params={ @@ -567,7 +550,7 @@ def test_restore_database_dump_with_connection_params_uses_connection_params_for def test_restore_database_dump_without_connection_params_uses_restore_params_in_config_for_restore(): - database_config = [ + databases_config = [ { 'name': 'foo', 'username': 'root', @@ -602,9 +585,10 @@ def test_restore_database_dump_without_connection_params_uses_restore_params_in_ ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=extract_process, connection_params={ @@ -617,14 +601,15 @@ def test_restore_database_dump_without_connection_params_uses_restore_params_in_ def test_restore_database_dump_with_dry_run_skips_restore(): - database_config = [{'name': 'foo'}] + databases_config = [{'name': 'foo'}] flexmock(module).should_receive('execute_command_with_processes').never() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=True, extract_process=flexmock(), connection_params={ diff --git a/tests/unit/hooks/test_postgresql.py b/tests/unit/hooks/test_postgresql.py index d7416100..51235ffc 100644 --- a/tests/unit/hooks/test_postgresql.py +++ b/tests/unit/hooks/test_postgresql.py @@ -464,7 +464,7 @@ def test_dump_databases_runs_non_default_pg_dump(): def test_restore_database_dump_runs_pg_restore(): - database_config = [{'name': 'foo', 'schemas': None}] + databases_config = [{'name': 'foo', 'schemas': None}, {'name': 'bar'}] extract_process = flexmock(stdout=flexmock()) flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'}) @@ -500,9 +500,10 @@ def test_restore_database_dump_runs_pg_restore(): ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=extract_process, connection_params={ @@ -514,33 +515,8 @@ def test_restore_database_dump_runs_pg_restore(): ) -def test_restore_database_dump_errors_on_multiple_database_config(): - database_config = [{'name': 'foo'}, {'name': 'bar'}] - - flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'}) - flexmock(module).should_receive('make_dump_path') - flexmock(module.dump).should_receive('make_database_dump_filename') - flexmock(module).should_receive('execute_command_with_processes').never() - flexmock(module).should_receive('execute_command').never() - - with pytest.raises(ValueError): - module.restore_database_dump( - database_config, - {}, - 'test.yaml', - dry_run=False, - extract_process=flexmock(), - connection_params={ - 'restore_hostname': None, - 'restore_port': None, - 'restore_username': None, - 'restore_password': None, - }, - ) - - def test_restore_database_dump_runs_pg_restore_with_hostname_and_port(): - database_config = [ + databases_config = [ {'name': 'foo', 'hostname': 'database.example.org', 'port': 5433, 'schemas': None} ] extract_process = flexmock(stdout=flexmock()) @@ -586,9 +562,10 @@ def test_restore_database_dump_runs_pg_restore_with_hostname_and_port(): ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=extract_process, connection_params={ @@ -601,7 +578,7 @@ def test_restore_database_dump_runs_pg_restore_with_hostname_and_port(): def test_restore_database_dump_runs_pg_restore_with_username_and_password(): - database_config = [ + databases_config = [ {'name': 'foo', 'username': 'postgres', 'password': 'trustsome1', 'schemas': None} ] extract_process = flexmock(stdout=flexmock()) @@ -645,9 +622,10 @@ def test_restore_database_dump_runs_pg_restore_with_username_and_password(): ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=extract_process, connection_params={ @@ -660,7 +638,7 @@ def test_restore_database_dump_runs_pg_restore_with_username_and_password(): def test_restore_database_dump_with_connection_params_uses_connection_params_for_restore(): - database_config = [ + databases_config = [ { 'name': 'foo', 'hostname': 'database.example.org', @@ -723,9 +701,10 @@ def test_restore_database_dump_with_connection_params_uses_connection_params_for ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=extract_process, connection_params={ @@ -738,7 +717,7 @@ def test_restore_database_dump_with_connection_params_uses_connection_params_for def test_restore_database_dump_without_connection_params_uses_restore_params_in_config_for_restore(): - database_config = [ + databases_config = [ { 'name': 'foo', 'hostname': 'database.example.org', @@ -801,9 +780,10 @@ def test_restore_database_dump_without_connection_params_uses_restore_params_in_ ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=extract_process, connection_params={ @@ -816,7 +796,7 @@ def test_restore_database_dump_without_connection_params_uses_restore_params_in_ def test_restore_database_dump_runs_pg_restore_with_options(): - database_config = [ + databases_config = [ { 'name': 'foo', 'restore_options': '--harder', @@ -861,9 +841,10 @@ def test_restore_database_dump_runs_pg_restore_with_options(): ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=extract_process, connection_params={ @@ -876,7 +857,7 @@ def test_restore_database_dump_runs_pg_restore_with_options(): def test_restore_database_dump_runs_psql_for_all_database_dump(): - database_config = [{'name': 'all', 'schemas': None}] + databases_config = [{'name': 'all', 'schemas': None}] extract_process = flexmock(stdout=flexmock()) flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'}) @@ -899,9 +880,10 @@ def test_restore_database_dump_runs_psql_for_all_database_dump(): ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='all', dry_run=False, extract_process=extract_process, connection_params={ @@ -914,7 +896,7 @@ def test_restore_database_dump_runs_psql_for_all_database_dump(): def test_restore_database_dump_runs_psql_for_plain_database_dump(): - database_config = [{'name': 'foo', 'format': 'plain', 'schemas': None}] + databases_config = [{'name': 'foo', 'format': 'plain', 'schemas': None}] extract_process = flexmock(stdout=flexmock()) flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'}) @@ -942,9 +924,10 @@ def test_restore_database_dump_runs_psql_for_plain_database_dump(): ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=extract_process, connection_params={ @@ -957,7 +940,7 @@ def test_restore_database_dump_runs_psql_for_plain_database_dump(): def test_restore_database_dump_runs_non_default_pg_restore_and_psql(): - database_config = [ + databases_config = [ { 'name': 'foo', 'pg_restore_command': 'docker exec mycontainer pg_restore', @@ -1006,9 +989,10 @@ def test_restore_database_dump_runs_non_default_pg_restore_and_psql(): ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=extract_process, connection_params={ @@ -1021,7 +1005,7 @@ def test_restore_database_dump_runs_non_default_pg_restore_and_psql(): def test_restore_database_dump_with_dry_run_skips_restore(): - database_config = [{'name': 'foo', 'schemas': None}] + databases_config = [{'name': 'foo', 'schemas': None}] flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'}) flexmock(module).should_receive('make_dump_path') @@ -1029,9 +1013,10 @@ def test_restore_database_dump_with_dry_run_skips_restore(): flexmock(module).should_receive('execute_command_with_processes').never() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=True, extract_process=flexmock(), connection_params={ @@ -1044,7 +1029,7 @@ def test_restore_database_dump_with_dry_run_skips_restore(): def test_restore_database_dump_without_extract_process_restores_from_disk(): - database_config = [{'name': 'foo', 'schemas': None}] + databases_config = [{'name': 'foo', 'schemas': None}] flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'}) flexmock(module).should_receive('make_dump_path') @@ -1080,9 +1065,10 @@ def test_restore_database_dump_without_extract_process_restores_from_disk(): ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=None, connection_params={ @@ -1095,7 +1081,7 @@ def test_restore_database_dump_without_extract_process_restores_from_disk(): def test_restore_database_dump_with_schemas_restores_schemas(): - database_config = [{'name': 'foo', 'schemas': ['bar', 'baz']}] + databases_config = [{'name': 'foo', 'schemas': ['bar', 'baz']}] flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'}) flexmock(module).should_receive('make_dump_path') @@ -1135,9 +1121,10 @@ def test_restore_database_dump_with_schemas_restores_schemas(): ).once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='foo', dry_run=False, extract_process=None, connection_params={ diff --git a/tests/unit/hooks/test_sqlite.py b/tests/unit/hooks/test_sqlite.py index f61ffc75..761a0557 100644 --- a/tests/unit/hooks/test_sqlite.py +++ b/tests/unit/hooks/test_sqlite.py @@ -93,7 +93,7 @@ def test_dump_databases_does_not_dump_if_dry_run(): def test_restore_database_dump_restores_database(): - database_config = [{'path': '/path/to/database', 'name': 'database'}] + databases_config = [{'path': '/path/to/database', 'name': 'database'}, {'name': 'other'}] extract_process = flexmock(stdout=flexmock()) flexmock(module).should_receive('execute_command_with_processes').with_args( @@ -109,9 +109,10 @@ def test_restore_database_dump_restores_database(): flexmock(module.os).should_receive('remove').once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='database', dry_run=False, extract_process=extract_process, connection_params={'restore_path': None}, @@ -119,7 +120,7 @@ def test_restore_database_dump_restores_database(): def test_restore_database_dump_with_connection_params_uses_connection_params_for_restore(): - database_config = [ + databases_config = [ {'path': '/path/to/database', 'name': 'database', 'restore_path': 'config/path/to/database'} ] extract_process = flexmock(stdout=flexmock()) @@ -137,9 +138,10 @@ def test_restore_database_dump_with_connection_params_uses_connection_params_for flexmock(module.os).should_receive('remove').once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='database', dry_run=False, extract_process=extract_process, connection_params={'restore_path': 'cli/path/to/database'}, @@ -147,7 +149,7 @@ def test_restore_database_dump_with_connection_params_uses_connection_params_for def test_restore_database_dump_without_connection_params_uses_restore_params_in_config_for_restore(): - database_config = [ + databases_config = [ {'path': '/path/to/database', 'name': 'database', 'restore_path': 'config/path/to/database'} ] extract_process = flexmock(stdout=flexmock()) @@ -165,9 +167,10 @@ def test_restore_database_dump_without_connection_params_uses_restore_params_in_ flexmock(module.os).should_receive('remove').once() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='database', dry_run=False, extract_process=extract_process, connection_params={'restore_path': None}, @@ -175,31 +178,33 @@ def test_restore_database_dump_without_connection_params_uses_restore_params_in_ def test_restore_database_dump_does_not_restore_database_if_dry_run(): - database_config = [{'path': '/path/to/database', 'name': 'database'}] + databases_config = [{'path': '/path/to/database', 'name': 'database'}] extract_process = flexmock(stdout=flexmock()) flexmock(module).should_receive('execute_command_with_processes').never() flexmock(module.os).should_receive('remove').never() module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='database', dry_run=True, extract_process=extract_process, connection_params={'restore_path': None}, ) -def test_restore_database_dump_raises_error_if_database_config_is_invalid(): - database_config = [] +def test_restore_database_dump_raises_error_if_database_config_is_empty(): + databases_config = [] extract_process = flexmock(stdout=flexmock()) with pytest.raises(ValueError): module.restore_database_dump( - database_config, + databases_config, {}, 'test.yaml', + database_name='database', dry_run=False, extract_process=extract_process, connection_params={'restore_path': None},