Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically generate custom migrations in Django

Django provides a really nice feature called makemigrations where it will create migration files based on the changes in models. We are developing a module where we will want to generate custom migrations.

I haven't found much info about creating custom migrations in the Django docs. There is documentation on the various Operation classes that can yield migrations but there's nothing about creating custom Operation classes that can yield custom migrations.

The autodetector module for generating new migrations also doesn't seem to leave much room for adding custom Operation classes: https://github.com/django/django/blob/master/django/db/migrations/autodetector.py#L160

It seems this is completely static. Are there other ways to generate custom migrations, perhaps by using existing classes with a custom management command?

like image 391
Robbietjuh Avatar asked Mar 15 '18 10:03

Robbietjuh


People also ask

How do I recreate migrations in Django?

Django's migration can be reset by cleaning all the migration files except __init__.py files under each project app directory, followed by dropping the database and creating migration again using python manage.py makemigrations and python manage.py migrate .

What task do migrations automate Django?

Migrations are Django's way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema. They're designed to be mostly automatic, but you'll need to know when to make migrations, when to run them, and the common problems you might run into.


1 Answers

You can create a custom class to hook into the makemigrations class and add your custom migrations stuff then execute using the "runscript" command. Below is a sample module where the file is named custom_migrations.py and located in a "scripts" folder off one of your apps:

from django.core.management.commands.makemigrations import Command
"""
To invoke this script use:
   manage.py runscript custom_migrations --script-args [app_label [app_label ...]] name=my_special_migration verbosity=1
"""    

class MyMigrationMaker(Command):
    '''
    Override the write method to add more stuff before finishing
    '''


    def write_migration_files(self, changes):
        print("Do some stuff to \"changes\" object here...")
        super().write_migration_files(changes)



def run(*args):
    nargs = []
    kwargs = {}
    # Preload some options with defaults and then can be overridden in the args parsing
    kwargs['empty'] = True
    kwargs['verbosity'] = 1
    kwargs['interactive'] = True
    kwargs['dry_run'] = False
    kwargs['merge'] = False
    kwargs['name'] = 'custom_migration'
    kwargs['check_changes'] = False
    for arg in args:
        kwarg = arg.split('=', 1)
        if len(kwarg) > 1:
            val = kwarg[1]
            if val == "True":
                arg_val = True
            elif val == "False":
                arg_val = False
            elif val.isdigits():
                arg_val = int(val)
            else:
                arg_val = val
            the_kwargs[kwarg[0]] = arg_val
        else:
            nargs.append(arg)
    MyMigrationMaker().handle(*nargs, **kwargs)
like image 176
Christopher Broderick Avatar answered Oct 06 '22 00:10

Christopher Broderick