1 |
1 |
import django
|
2 |
2 |
from optparse import NO_DEFAULT
|
3 |
3 |
|
|
4 |
from django.apps import apps
|
4 |
5 |
from django.core.management.commands.migrate import Command as MigrateCommand
|
|
6 |
from django.db.migrations.loader import MigrationLoader
|
5 |
7 |
from django.db.migrations.recorder import MigrationRecorder
|
6 |
8 |
from django.db import connection
|
7 |
9 |
from django.conf import settings
|
... | ... | |
45 |
47 |
else:
|
46 |
48 |
self.run_migrations(self.schema_name, settings.TENANT_APPS)
|
47 |
49 |
else:
|
|
50 |
app_labels = []
|
|
51 |
for app in apps.get_app_configs():
|
|
52 |
if app.name in settings.TENANT_APPS:
|
|
53 |
app_labels.append(app.label)
|
|
54 |
loader = MigrationLoader(connection)
|
|
55 |
loader.load_disk()
|
|
56 |
recorder = MigrationRecorder(connection)
|
|
57 |
applied_public_migrations = set(
|
|
58 |
[(app, migration) for app, migration
|
|
59 |
in recorder.applied_migrations()
|
|
60 |
if app in app_labels and
|
|
61 |
(app, migration) in loader.disk_migrations])
|
48 |
62 |
for tenant in TenantMiddleware.get_tenants():
|
|
63 |
connection.set_schema(tenant.schema_name, include_public=False)
|
|
64 |
applied_migrations = self.get_applied_migrations(app_labels)
|
|
65 |
if options.get('fake'):
|
|
66 |
# disregard applied migrations if --fake is given.
|
|
67 |
applied_migrations = []
|
|
68 |
if all([x in applied_migrations for x in applied_public_migrations]):
|
|
69 |
if int(self.options.get('verbosity', 1)) >= 1:
|
|
70 |
self._notice("=== Skipping migrations of schema %s" % tenant.schema_name)
|
|
71 |
continue
|
49 |
72 |
self.run_migrations(tenant.schema_name, settings.TENANT_APPS)
|
50 |
73 |
|
|
74 |
def get_applied_migrations(self, app_labels):
|
|
75 |
applied_migrations = []
|
|
76 |
with connection.cursor() as cursor:
|
|
77 |
cursor.execute('SELECT app, name FROM django_migrations')
|
|
78 |
for row in cursor.fetchall():
|
|
79 |
applied_migrations.append(row)
|
|
80 |
applied_migrations = [x for x in applied_migrations if x[0] in app_labels]
|
|
81 |
return applied_migrations
|
|
82 |
|
51 |
83 |
def run_migrations(self, schema_name, included_apps):
|
52 |
84 |
if int(self.options.get('verbosity', 1)) >= 1:
|
53 |
85 |
self._notice("=== Running migrate for schema %s" % schema_name)
|
54 |
|
-
|