Exemplo n.º 1
0
def run_migrations(args, options, executor_codename, schema_name, allow_atomic=True, idx=None, count=None):
    from django.core.management import color
    from django.core.management.base import OutputWrapper
    from django.db import connections

    style = color.color_style()

    def style_func(msg):
        percent_str = ''
        if idx is not None and count is not None and count > 0:
            percent_str = '%d/%d (%s%%) ' % (idx + 1, count, int(100 * (idx + 1) / count))
        return '[%s%s:%s] %s' % (
            percent_str,
            style.NOTICE(executor_codename),
            style.NOTICE(schema_name),
            msg
        )

    connection = connections[get_tenant_database_alias()]
    connection.set_schema(schema_name)

    stdout = OutputWrapper(sys.stdout)
    stdout.style_func = style_func
    stderr = OutputWrapper(sys.stderr)
    stderr.style_func = style_func
    if int(options.get('verbosity', 1)) >= 1:
        stdout.write(style.NOTICE("=== Starting migration"))
    MigrateCommand(stdout=stdout, stderr=stderr).execute(*args, **options)

    try:
        transaction.commit()
        connection.close()
        connection.connection = None
    except transaction.TransactionManagementError:
        if not allow_atomic:
            raise

        # We are in atomic transaction, don't close connections
        pass

    connection.set_schema_to_public()
Exemplo n.º 2
0
    def handle(self, *args, **kwargs):
        migrate = MigrateCommand()

        options = {
            'app_label': None,
            'migration_name': None,
            'interactive': True,
            'fake': False,
            'fake_initial': False,
            'run_syncdb': False,
            'plan': False,
        }
        options.update(kwargs)

        for database in settings.DATABASES:
            options['database'] = database
            if options['verbosity'] >= 1:
                self.stdout.write(self.style.MIGRATE_HEADING('Database:'))
                self.stdout.write('  ' + database)
            migrate.handle(*args, **options)
            if options['verbosity'] >= 1:
                self.stdout.write('')
Exemplo n.º 3
0
def run_migrations(args,
                   options,
                   executor_codename,
                   schema_name,
                   allow_atomic=True):
    from django.core.management import color
    from django.core.management.base import OutputWrapper
    from django.db import connection

    style = color.color_style()

    def style_func(msg):
        return '[%s:%s] %s' % (style.NOTICE(executor_codename),
                               style.NOTICE(schema_name), msg)

    stdout = OutputWrapper(sys.stdout)
    stdout.style_func = style_func
    stderr = OutputWrapper(sys.stderr)
    stderr.style_func = style_func
    if int(options.get('verbosity', 1)) >= 1:
        stdout.write(
            style.NOTICE("=== Running migrate for schema %s" % schema_name))

    connection.set_schema(schema_name)
    MigrateCommand(stdout=stdout, stderr=stderr).execute(*args, **options)

    try:
        transaction.commit()
        connection.close()
        connection.connection = None
    except transaction.TransactionManagementError:
        if not allow_atomic:
            raise

        # We are in atomic transaction, don't close connections
        pass

    connection.set_schema_to_public()
Exemplo n.º 4
0
    def run_migrations(self, schema_name, included_apps):
        self._notice("=== Running migrate for schema %s" % schema_name)
        connection.set_schema(schema_name, include_public=False)
        apps.app_configs = OrderedDict()
        apps.clear_cache()
        apps.set_installed_apps(included_apps)

        command = MigrateCommand()

        defaults = {}
        for opt in MigrateCommand.option_list:
            if opt.dest in self.options:
                defaults[opt.dest] = self.options[opt.dest]
            elif opt.default is NO_DEFAULT:
                defaults[opt.dest] = None
            else:
                defaults[opt.dest] = opt.default

        command.execute(*self.args, **defaults)

        connection.set_schema('public', include_public=True)
        apps.app_configs = OrderedDict()
        apps.clear_cache()
        apps.set_installed_apps(settings.SHARED_APPS)
 def add_arguments(self, parser):
     super(MigrateSchemasCommand, self).add_arguments(parser)
     command = MigrateCommand()
     command.add_arguments(parser)
Exemplo n.º 6
0
def migrate(params=None):
    command = MigrateCommand()
    parser = command.create_parser('manage.py', 'migrate')
    options = parser.parse_args(params or [])
    cmd_options = vars(options)
    return command.execute(**cmd_options)