21 |
21 |
from django.db import connection
|
22 |
22 |
from django.db.migrations.loader import MigrationLoader
|
23 |
23 |
from django.db.migrations.recorder import MigrationRecorder
|
|
24 |
from django.utils.timezone import localtime
|
24 |
25 |
from tenant_schemas.postgresql_backend.base import FakeTenant
|
25 |
26 |
from tenant_schemas.utils import get_public_schema_name, schema_exists
|
26 |
27 |
|
... | ... | |
59 |
60 |
all_migrations = {
|
60 |
61 |
(app, migration) for app, migration in loader.disk_migrations if app in app_labels
|
61 |
62 |
}
|
62 |
|
for tenant in TenantMiddleware.get_tenants():
|
|
63 |
tenants = list(TenantMiddleware.get_tenants())
|
|
64 |
len_tenants = len(tenants)
|
|
65 |
start_datetime = localtime()
|
|
66 |
for step, tenant in enumerate(tenants, start=1):
|
63 |
67 |
connection.set_tenant(tenant, include_public=False)
|
64 |
68 |
applied_migrations = self.get_applied_migrations(app_labels)
|
65 |
69 |
if options.get('fake') or options.get('migration_name') or options.get('app_label'):
|
... | ... | |
68 |
72 |
applied_migrations = []
|
69 |
73 |
if all([x in applied_migrations for x in all_migrations]):
|
70 |
74 |
if int(self.options.get('verbosity', 1)) >= 1:
|
71 |
|
self._notice("=== Skipping migrations of tenant %s" % tenant.domain_url)
|
|
75 |
self._notice(
|
|
76 |
"=== Skipping migrations of tenant %s (%s/%s)"
|
|
77 |
% (tenant.domain_url, step, len_tenants)
|
|
78 |
)
|
72 |
79 |
continue
|
73 |
|
self.run_migrations(tenant, settings.TENANT_APPS)
|
|
80 |
self.run_migrations(tenant, settings.TENANT_APPS, step, len_tenants)
|
|
81 |
if int(self.options.get('verbosity', 1)) >= 1:
|
|
82 |
eta = start_datetime + len_tenants * (localtime() - start_datetime) / step
|
|
83 |
self._notice('=== migrate_schemas ETA: %s' % eta, flush=True)
|
74 |
84 |
|
75 |
85 |
def get_applied_migrations(self, app_labels):
|
76 |
86 |
applied_migrations = []
|
... | ... | |
81 |
91 |
applied_migrations = [x for x in applied_migrations if x[0] in app_labels]
|
82 |
92 |
return applied_migrations
|
83 |
93 |
|
84 |
|
def run_migrations(self, tenant, included_apps):
|
|
94 |
def run_migrations(self, tenant, included_apps, step=1, steps=1):
|
85 |
95 |
if int(self.options.get('verbosity', 1)) >= 1:
|
86 |
|
self._notice("=== Running migrate for tenant %s" % tenant.domain_url)
|
|
96 |
self._notice("=== Running migrate for tenant %s (%s/%s)" % (tenant.domain_url, step, steps))
|
87 |
97 |
connection.set_tenant(tenant, include_public=False)
|
88 |
98 |
command = MigrateCommand()
|
89 |
99 |
command.requires_system_checks = False
|
... | ... | |
101 |
111 |
command.execute(*self.args, **self.options)
|
102 |
112 |
connection.set_schema_to_public()
|
103 |
113 |
|
104 |
|
def _notice(self, output):
|
|
114 |
def _notice(self, output, flush=False):
|
105 |
115 |
self.stdout.write(self.style.NOTICE(output))
|
|
116 |
if flush:
|
|
117 |
self.stdout.flush()
|
106 |
118 |
|
107 |
119 |
|
108 |
120 |
Command = MigrateSchemasCommand
|