def migrate_tenant_apps(self, schema_name=None): self._save_south_settings() apps = self.tenant_apps or self.installed_apps self._set_managed_apps(included_apps=apps, excluded_apps=self.shared_apps) syncdb_command = MigrateCommand() if schema_name: print self.style.NOTICE("=== Running migrate for schema: %s" % schema_name) connection.set_schema_to_public() sync_tenant = get_tenant_model().objects.filter(schema_name=schema_name).get() connection.set_tenant(sync_tenant, include_public=False) syncdb_command.execute(**self.options) else: public_schema_name = get_public_schema_name() tenant_schemas_count = get_tenant_model().objects.exclude(schema_name=public_schema_name).count() if not tenant_schemas_count: print self.style.NOTICE("No tenants found") for tenant_schema in get_tenant_model().objects.exclude(schema_name=public_schema_name).all(): Migrations._dependencies_done = False # very important, the dependencies need to be purged from cache print self.style.NOTICE("=== Running migrate for schema %s" % tenant_schema.schema_name) connection.set_tenant(tenant_schema, include_public=False) syncdb_command.execute(**self.options) self._restore_south_settings()
def setUp(self): if 'south' in settings.INSTALLED_APPS: from south.management.commands.migrate import Command command = Command() command.handle(all_apps=True) self.templates = [ ("{% node_url %}", "/root/second/"), ("{% node_url for node2 %}", "/root/second2/"), ("{% node_url as hello %}<p>{{ hello|slice:'1:' }}</p>", "<p>root/second/</p>"), ("{% node_url for nodes|first %}", "/root/"), ("{% node_url with entry %}", settings.TEMPLATE_STRING_IF_INVALID), ("{% node_url with entry for node2 %}", "/root/second2/2010/10/20/first-entry"), ("{% node_url with tag for node2 %}", "/root/second2/tags/test-tag/"), ("{% node_url with date for node2 %}", "/root/second2/2010/10/20"), ("{% node_url entries_by_day year=date|date:'Y' month=date|date:'m' day=date|date:'d' for node2 as goodbye %}<em>{{ goodbye|upper }}</em>", "<em>/ROOT/SECOND2/2010/10/20</em>"), ("{% node_url entries_by_month year=date|date:'Y' month=date|date:'m' for node2 %}", "/root/second2/2010/10"), ("{% node_url entries_by_year year=date|date:'Y' for node2 %}", "/root/second2/2010/"), ] nodes = Node.objects.all() blog = Blog.objects.all()[0] self.context = template.Context({ 'node': nodes.get(slug='second'), 'node2': nodes.get(slug='second2'), 'nodes': nodes, 'entry': BlogEntry.objects.all()[0], 'tag': blog.entry_tags.all()[0], 'date': blog.entry_dates['day'][0] })
def migrate_tenant_apps(self, schema_name=None): self._save_south_settings() apps = self.tenant_apps or self.installed_apps self._set_managed_apps(included_apps=apps, excluded_apps=self.shared_apps) migrate_command = MigrateCommand() if schema_name: print self.style.NOTICE("=== Running migrate for schema: %s" % schema_name) connection.set_schema_to_public() sync_tenant = get_tenant_model().objects.filter( schema_name=schema_name).get() connection.set_tenant(sync_tenant, include_public=False) migrate_command.execute(**self.options) else: public_schema_name = get_public_schema_name() tenant_schemas_count = get_tenant_model().objects.exclude( schema_name=public_schema_name).count() if not tenant_schemas_count: print self.style.NOTICE("No tenants found") for tenant_schema in get_tenant_model().objects.exclude( schema_name=public_schema_name).all(): Migrations._dependencies_done = False # very important, the dependencies need to be purged from cache print self.style.NOTICE("=== Running migrate for schema %s" % tenant_schema.schema_name) connection.set_tenant(tenant_schema, include_public=False) migrate_command.execute(**self.options) self._restore_south_settings()
def handle_noargs(self, **options): key = options.pop('key', '') idle = options.pop('idle', False) syncdb_opts = deepcopy(options) syncdb_opts['migrate_all'] = False syncdb_opts['interactive'] = False syncdb_opts['migrate'] = False syncdb_opts['database'] = 'default' migrate_opts = deepcopy(options) migrate_opts['fake'] = False migrate_opts['interactive'] = False try: cachetable = CreateCacheTable() cachetable.stdout = self.stdout cachetable.stderr = self.stderr cachetable.handle('django_dbcache', database='default') self.stdout.write('created cache table "django_dbcache"') except CommandError: self.stdout.write('not created cache table "django_dbcache". already exists.') self.stdout.write("Detecting database status\n") try: with commit_on_success(): MigrationHistory.objects.count() except DatabaseError: self.stdout.write("No database yet, but NOT running full syncdb anyway (because that causes problems with django-cms 3 cms plugin table renames).\n") if False: self.stdout.write("No database yet, running full syncdb\n") syncdb_opts['migrate_all'] = True migrate_opts['fake'] = True syncdb = SyncDB() syncdb.stdout = self.stdout syncdb.stderr = self.stderr syncdb.handle_noargs(**syncdb_opts) migrate = Migrate() migrate.stdout = self.stdout migrate.stderr = self.stderr migrate.handle(**migrate_opts) datayaml = os.path.join(settings.PROJECT_DIR, 'data.yaml') if os.path.exists(datayaml): self.stdout.write("Found data.yaml, trying to load.\n") activate(settings.CMS_LANGUAGES[0][0]) os.chdir(settings.PROJECT_DIR) loader = Loader() loader.load(datayaml) else: self.stdout.write("data.yaml not found, not loading any data.\n") if idle: self.stdout.write("running dummy http server for unknown reasons.\n") dummy_http()
def migrate_fixture(fixture_path, db='fixture_migrator'): """ @brief: Uses South migrations in the current project to update the contents of the fixture at \a fixture_path. @author: Jivan @since: 2014-04-08 """ # --- Create empty database migrated to latest migrations. # from django.core.management.commands.flush import Command as FlushCommand # fc = FlushCommand() # fc.execute(database=db, interactive=False, verbosity=0) logger.info('--- Syncing Database tables to Current Models') from south.management.commands.syncdb import Command as SyncDBCommand sc = SyncDBCommand() sc.execute(migrate_all=True, migrate=False, database=db, interactive=False, verbosity=0) logger.info('--- Faking Migrations to Current Latest') from south.management.commands.migrate import Command as MigrateCommand mc = MigrateCommand() mc.execute(all_apps=True, fake=True, database=db, interactive=False, verbosity=0) # --- Get South Migration History from fixture. # Fixture file with open(fixture_path, 'r') as ff: fixture_contents = json.load(ff) fixture_migrations = [ { i['fields']['app_name']: i['fields']['migration'] } for i in fixture_contents if i['model'] == 'south.migrationhistory' ] if len(fixture_migrations) == 0: logger.info('No migration history found in fixture, guessing migrations from last commit this fixture was migrated.') fixture_migrations = guess_migrations_from_git_repository(fixture_path) fixture_latest_migrations = defaultdict(unicode) for app, migration in fixture_migrations.items(): latest_migration = fixture_latest_migrations[app] if latest_migration == '' or migration > latest_migration: fixture_latest_migrations[app] = migration # --- Migrate database to latest migrations in fixture logger.info('--- Migrating database backwards to latest migrations in fixture.') for app, latest_migration in fixture_latest_migrations.items(): print('Migrating {} to {}'.format(app, latest_migration)) try: mc.execute(app=app, target=latest_migration, database=db, interactive=False, verbosity=0) except ImproperlyConfigured as ex: if ex.message == 'App with label {} could not be found'.format(app): logger.error("Looks like app '{}' was removed from settings. " "I'll remove its entries from South's Migration history " "in the new fixture.".format(app)) MigrationHistory.objects.using(db).filter(app_name=app).delete() continue # --- Load fixture from django.core.management.commands.loaddata import Command as LoadDataCommand ldc = LoadDataCommand() ldc.execute(fixture_path, database=db, verbosity=1) # --- Migrate to latest migrations in codebase mc.execute(database=db, interactive=False, verbosity=1) # --- Dump the contents back out to fixture from django.core.management.commands.dumpdata import Command as DumpDataCommand ddc = DumpDataCommand() from cStringIO import StringIO old_stdout = sys.stdout sys.stdout = mystdout = StringIO() ddc.execute(format='json', indent=4, database=db, exclude=[]) sys.stdout = old_stdout with open(fixture_path, 'w') as f: f.write(mystdout.getvalue()) mystdout.close()
def setUp(self): if 'south' in settings.INSTALLED_APPS: from south.management.commands.migrate import Command command = Command() command.handle(all_apps=True)