From 3ae1be021f44b3c871307037f5d08e1d45b74ad8 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Wed, 3 Jul 2019 09:28:57 +0200 Subject: [PATCH 2/2] do not use public schema for finding list of migrations (#31042) --- .../management/commands/migrate_schemas.py | 10 +++------ tests_multitenant/test_create_tenant.py | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/hobo/multitenant/management/commands/migrate_schemas.py b/hobo/multitenant/management/commands/migrate_schemas.py index 579c1d8..87e28b5 100644 --- a/hobo/multitenant/management/commands/migrate_schemas.py +++ b/hobo/multitenant/management/commands/migrate_schemas.py @@ -58,13 +58,9 @@ class MigrateSchemasCommand(SyncCommon): for app in apps.get_app_configs(): if app.name in settings.TENANT_APPS: app_labels.append(app.label) - loader = MigrationLoader(connection) + loader = MigrationLoader(None) loader.load_disk() - recorder = MigrationRecorder(connection) - applied_public_migrations = set( - [(app, migration) - for app, migration in recorder.applied_migrations() - if app in app_labels and (app, migration) in loader.disk_migrations]) + all_migrations = set([(app, migration) for app, migration in loader.disk_migrations if app in app_labels]) for tenant in TenantMiddleware.get_tenants(): connection.set_schema(tenant.schema_name, include_public=False) applied_migrations = self.get_applied_migrations(app_labels) @@ -72,7 +68,7 @@ class MigrateSchemasCommand(SyncCommon): # never skip migrations if explicit migration actions # are given. applied_migrations = [] - if all([x in applied_migrations for x in applied_public_migrations]): + if all([x in applied_migrations for x in all_migrations]): if int(self.options.get('verbosity', 1)) >= 1: self._notice("=== Skipping migrations of schema %s" % tenant.schema_name) continue diff --git a/tests_multitenant/test_create_tenant.py b/tests_multitenant/test_create_tenant.py index b7aafcf..a566ee3 100644 --- a/tests_multitenant/test_create_tenant.py +++ b/tests_multitenant/test_create_tenant.py @@ -63,3 +63,25 @@ def test_create_tenant_failure(db, caplog): with connection.cursor() as cursor: cursor.execute('select schema_name from information_schema.schemata') assert 'www_example_com' not in [row[0] for row in cursor.fetchall()] + + +def test_migrate_schemas_skip_applied(db, capsys): + assert not schema_exists('www_example_com') + call_command('create_tenant', 'www.example.com') + captured = capsys.readouterr() + assert 'Running migrate for schema www_example_com' in captured.out + call_command('migrate_schemas', verbosity=1) + captured = capsys.readouterr() + assert 'Skipping migrations of schema www_example_com' in captured.out + call_command('migrate_schemas', 'common', '0001_initial', verbosity=1) + captured = capsys.readouterr() + assert 'Running migrate for schema www_example_com' in captured.out + assert 'Unapplying common.0002' in captured.out + call_command('migrate_schemas', verbosity=1) + captured = capsys.readouterr() + assert 'Running migrate for schema www_example_com' in captured.out + assert 'Applying common.0002' in captured.out + call_command('migrate_schemas', verbosity=1) + captured = capsys.readouterr() + assert 'Skipping migrations of schema www_example_com' in captured.out + -- 2.20.1