Created
February 29, 2020 19:33
-
-
Save nmfzone/0b6ac9b631a6ad4c1985b9eeb0497564 to your computer and use it in GitHub Desktop.
Django migrate:fresh command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.core.management import call_command | |
from django.core.management.base import BaseCommand | |
from django.db import connection | |
class Command(BaseCommand): | |
help = 'Delete all tables and run all migrations' | |
def add_arguments(self, parser): | |
parser.add_argument('--seed', nargs='*', help='Run the DB seeders.') | |
def handle(self, *app_labels, **options): | |
tables = connection.introspection.table_names() | |
connection.disable_constraint_checking() | |
schema_editor = connection.schema_editor() | |
schema_editor.execute(schema_editor.sql_delete_table % { | |
'table': ','.join(tables) | |
}) | |
connection.enable_constraint_checking() | |
call_command('migrate') | |
if options['seed'] is not None: | |
if len(options['seed']) == 0: | |
call_command('seed') | |
else: | |
call_command('seed', *options['seed']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want another version called
migraterefresh
(un-apply all migrations), you can go to:https://gist.github.com/nmfzone/2598d56144f911649385aaaefaac00d7