Projet

Général

Profil

0002-do-not-use-public-schema-for-finding-list-of-migrati.patch

Benjamin Dauvergne, 03 juillet 2019 09:45

Télécharger (3,71 ko)

Voir les différences:

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(-)
hobo/multitenant/management/commands/migrate_schemas.py
58 58
            for app in apps.get_app_configs():
59 59
                if app.name in settings.TENANT_APPS:
60 60
                    app_labels.append(app.label)
61
            loader = MigrationLoader(connection)
61
            loader = MigrationLoader(None)
62 62
            loader.load_disk()
63
            recorder = MigrationRecorder(connection)
64
            applied_public_migrations = set(
65
                [(app, migration)
66
                 for app, migration in recorder.applied_migrations()
67
                 if app in app_labels and (app, migration) in loader.disk_migrations])
63
            all_migrations = set([(app, migration) for app, migration in loader.disk_migrations if app in app_labels])
68 64
            for tenant in TenantMiddleware.get_tenants():
69 65
                connection.set_schema(tenant.schema_name, include_public=False)
70 66
                applied_migrations = self.get_applied_migrations(app_labels)
......
72 68
                    # never skip migrations if explicit migration actions
73 69
                    # are given.
74 70
                    applied_migrations = []
75
                if all([x in applied_migrations for x in applied_public_migrations]):
71
                if all([x in applied_migrations for x in all_migrations]):
76 72
                    if int(self.options.get('verbosity', 1)) >= 1:
77 73
                        self._notice("=== Skipping migrations of schema %s" % tenant.schema_name)
78 74
                    continue
tests_multitenant/test_create_tenant.py
63 63
    with connection.cursor() as cursor:
64 64
        cursor.execute('select schema_name from information_schema.schemata')
65 65
        assert 'www_example_com' not in [row[0] for row in cursor.fetchall()]
66

  
67

  
68
def test_migrate_schemas_skip_applied(db, capsys):
69
    assert not schema_exists('www_example_com')
70
    call_command('create_tenant', 'www.example.com')
71
    captured = capsys.readouterr()
72
    assert 'Running migrate for schema www_example_com' in captured.out
73
    call_command('migrate_schemas', verbosity=1)
74
    captured = capsys.readouterr()
75
    assert 'Skipping migrations of schema www_example_com' in captured.out
76
    call_command('migrate_schemas', 'common', '0001_initial', verbosity=1)
77
    captured = capsys.readouterr()
78
    assert 'Running migrate for schema www_example_com' in captured.out
79
    assert 'Unapplying common.0002' in captured.out
80
    call_command('migrate_schemas', verbosity=1)
81
    captured = capsys.readouterr()
82
    assert 'Running migrate for schema www_example_com' in captured.out
83
    assert 'Applying common.0002' in captured.out
84
    call_command('migrate_schemas', verbosity=1)
85
    captured = capsys.readouterr()
86
    assert 'Skipping migrations of schema www_example_com' in captured.out
87

  
66
-