do not use public schema for finding list of migrations (#31042)

This commit is contained in:
Benjamin Dauvergne 2019-07-03 09:28:57 +02:00
parent d4790d882b
commit 02b1ef8c2e
2 changed files with 24 additions and 7 deletions

View File

@ -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

View File

@ -63,3 +63,24 @@ 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