Automation of fake migration in Docker-Compose + Django using Makefile (Unix / Linux)
Automation of fake migration (Unix / Linux)
Before we start what is Makefile ?
Make is Unix utility that is designed to start execution of a makefile. A makefile is a special file, containing shell commands, that you create and name makefile (or Makefile depending upon the system). While in the directory containing this makefile, you will type make and the commands in the makefile will be executed. If you create more than one makefile, be certain you are in the correct directory before typing make.
Let’s begin
1st create a makefile in your django project
since i was using docker my directory look like this.
2. Populate Makefile with this script
docker-compose = docker-compose run api python manage.pyfakeapp:
ifeq ($(application),)
echo "Can't Proceed Without putting any values in test"
else
$(docker-compose) showmigrations
$(docker-compose) migrate --fake $(application) zero
find . -path "*/${application}/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/${application}/migrations/*.pyc" -delete
$(docker-compose) makemigrations
$(docker-compose) migrate --fake-initial
endif
now i will explain what’s happening right here
i use a fakeapp parameter where when i type
make fakeapp
the fakeapp parameter will now be executed but i use conditions here where if application is null
so the command will be
make fakeapp application=information
but in the first line of else i run
1. $(docker-compose) showmigrations
to show the migrations where i want to be faked
Output
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
[X] 0003_logentry_add_action_flag_choices
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
[X] 0008_alter_user_username_max_length
[X] 0009_alter_user_last_name_max_length
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
information
[X] 0001_initial
[X] 0002_xxx
[X] 0003_xxx
========================================
2. $(docker-compose) migrate --fake $(application) zero
is equivalent to
docker-compose run api python manage.py migrate --fake information zero
now the information application will be set to faked migration
Output
Operations to perform:
Unapply all migrations: requests
Running migrations:
Rendering model states... DONE
Unapplying requests.0001_initial... FAKED
Unapplying requests.0002_xxx... FAKED
Unapplying requests.0003_xxx... FAKED
========================================
3. find . -path "*/${application}/migrations/*.py" -not -name "__init__.py" -delete
4. find . -path "*/${application}/migrations/*.pyc" -delete
this part will looking for /information/migrations/*.py
and delete all .py but not including __init__.py
5. $(docker-compose) makemigrations
Now it will create the fresh migration
6. lastly $(docker-compose) migrate --fake-initial
It will now faked the migration
Output
Operations to perform:
Apply all migrations: admin, auth, contenttypes, requests, sessions
Running migrations:
Applying requests.0001_initial... FAKED
Clarification
I just saw this kind of implementation in https://simpleisbetterthancomplex.com/tutorial/2016/07/26/how-to-reset-migrations.html and add automation in the current implementation given.