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',
default=DEFAULT_DB_ALIAS, help='Nominates a database to synchronize. '
'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."
@ -109,7 +111,7 @@ class Command(BaseCommand):
if self.verbosity >= 1:
self.stdout.write(" No migrations needed.")
else:
executor.migrate(targets, plan)
executor.migrate(targets, plan, fake=options.get("fake", False))
def migration_progress_callback(self, action, migration):
if self.verbosity >= 1:

View File

@ -44,7 +44,7 @@ class MigrationExecutor(object):
applied.add(migration)
return plan
def migrate(self, targets, plan=None):
def migrate(self, targets, plan=None, fake=False):
"""
Migrates the database up to the given targets.
"""
@ -52,16 +52,17 @@ class MigrationExecutor(object):
plan = self.migration_plan(targets)
for migration, backwards in plan:
if not backwards:
self.apply_migration(migration)
self.apply_migration(migration, fake=fake)
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.
"""
if self.progress_callback:
self.progress_callback("apply_start", migration)
if not fake:
with self.connection.schema_editor() as schema_editor:
project_state = self.loader.graph.project_state((migration.app_label, migration.name), at_end=False)
migration.apply(project_state, schema_editor)
@ -69,12 +70,13 @@ class MigrationExecutor(object):
if self.progress_callback:
self.progress_callback("apply_success", migration)
def unapply_migration(self, migration):
def unapply_migration(self, migration, fake=False):
"""
Runs a migration backwards.
"""
if self.progress_callback:
self.progress_callback("unapply_start", migration)
if not fake:
with self.connection.schema_editor() as schema_editor:
project_state = self.loader.graph.project_state((migration.app_label, migration.name), at_end=False)
migration.unapply(project_state, schema_editor)