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
|
5 |
6 |
from django.db.migrations.recorder import MigrationRecorder
|
6 |
7 |
from django.db import connection
|
... | ... | |
45 |
46 |
else:
|
46 |
47 |
self.run_migrations(self.schema_name, settings.TENANT_APPS)
|
47 |
48 |
else:
|
|
49 |
app_names = []
|
|
50 |
for app in apps.get_app_configs():
|
|
51 |
if app.name in settings.TENANT_APPS:
|
|
52 |
app_names.append(app.name)
|
|
53 |
applied_public_migrations = self.get_applied_migrations(app_names)
|
48 |
54 |
for tenant in TenantMiddleware.get_tenants():
|
|
55 |
connection.set_schema(tenant.schema_name, include_public=False)
|
|
56 |
applied_migrations = self.get_applied_migrations(app_names)
|
|
57 |
if all([x in applied_migrations for x in applied_public_migrations]):
|
|
58 |
if int(self.options.get('verbosity', 1)) >= 1:
|
|
59 |
self._notice("=== Skipping migrations of schema %s" % tenant.schema_name)
|
|
60 |
continue
|
49 |
61 |
self.run_migrations(tenant.schema_name, settings.TENANT_APPS)
|
50 |
62 |
|
|
63 |
def get_applied_migrations(self, app_names):
|
|
64 |
applied_migrations = []
|
|
65 |
with connection.cursor() as cursor:
|
|
66 |
cursor.execute('SELECT app, name FROM django_migrations')
|
|
67 |
for row in cursor.fetchall():
|
|
68 |
applied_migrations.append(row)
|
|
69 |
applied_migrations = [x for x in applied_migrations if x[0] in app_names]
|
|
70 |
return applied_migrations
|
|
71 |
|
51 |
72 |
def run_migrations(self, schema_name, included_apps):
|
52 |
73 |
if int(self.options.get('verbosity', 1)) >= 1:
|
53 |
74 |
self._notice("=== Running migrate for schema %s" % schema_name)
|
54 |
|
-
|