Add --fake option to migrate

This commit is contained in:
Andrew Godwin 2013-07-26 16:47:00 +01:00
parent 3f7113f1d9
commit 52643a69e3
2 changed files with 17 additions and 13 deletions

View File

@ -24,6 +24,8 @@ class Command(BaseCommand):
make_option('--database', action='store', dest='database', make_option('--database', action='store', dest='database',
default=DEFAULT_DB_ALIAS, help='Nominates a database to synchronize. ' default=DEFAULT_DB_ALIAS, help='Nominates a database to synchronize. '
'Defaults to the "default" database.'), 'Defaults to the "default" database.'),
make_option('--fake', action='store_true', dest='fake', default=False,
help='Mark migrations as run without actually running them'),
) )
help = "Updates database schema. Manages both apps with migrations and those without." help = "Updates database schema. Manages both apps with migrations and those without."
@ -109,7 +111,7 @@ class Command(BaseCommand):
if self.verbosity >= 1: if self.verbosity >= 1:
self.stdout.write(" No migrations needed.") self.stdout.write(" No migrations needed.")
else: else:
executor.migrate(targets, plan) executor.migrate(targets, plan, fake=options.get("fake", False))
def migration_progress_callback(self, action, migration): def migration_progress_callback(self, action, migration):
if self.verbosity >= 1: if self.verbosity >= 1:
@ -181,7 +183,7 @@ class Command(BaseCommand):
for statement in sql: for statement in sql:
cursor.execute(statement) cursor.execute(statement)
tables.append(connection.introspection.table_name_converter(model._meta.db_table)) tables.append(connection.introspection.table_name_converter(model._meta.db_table))
# We force a commit here, as that was the previous behaviour. # We force a commit here, as that was the previous behaviour.
# If you can prove we don't need this, remove it. # If you can prove we don't need this, remove it.
transaction.set_dirty(using=connection.alias) transaction.set_dirty(using=connection.alias)

View File

@ -44,7 +44,7 @@ class MigrationExecutor(object):
applied.add(migration) applied.add(migration)
return plan return plan
def migrate(self, targets, plan=None): def migrate(self, targets, plan=None, fake=False):
""" """
Migrates the database up to the given targets. Migrates the database up to the given targets.
""" """
@ -52,32 +52,34 @@ class MigrationExecutor(object):
plan = self.migration_plan(targets) plan = self.migration_plan(targets)
for migration, backwards in plan: for migration, backwards in plan:
if not backwards: if not backwards:
self.apply_migration(migration) self.apply_migration(migration, fake=fake)
else: else:
self.unapply_migration(migration) self.unapply_migration(migration, fake=fake)
def apply_migration(self, migration): def apply_migration(self, migration, fake=False):
""" """
Runs a migration forwards. Runs a migration forwards.
""" """
if self.progress_callback: if self.progress_callback:
self.progress_callback("apply_start", migration) self.progress_callback("apply_start", migration)
with self.connection.schema_editor() as schema_editor: if not fake:
project_state = self.loader.graph.project_state((migration.app_label, migration.name), at_end=False) with self.connection.schema_editor() as schema_editor:
migration.apply(project_state, schema_editor) project_state = self.loader.graph.project_state((migration.app_label, migration.name), at_end=False)
migration.apply(project_state, schema_editor)
self.recorder.record_applied(migration.app_label, migration.name) self.recorder.record_applied(migration.app_label, migration.name)
if self.progress_callback: if self.progress_callback:
self.progress_callback("apply_success", migration) self.progress_callback("apply_success", migration)
def unapply_migration(self, migration): def unapply_migration(self, migration, fake=False):
""" """
Runs a migration backwards. Runs a migration backwards.
""" """
if self.progress_callback: if self.progress_callback:
self.progress_callback("unapply_start", migration) self.progress_callback("unapply_start", migration)
with self.connection.schema_editor() as schema_editor: if not fake:
project_state = self.loader.graph.project_state((migration.app_label, migration.name), at_end=False) with self.connection.schema_editor() as schema_editor:
migration.unapply(project_state, schema_editor) project_state = self.loader.graph.project_state((migration.app_label, migration.name), at_end=False)
migration.unapply(project_state, schema_editor)
self.recorder.record_unapplied(migration.app_label, migration.name) self.recorder.record_unapplied(migration.app_label, migration.name)
if self.progress_callback: if self.progress_callback:
self.progress_callback("unapply_success", migration) self.progress_callback("unapply_success", migration)