Add --dry-run option to makemigrations

This commit is contained in:
Andrew Godwin 2013-12-04 13:55:20 +00:00
parent 1d20e6df95
commit ab587fa51a
1 changed files with 17 additions and 15 deletions

View File

@ -14,17 +14,18 @@ from django.db.models.loading import cache
class Command(BaseCommand): class Command(BaseCommand):
option_list = BaseCommand.option_list + ( option_list = BaseCommand.option_list + (
make_option('--empty', action='store_true', dest='empty', default=False, make_option('--dry-run', action='store_true', dest='dry_run', default=False,
help='Make a blank migration.'), help="Just show what migrations would be made; don't actually write them."),
) )
help = "Creates new migration(s) for apps." help = "Creates new migration(s) for apps."
usage_str = "Usage: ./manage.py makemigrations [--empty] [app [app ...]]" usage_str = "Usage: ./manage.py makemigrations [--dry-run] [app [app ...]]"
def handle(self, *app_labels, **options): def handle(self, *app_labels, **options):
self.verbosity = int(options.get('verbosity')) self.verbosity = int(options.get('verbosity'))
self.interactive = options.get('interactive') self.interactive = options.get('interactive')
self.dry_run = options.get('dry_run', False)
# Make sure the app they asked for exists # Make sure the app they asked for exists
app_labels = set(app_labels) app_labels = set(app_labels)
@ -73,15 +74,16 @@ class Command(BaseCommand):
for operation in migration.operations: for operation in migration.operations:
self.stdout.write(" - %s\n" % operation.describe()) self.stdout.write(" - %s\n" % operation.describe())
# Write it # Write it
migrations_directory = os.path.dirname(writer.path) if not self.dry_run:
if not directory_created.get(app_label, False): migrations_directory = os.path.dirname(writer.path)
if not os.path.isdir(migrations_directory): if not directory_created.get(app_label, False):
os.mkdir(migrations_directory) if not os.path.isdir(migrations_directory):
init_path = os.path.join(migrations_directory, "__init__.py") os.mkdir(migrations_directory)
if not os.path.isfile(init_path): init_path = os.path.join(migrations_directory, "__init__.py")
open(init_path, "w").close() if not os.path.isfile(init_path):
# We just do this once per app open(init_path, "w").close()
directory_created[app_label] = True # We just do this once per app
migration_string = writer.as_string() directory_created[app_label] = True
with open(writer.path, "wb") as fh: migration_string = writer.as_string()
fh.write(migration_string) with open(writer.path, "wb") as fh:
fh.write(migration_string)