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, but discovered 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.

Idea

Generate database state from migrations

GitLab CI (the one I have more experience, but I'm sure others have similar features) has caching based on versioned files.

I can cache database state, based on migrations files (and installed dependencies?).

When cache exists, load this database state (restore?) before your tests and run your tests without migrations.

When cache doesn't exist, run migrations and dump the final state for an SQL file.

Dump and restore are database-specific, but possible to handle all Django supported databases.

Workflow

This is how the "run test" CI job should work.

restore djangomigrations.sql from CI cache

if djangomigrations.sql exists on cache:
  Restore djangomigrations.sql to test database
else:
  Setup test database
  Dump test database to djangomigrations.sql

Clone the test database to run threaded tests
save djangomigrations.sql to CI cache

Cache example on GitHub

TODO #1, I never did it but I'm sure it is possible in some way. See #1

Cache example on GitLab

Still have to abstract for all databases, but I expect something like that will work:

test_job:
  stage: test
  script:
    - |
      if [ -f djangomigrations-dbtest.sqlite3 ]; then
        cp djangomigrations-dbtest.sqlite3 dbtest.sqlite3
      else
        ./manage.py setup_test_db
        cp dbtest.sqlite3 djangomigrations-dbtest.sqlite3
      fi
    - ./manage.py clone_test_db
    - pytest -n $(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:
      - djangomigrations-dbtest.sqlite3

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.1.tar.gz (6.8 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.1-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_migrations_ci-0.0.1.tar.gz
  • Upload date:
  • Size: 6.8 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.1.tar.gz
Algorithm Hash digest
SHA256 32685ee300a5bb243a1e199cc05aa1d7f33c0984c4803a25fb8e7d95dbdea9d5
MD5 fff50a97f633a20a9337aa6b3d73e3c2
BLAKE2b-256 176eabacf4d0767b36a8b47b072745e7ac68d0639ed8fb79dfd9e3e7402c5169

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_migrations_ci-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d3607540adc9d215a3003605d848fdb0d6cc0e485eb81ab24f588d3f205cf3ab
MD5 9695314d32f3dfd7e651584410d9a62b
BLAKE2b-256 16234a50d8dc858230ee4a6451e9f4151fa9f195e13bcdf57ae998567ec487c5

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