Django migrations CI optimization
Project description
django-migrations-ci
Optimizations to run less migrations on CI.
Django migrations are slow because of state recreation for every migration and other internal Django magic.
In the past, I tried to optimize that on Django core, but learnt it's a running issue.
Assumptions
- I want to run my migrations on CI. It is a sanity check, even if they are generated by Django in some cases.
- I don't have to run migrations all the time. If migrations didn't change, I can reuse them.
- The solutions probably are CI-specific and database-specific, but it is possible to write code for a generic workflow and snippets to support each CI.
Install
Install the package with pip:
pip install django-migrations-ci
Add django_migrations_ci
to Django settings INSTALLED_APPS
.
INSTALLED_APPS = [
..., # other packages
"django_migrations_ci",
]
How to use
The command migrateci
execute all migrations and generate dump files migrateci-*
to be cached on CI.
If these files already exist on disk, they are used to prepare the database without running all migrations again.
Configure your CI to cache these migrateci-*
files, based on migration files.
/
Workflow
This is how the "run test" CI job should work.
Restore migrateci.sql from CI cache
if migrateci.sql exists on cache:
Restore migrateci.sql to test database
else:
Setup test database
Dump test database to migrateci.sql
Clone the test database to run threaded tests
Save migrateci.sql to CI cache
Cache example on GitHub
steps:
- uses: actions/cache@v3
name: Cache migrations
with:
path: migrateci-*
key: ${{ secrets.EXAMPLE_CACHE_PREFIX }}-${{ hashFiles('**/migrations/*.py') }}
- name: Migrate database
run: ./manage.py migrateci --parallel $(nproc)
- name: Test with Django
run: ./manage.py test --keepdb --parallel $(nproc)
Cache example on GitLab
Still have to abstract psql/pg_dump/pg_restore
, but I expect something like that will work:
test_job:
stage: test
script:
- ./manage.py migrateci $(nproc)
- ./manage.py test --keepdb --parallel $(nproc)
cache:
key:
# GitLab docs say it accepts only two files, but for some reason it works with wildcards too.
# You can't add more than two lines here.
files:
- "requirements.txt"
- "*/migrations/*.py"
paths:
- migrateci-*
How databases for parallel tests are named
Django test framework has a --parallel N
flag to test with N parallel processes,
naming databases from 1 to N.
- On sqlite3, a
db.sqlite3
generatedb_N.sqlite3
files. - On PostgreSQL, a
db
generatetest_db_N
.
Pytest pytest-django
use pytest-xdist
for parallel support, naming databases
from 0 to N-1.
- On sqlite3, a
db.sqlite3
generatedb.sqlite3_gwN
files. - On PostgreSQL, a
db
generatetest_db_gwN
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_migrations_ci-0.0.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 363bfdf80f5b507ee8598658d300f79ad19def0d2d0565ce4d8a4588faf7cbd0 |
|
MD5 | f4beb1c381b9f718f9b7bf0f282e4d93 |
|
BLAKE2b-256 | 512572ad1987cfb119d8880927cb9f16eac68ddb3fc41d819d6533d3d3a52201 |
Hashes for django_migrations_ci-0.0.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8429f82bb6dcc4d19cf5a577f535ba96906d4c6755bf65e3073a53f6fe75dd8d |
|
MD5 | 8f1644d3a45019548f14f2dbb0b920fe |
|
BLAKE2b-256 | 7ad12cb42ce4b07ea00d48321b8386e53329481748ebe38fc1b1834ba9d5cd66 |