1
|
# this file derive from django-tenant-schemas
|
2
|
# Author: Bernardo Pires Carneiro
|
3
|
# Email: carneiro.be@gmail.com
|
4
|
# License: MIT license
|
5
|
# Home-page: http://github.com/bcarneiro/django-tenant-schemas
|
6
|
from django.conf import settings
|
7
|
from django.db import connection
|
8
|
from south import migration
|
9
|
from south.migration.base import Migrations
|
10
|
from south.management.commands.migrate import Command as MigrateCommand
|
11
|
from entrouvert.djommon.multitenant.middleware import TenantMiddleware
|
12
|
from entrouvert.djommon.multitenant.management.commands import SyncCommon
|
13
|
|
14
|
|
15
|
class Command(SyncCommon):
|
16
|
help = "Migrate schemas with South"
|
17
|
option_list = MigrateCommand.option_list + SyncCommon.option_list
|
18
|
|
19
|
def handle(self, *args, **options):
|
20
|
super(Command, self).handle(*args, **options)
|
21
|
|
22
|
if self.sync_public:
|
23
|
self.migrate_public_apps()
|
24
|
if self.sync_tenant:
|
25
|
self.migrate_tenant_apps(self.schema_name)
|
26
|
|
27
|
def _set_managed_apps(self, included_apps, excluded_apps):
|
28
|
""" while sync_schemas works by setting which apps are managed, on south we set which apps should be ignored """
|
29
|
ignored_apps = []
|
30
|
if excluded_apps:
|
31
|
for item in excluded_apps:
|
32
|
if item not in included_apps:
|
33
|
ignored_apps.append(item)
|
34
|
|
35
|
for app in ignored_apps:
|
36
|
app_label = app.split('.')[-1]
|
37
|
settings.SOUTH_MIGRATION_MODULES[app_label] = 'ignore'
|
38
|
|
39
|
def _save_south_settings(self):
|
40
|
self._old_south_modules = None
|
41
|
if hasattr(settings, "SOUTH_MIGRATION_MODULES") and settings.SOUTH_MIGRATION_MODULES is not None:
|
42
|
self._old_south_modules = settings.SOUTH_MIGRATION_MODULES.copy()
|
43
|
else:
|
44
|
settings.SOUTH_MIGRATION_MODULES = dict()
|
45
|
|
46
|
def _restore_south_settings(self):
|
47
|
settings.SOUTH_MIGRATION_MODULES = self._old_south_modules
|
48
|
|
49
|
def _clear_south_cache(self):
|
50
|
for mig in list(migration.all_migrations()):
|
51
|
delattr(mig._application, "migrations")
|
52
|
Migrations._clear_cache()
|
53
|
|
54
|
def _migrate_schema(self, tenant):
|
55
|
connection.set_tenant(tenant, include_public=False)
|
56
|
MigrateCommand().execute(*self.args, **self.options)
|
57
|
|
58
|
def migrate_tenant_apps(self, schema_name=None):
|
59
|
self._save_south_settings()
|
60
|
|
61
|
apps = self.tenant_apps or self.installed_apps
|
62
|
self._set_managed_apps(included_apps=apps, excluded_apps=self.shared_apps)
|
63
|
|
64
|
if schema_name:
|
65
|
self._notice("=== Running migrate for schema: %s" % schema_name)
|
66
|
connection.set_schema_to_public()
|
67
|
tenant = TenantMiddleware.get_tenant_by_hostname(schema_name)
|
68
|
self._migrate_schema(tenant)
|
69
|
else:
|
70
|
all_tenants = TenantMiddleware.get_tenants()
|
71
|
if not all_tenants:
|
72
|
self._notice("No tenants found")
|
73
|
|
74
|
for tenant in all_tenants:
|
75
|
Migrations._dependencies_done = False # very important, the dependencies need to be purged from cache
|
76
|
self._notice("=== Running migrate for schema %s" % tenant.schema_name)
|
77
|
self._migrate_schema(tenant)
|
78
|
|
79
|
self._restore_south_settings()
|
80
|
|
81
|
def migrate_public_apps(self):
|
82
|
self._save_south_settings()
|
83
|
|
84
|
apps = self.shared_apps or self.installed_apps
|
85
|
self._set_managed_apps(included_apps=apps, excluded_apps=self.tenant_apps)
|
86
|
|
87
|
self._notice("=== Running migrate for schema public")
|
88
|
MigrateCommand().execute(*self.args, **self.options)
|
89
|
|
90
|
self._clear_south_cache()
|
91
|
self._restore_south_settings()
|