def handle(self, app=None, *args, **options): # Make sure we have an app if not app: print "Please specify an app to convert." return # See if the app exists app = app.split(".")[-1] try: app_module = models.get_app(app) except ImproperlyConfigured: print "There is no enabled application matching '%s'." % app return # Try to get its list of models model_list = models.get_models(app_module) if not model_list: print "This application has no models; this command is for applications that already have models syncdb'd." print "Make some models, and then use ./manage.py schemamigration %s --initial instead." % app return # Ask South if it thinks it's already got migrations try: Migrations(app) except NoMigrations: pass else: print "This application is already managed by South." return # Finally! It seems we've got a candidate, so do the two-command trick verbosity = int(options.get('verbosity', 0)) management.call_command("schemamigration", app, initial=True, verbosity=verbosity) # Now, we need to re-clean and sanitise appcache hacks.clear_app_cache() hacks.repopulate_app_cache() # And also clear our cached Migration classes Migrations._clear_cache() # Now, migrate management.call_command( "migrate", app, "0001", fake=True, verbosity=verbosity, ignore_ghosts=options.get("ignore_ghosts", False), delete_ghosts=options.get("delete_ghosts", False), ) print print "App '%s' converted. Note that South assumed the application's models matched the database" % app print "(i.e. you haven't changed it since last syncdb); if you have, you should delete the %s/migrations" % app print "directory, revert models.py so it matches the database, and try again."
def handle(self, app=None, *args, **options): # Make sure we have an app if not app: print("Please specify an app to convert.") return # See if the app exists app = app.split(".")[-1] try: app_module = models.get_app(app) except ImproperlyConfigured: print("There is no enabled application matching '%s'." % app) return # Try to get its list of models model_list = models.get_models(app_module) if not model_list: print("This application has no models; this command is for applications that already have models syncdb'd.") print("Make some models, and then use ./manage.py schemamigration %s --initial instead." % app) return # Ask South if it thinks it's already got migrations try: Migrations(app) except NoMigrations: pass else: print("This application is already managed by South.") return # Finally! It seems we've got a candidate, so do the two-command trick verbosity = int(options.get('verbosity', 0)) management.call_command("schemamigration", app, initial=True, verbosity=verbosity) # Now, we need to re-clean and sanitise appcache hacks.clear_app_cache() hacks.repopulate_app_cache() # And also clear our cached Migration classes Migrations._clear_cache() # Now, migrate management.call_command( "migrate", app, "0001", fake=True, verbosity=verbosity, ignore_ghosts=options.get("ignore_ghosts", False), delete_ghosts=options.get("delete_ghosts", False), ) print() print("App '%s' converted. Note that South assumed the application's models matched the database" % app) print("(i.e. you haven't changed it since last syncdb); if you have, you should delete the %s/migrations" % app) print("directory, revert models.py so it matches the database, and try again.")
def _south_migrate_all(): if not 'south' in settings.INSTALLED_APPS: sys.stderr.write( "warning: 'south' is not in INSTALLED_APPS, no migration done.\n") return 0 #pylint: disable=import-error,too-many-nested-blocks from south.migration import Migrations from south.management.commands import migrate schema_cmd = SchemaMigration() initial_apps = [] auto_apps = [] #pylint: disable=unused-variable for app in [app for app in settings.INSTALLED_APPS if app != 'south']: try: app_module = models.get_app(app) #pylint: disable=no-member # South only used with Django < 1.7 clsmembers = inspect.getmembers(app_module, is_model_class) if clsmembers: migrations_dir = os.path.join( os.path.dirname(app_module.__file__), 'migrations') if os.path.isdir(migrations_dir): schema_cmd.handle(app, auto=True)#pylint:disable=no-member found = False for migration_file in os.listdir(migrations_dir): if (re.match(r'^\d\d\d\d', migration_file) and not migration_file.startswith('0001_initial')): found = True break if found: auto_apps += [app] else: initial_apps += [app] else: schema_cmd.handle( #pylint:disable=no-member app, initial=True) initial_apps += [app] else: sys.stderr.write( "warning: App %s does not seem to contain any Model\n" % app) except OSError as err: sys.stderr.write("error: App %s, %s\n" % (app, err)) except RuntimeError as err: sys.stderr.write("error: App %s, %s\n" % (app, err)) except ImproperlyConfigured: sys.stderr.write( "warning: App %s does not seem to contain a models.py\n" % app) # Clear the cached Migrations instances now that we have more of them. Migrations._clear_cache() #pylint: disable=no-member,protected-access migrate_cmd = migrate.Command() for app in initial_apps: sys.stderr.write("initial migrate for %s\n" % app) migrate_cmd.handle(app, fake=True) sys.stderr.write("MIGRATE ALL!\n") migrate_cmd.handle(no_initial_data=True) return 0
else: schema_cmd.handle( #pylint:disable=no-member app, initial=True) initial_apps += [app] else: print("warning: App %s does not seem to contain any Model" % app) except OSError, err: print "error: App %s, %s" % (app, err) except RuntimeError, err: print "warning: App %s, %s" % (app, err) except ImproperlyConfigured: print "warning: App %s does not seem to contain a models.py" % app # Clear the cached Migrations instances now that we have more of them. Migrations._clear_cache() #pylint: disable=no-member,protected-access migrate_cmd = migrate.Command() for app in initial_apps: print "initial migrate for %s" % app migrate_cmd.handle(app, fake=True) print "MIGRATE ALL!" migrate_cmd.handle(no_initial_data=True) return 0 def migrate_all(): """ Create schema migrations for all apps specified in INSTALLED_APPS, then run a migrate command. """ if 'south' in settings.INSTALLED_APPS: