Migrations - makersacademy/simpleassettracker GitHub Wiki
Migrations are how we create the database structure. To do this you will need to do one of the following:
Creating a new migration
Scenario: You want to make a change or add to the database.
You go to the app where you want to make the change, add the necessary info to your models.py
file and then, in the command line, run the following commands:
python manage.py makemigrations
# This creates a file in the migrations folderpython manage.py migrate
# This checks the files in the migrations folders and applies those that haven't yet been applied
Running an existing migration
Scenario: A team member has made a change to the database and you need to update locally so that you can continue working on the app.
In the command line run the following command:
python manage.py migrate
Heroku
Scenario: A team member has made a change to the database and the app is now broken on Heroku so you need to fix it.
Heroku doesn't automatically run the migrations when it does a build so you need to run them manually. You can either do it via the app at heroku.com or locally if you the Heruko CLI installed. To do it locally you will need to do the following:
- Open the command line, run
heroku login
and follow the onscreen instructions - Run
heroku run python manage.py migrate
Or to do it on heroku.com you will need to do the following:
- Log into your Heroku account and go to the dashboard of the app
- Top right it says
more
click that and then click onrun console
- Run
python manage.py migrate
nb: no need forheroku run
here as Heroku does this automatically
Important notes
The only time you need to run the python manage.py makemigrations
command is when you are creating a new migration. As previously mentioned, this creates a migration file in the migrations folder and this means anyone else only needs to run the migrate command to update their database.
The migration files should not be deleted and recreated as this will cause problems. Within the database structure, there is a table called django_migrations
and this lists all the migration files that have been created. If you delete a set of migrations and then recreate them, the migrations won't get applied correctly. For example, there are 3 migration files in the assets app, a team member decides to delete and recreate the migrations which results in just one migration file. They then make a change to the asset table but you are unable to apply this change as there are now only 2 migration files but your DB has previously run 3 migration files for the assets app so doesn't think anything needs to be applied. To fix this you could delete the necessary migrations from the django_migrations
table but this might not be enough. Potentially you could end up having to drop the whole DB and start again. Doing this would delete all the data (unless you can back it up) and that is not good. Even though you probably won't have much data in your local DB, the production one will have a lot of data that you won't want to delete. The simplest and best thing to do is NEVER touch the migration files. By not touching them, you shouldn't encounter any migration issues.