Projet

Général

Profil

« Précédent | Suivant » 

Révision cd1fa561

Ajouté par Thomas Noël il y a plus de 9 ans

add safemigrate_schemas command (#5791)

Voir les différences:

entrouvert/djommon/multitenant/management/commands/safemigrate_schemas.py
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 entrouvert.djommon.multitenant.middleware import TenantMiddleware
11
from entrouvert.djommon.multitenant.management.commands import SyncCommon
12
from entrouvert.djommon.management.commands.safemigrate import Command as SafeMigrateCommand
13
from entrouvert.djommon.multitenant.management.commands.sync_schemas import Command as MTSyncCommand
14
from entrouvert.djommon.multitenant.management.commands.migrate_schemas import Command as MTMigrateCommand
15

  
16

  
17
class Command(SyncCommon):
18
    help = "Safely migrate schemas with South"
19
    option_list = MTMigrateCommand.option_list
20

  
21
    def handle(self, *args, **options):
22
        super(Command, self).handle(*args, **options)
23

  
24
        MTSyncCommand().execute(*args, **options)
25
        connection.set_schema_to_public()
26
        if self.sync_public:
27
            self.fake_public_apps()
28
        if self.sync_tenant:
29
            self.fake_tenant_apps(self.domain)
30
        connection.set_schema_to_public()
31
        MTMigrateCommand().execute(*args, **options)
32

  
33
    def _set_managed_apps(self, included_apps, excluded_apps):
34
        """ while sync_schemas works by setting which apps are managed, on south we set which apps should be ignored """
35
        ignored_apps = []
36
        if excluded_apps:
37
            for item in excluded_apps:
38
                if item not in included_apps:
39
                    ignored_apps.append(item)
40

  
41
        for app in ignored_apps:
42
            app_label = app.split('.')[-1]
43
            settings.SOUTH_MIGRATION_MODULES[app_label] = 'ignore'
44

  
45
    def _save_south_settings(self):
46
        self._old_south_modules = None
47
        if hasattr(settings, "SOUTH_MIGRATION_MODULES") and settings.SOUTH_MIGRATION_MODULES is not None:
48
            self._old_south_modules = settings.SOUTH_MIGRATION_MODULES.copy()
49
        else:
50
            settings.SOUTH_MIGRATION_MODULES = dict()
51

  
52
    def _restore_south_settings(self):
53
        settings.SOUTH_MIGRATION_MODULES = self._old_south_modules
54

  
55
    def _clear_south_cache(self):
56
        for mig in list(migration.all_migrations()):
57
            delattr(mig._application, "migrations")
58
        Migrations._clear_cache()
59

  
60
    def _fake_schema(self, tenant):
61
        connection.set_tenant(tenant, include_public=False)
62
        SafeMigrateCommand().fake_if_needed()
63

  
64
    def fake_tenant_apps(self, schema_name=None):
65
        self._save_south_settings()
66

  
67
        apps = self.tenant_apps or self.installed_apps
68
        self._set_managed_apps(included_apps=apps, excluded_apps=self.shared_apps)
69

  
70
        if schema_name:
71
            self._notice("=== Running fake_if_needed for schema: %s" % schema_name)
72
            connection.set_schema_to_public()
73
            tenant = TenantMiddleware.get_tenant_by_hostname(schema_name)
74
            self._fake_schema(tenant)
75
        else:
76
            all_tenants = TenantMiddleware.get_tenants()
77
            if not all_tenants:
78
                self._notice("No tenants found")
79

  
80
            for tenant in all_tenants:
81
                Migrations._dependencies_done = False  # very important, the dependencies need to be purged from cache
82
                self._notice("=== Running fake_if_needed for schema %s" % tenant.schema_name)
83
                self._fake_schema(tenant)
84

  
85
        self._restore_south_settings()
86

  
87
    def fake_public_apps(self):
88
        self._save_south_settings()
89

  
90
        apps = self.shared_apps or self.installed_apps
91
        self._set_managed_apps(included_apps=apps, excluded_apps=self.tenant_apps)
92

  
93
        self._notice("=== Running fake_if_needed for schema public")
94
        SafeMigrateCommand().fake_if_needed()
95

  
96
        self._clear_south_cache()
97
        self._restore_south_settings()

Formats disponibles : Unified diff