Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle migrations in CI of a django application

We have a continuous integration environment with git for a django application.

Sometimes we need to make modifications to our models and migrate the changes in the db (pre/pro).

In our hook we do:

python manage.py migrate

Which gets the migrations previously created in our local development environment and apply them. But I've notice that sometimes migrate asks for user interaction. I.E.:

makemigrations (local development)

(project-name-develop)lostcitizen@project-name:~/dev/project-name/project-name.git$ ./manage.py makemigrations
Did you rename publication.writer to publication.author (a ForeignKey)? [y/N] y
Migrations for 'publications':
  0017_auto_20151117_1050.py:
    - Rename field writer on publication to author

migrate (remote)

(project-name-develop)lostcitizen@project-name:~/dev/project-name/project-name.git$ ./manage.py migrate
Skipping creation of NoticeTypes as notification app not found
Operations to perform:
  Synchronize unmigrated apps: landing, about, publications, [...]
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying publications.0016_auto_20151116_1650... OK
  Applying publications.0017_auto_20151117_1050... OK
The following content types are stale and need to be deleted:

    publications | hashtag

Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
If you're unsure, answer 'no'.

    Type 'yes' to continue, or 'no' to cancel: yes

My question is how to handle this to be "safe" for automated deployment? I can do it manually, but I wanted to abstract this from the rest of the developers workflow.

I think it is not possible to ask for user interaction in a git hook, so I though on using this:

yes | python manage.py migrate

But I don't think it's safe to use it. What do you think?

Regards,

like image 515
lostcitizen Avatar asked Mar 14 '23 03:03

lostcitizen


2 Answers

Django commands have a --noinput switch to help with this situation. Migrate also supports it (see python manage.py migrate --help)

like image 114
tuomur Avatar answered Mar 16 '23 15:03

tuomur


You can use the --noinput option to suppress any user prompts:

python manage.py migrate --noinput

This will choose the safest option for each prompt.

like image 34
knbk Avatar answered Mar 16 '23 16:03

knbk