Skip to main content

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

  1. I want to run my migrations on CI. It is a sanity check, even if they are generated by Django in some cases.
  2. I don't have to run migrations all the time. If migrations didn't change, I can reuse them.
  3. 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 generate db_N.sqlite3 files.
  • On PostgreSQL, a db generate test_db_N.

Pytest pytest-django use pytest-xdist for parallel support, naming databases from 0 to N-1.

  • On sqlite3, a db.sqlite3 generate db.sqlite3_gwN files.
  • On PostgreSQL, a db generate test_db_gwN.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_migrations_ci-0.0.2.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_migrations_ci-0.0.2-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file django_migrations_ci-0.0.2.tar.gz.

File metadata

  • Download URL: django_migrations_ci-0.0.2.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for django_migrations_ci-0.0.2.tar.gz
Algorithm Hash digest
SHA256 09eeeef4b9c3048a6d920776862209df384770613f24727a750605f704dce857
MD5 f5c8237f6ea4cfd4e57c2686df997f61
BLAKE2b-256 2e8c9fb30648daa9ca37ff50fb3c38e2494e841c3152b9b2a00faa6e786f98f4

See more details on using hashes here.

File details

Details for the file django_migrations_ci-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_migrations_ci-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b8cf3b7f3b230c0c229d7800a5be21d1347833eff0c504a055bc81c1e4d5b0ce
MD5 7598656b37bf65d90e2cbe763c68cad7
BLAKE2b-256 7a651cda0747a1463b64902d4f428656af5327b881315f423e0a51cbb9cbef7c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page