def migration_strings(f): """ """ models, complete_apps = parse_migration(f) strings = set(complete_apps) _deep_add(strings, models) return strings
def squash_migrations(app_to_migration_path): """ Squash many initial migrations into a single migration. `app_to_migration_path` is a dict from app name to the path of the initial migration for that app. Returns the squashed migration as a StringIO object. The squashed migration has: - no dependencies - a forwards() function obtained by concatenating the forwards() functions of all the given migrations - a backwards() function that raises RuntimeError - all frozen models in all the given migrations - all complete_apps in all the given migrations """ output = StringIO() # Write header and start the forwards() method. print >>output, _imports print >>output, _class_def print >>output, _forwards # In forwards(), concatenate all the forwards() of individual migrations. for app, migration_path in sorted(app_to_migration_path.items()): print >>output, "\n ### {} app ###\n".format(app) output.write(forwards_contents(open(migration_path))) # Write the backwards() method. print >>output, _backwards # Collect all frozen models from individual migrations, and write all of # them to the new migration. all_models = {} for app, migration_path in app_to_migration_path.iteritems(): models, complete_apps = parse_migration(open(migration_path)) if complete_apps != [app]: raise ValueError("App '{}' has unexpected complete_apps {}.".format( app, complete_apps)) all_models.update(models) _pretty_print_models(all_models, output) # Write complete_apps for the new migration. _pretty_print_complete_apps(app_to_migration_path.keys(), output) return output
def make_dummy_migration(app, migration_path, destination_app, squashed_migration_name): """ Make a new initial migration for an app whose initial migration has been squashed. `app` is the name of the app for which to create an initial migration. `migration_path` is the path to the initial migration for that app. `destination_app` is the app containing the new squashed migration. `squashed_migration_name` is the name of the squashed migration (without the '.py' suffix) Returns the new initial migration as a StringIO object. The new migration has: - a dependency on the squashed migration - a forwards() function that does nothing - a backwards() function that raises RuntimeError - all frozen models in the original migration - the complete_apps in the original migration """ output = StringIO() # Write header, depends_on, dummy forwards, and backwards. print >>output, _imports print >>output, _class_def print >>output, _depends_on.format(destination_app, squashed_migration_name) print >>output, _forwards print >>output, " pass" print >>output, _backwards # Write the original frozen models and complete_apps for this migration. models, complete_apps = parse_migration(open(migration_path)) _pretty_print_models(models, output) _pretty_print_complete_apps(complete_apps, output) return output