alfonso.kim
alfonso.kim

Reputation: 2900

Best way to add Django migrations to a django project

I have a Django application initially created for version 1.6 and I've just finished the upgrade for the last version. All the models has managed = False and prior none of them was managed by south and now I want to start using Django migrations for 1.7 version.

Which would be the best and seamless way to do it? I'm afraid that just changing managed = True in all models and run manage.py makemigrations will make a mess in both migrations files and database.

Thanks

EDIT

As was suggested I ran manage.py makemigrations. It created the script 0001_initial with some model definitions but not for all the objects in models package. It creates 11 tables but I have 19 tables. All models has managed = True and I don't have any database router.

Upvotes: 0

Views: 688

Answers (2)

sax
sax

Reputation: 3806

Most depends by the code

The project does not have migrations at all

./manage.py makemigrations
./manage.py migrate

The project has South migrations:

you can:

  1. move the south migrations in south_migrations

    or

  2. totally remove the south migrations

and

./manage.py makemigrations
./manage.py migrate

if you go for option 1 you have to remember to keep uptodate your migrations with both systems (south and django). This is useful only if you want to keep django <1.7 compatibility

You have a pluggable application

This is the most complex situataion as you must preserve the south compatibility and you have to manage different version of south. Here the how to:

  1. move the south migrations in south_migrations
  2. run ./manage.py makemigrations
  3. to prevent South to load the wrong migratins put the following code into migration.__init__.py

```

"""
Django migrations

This package does not contain South migrations.  South migrations can be found
in the ``south_migrations`` package.
"""

SOUTH_ERROR_MESSAGE = """\n
For South support, customize the SOUTH_MIGRATION_MODULES setting like so:

SOUTH_MIGRATION_MODULES = {
    'wfp_auth': 'wfp_auth.south_migrations',
}
"""

# Ensure the user is not using Django 1.6 or below with South
try:
    from django.db import migrations  # noqa
except ImportError:
    from django.core.exceptions import ImproperlyConfigured
raise ImproperlyConfigured(SOUTH_ERROR_MESSAGE)

```

Upvotes: 1

Victor Bruno
Victor Bruno

Reputation: 1043

I have done the migration from 1.6 to 1.7 on an existing project. It was fairly painless.

I renamed my old South migrations folders and let the django 1.7 migrations create a new one. (i.e. $mv appname/migrations appname/migrations.south) That will make it easier to go back to South for any reason and will keep from having a jumbled mess of both in your migrations folders.

Upvotes: 0

Related Questions