Projet

Général

Profil

« Précédent | Suivant » 

Révision 1f642209

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

migrate_schemas: use django 1.7 if available (#6388)

Voir les différences:

entrouvert/djommon/multitenant/management/commands/migrate_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
1
import django
2
from optparse import NO_DEFAULT
3

  
4
if django.VERSION >= (1, 7, 0):
5
    from django.core.management.commands.migrate import Command as MigrateCommand
6
    from django.db.migrations.recorder import MigrationRecorder
7 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
8
from django.conf import settings
9

  
10
from tenant_schemas.utils import get_public_schema_name
11
from entrouvert.djommon.multitenant.middleware import TenantMiddleware, TenantNotFound
12 12
from entrouvert.djommon.multitenant.management.commands import SyncCommon
13 13

  
14 14

  
15
class Command(SyncCommon):
16
    help = "Migrate schemas with South"
17
    option_list = MigrateCommand.option_list + SyncCommon.option_list
15
class MigrateSchemasCommand(SyncCommon):
16
    help = "Updates database schema. Manages both apps with migrations and those without."
17

  
18
    def run_from_argv(self, argv):
19
        """
20
        Changes the option_list to use the options from the wrapped command.
21
        Adds schema parameter to specify which schema will be used when
22
        executing the wrapped command.
23
        """
24
        self.option_list += MigrateCommand.option_list
25
        super(MigrateSchemasCommand, self).run_from_argv(argv)
18 26

  
19 27
    def handle(self, *args, **options):
20
        super(Command, self).handle(*args, **options)
28
        super(MigrateSchemasCommand, self).handle(*args, **options)
29
        self.PUBLIC_SCHEMA_NAME = get_public_schema_name()
30

  
31
        if self.sync_public and not self.domain:
32
            self.domain = self.PUBLIC_SCHEMA_NAME
21 33

  
22 34
        if self.sync_public:
23
            self.migrate_public_apps()
35
            self.run_migrations(self.domain, settings.SHARED_APPS)
24 36
        if self.sync_tenant:
25
            self.migrate_tenant_apps(self.domain)
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()
37
            if self.domain and self.domain != self.PUBLIC_SCHEMA_NAME:
38
                try:
39
                    tenant = TenantMiddleware.get_tenant_by_hostname(self.domain)
40
                except TenantNotFound:
41
                    raise RuntimeError('Schema "{}" does not exist'.format(
42
                        self.domain))
43
                else:
44
                    self.run_migrations(tenant.schema_name, settings.TENANT_APPS)
45
            else:
46
                all_tenants = TenantMiddleware.get_tenants()
47
                for tenant in all_tenants:
48
                    self.run_migrations(tenant.schema_name, settings.TENANT_APPS)
49

  
50
    def run_migrations(self, schema_name, included_apps):
51
        self._notice("=== Running migrate for schema %s" % schema_name)
52
        connection.set_schema(schema_name)
53
        command = MigrateCommand()
54

  
55
        defaults = {}
56
        for opt in MigrateCommand.option_list:
57
            if opt.dest in self.options:
58
                defaults[opt.dest] = self.options[opt.dest]
59
            elif opt.default is NO_DEFAULT:
60
                defaults[opt.dest] = None
61
            else:
62
                defaults[opt.dest] = opt.default
63

  
64
        command.execute(*self.args, **defaults)
65
        connection.set_schema_to_public()
66

  
67
    def _notice(self, output):
68
        self.stdout.write(self.style.NOTICE(output))
69

  
70

  
71
if django.VERSION >= (1, 7, 0):
72
    Command = MigrateSchemasCommand
73
else:
74
    from .legacy.migrate_schemas import Command

Formats disponibles : Unified diff