2020-08-11 01:47:54 +02:00
|
|
|
# Schema migrations
|
2016-04-01 08:50:53 +02:00
|
|
|
|
|
|
|
Zulip uses the [standard Django system for doing schema
|
2024-05-24 16:57:31 +02:00
|
|
|
migrations](https://docs.djangoproject.com/en/5.0/topics/migrations/).
|
2016-06-26 18:47:23 +02:00
|
|
|
There is some example usage in the [new feature
|
2019-09-30 19:37:56 +02:00
|
|
|
tutorial](../tutorials/new-feature-tutorial.md).
|
2016-04-01 08:50:53 +02:00
|
|
|
|
|
|
|
This page documents some important issues related to writing schema
|
|
|
|
migrations.
|
2017-07-11 01:02:19 +02:00
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- If your database migration is just to reflect new fields in
|
2023-12-05 19:29:58 +01:00
|
|
|
`models/*.py`, you'll typically want to just:
|
2021-08-20 21:45:39 +02:00
|
|
|
- Rebase your branch before you start (this may save work later).
|
2023-12-05 19:29:58 +01:00
|
|
|
- Update the model class definitions in `zerver/models/*.py`.
|
2021-08-20 21:45:39 +02:00
|
|
|
- Run `./manage.py makemigrations` to generate a migration file
|
|
|
|
- Rename the migration file to have a descriptive name if Django
|
2020-01-17 09:00:09 +01:00
|
|
|
generated used a date-based name like `0089_auto_20170710_1353.py`
|
|
|
|
(which happens when the changes are to multiple models and Django).
|
2021-08-20 21:45:39 +02:00
|
|
|
- `git add` the new migration file
|
|
|
|
- Run `tools/provision` to update your local database to apply the
|
2020-01-17 09:00:09 +01:00
|
|
|
migrations.
|
2021-08-20 21:45:39 +02:00
|
|
|
- Commit your changes.
|
|
|
|
- For more complicated migrations where you need to run custom Python
|
2020-01-17 09:00:09 +01:00
|
|
|
code as part of the migration, it's best to read past migrations to
|
2021-09-08 00:23:24 +02:00
|
|
|
understand how to write them well.
|
|
|
|
`git grep RunPython zerver/migrations/02*` will find many good
|
2021-08-20 21:53:28 +02:00
|
|
|
examples. Before writing migrations of this form, you should read
|
2021-09-08 00:23:24 +02:00
|
|
|
Django's docs and the sections below.
|
2021-08-20 21:45:39 +02:00
|
|
|
- **Numbering conflicts across branches**: If you've done your schema
|
2016-04-01 08:50:53 +02:00
|
|
|
change in a branch, and meanwhile another schema change has taken
|
2019-11-08 22:49:24 +01:00
|
|
|
place, Django will now have two migrations with the same
|
|
|
|
number. There are two easy way to fix this:
|
2021-08-20 21:45:39 +02:00
|
|
|
- If your migrations were automatically generated using
|
2021-09-08 00:23:24 +02:00
|
|
|
`manage.py makemigrations`, a good option is to just remove your
|
2021-08-20 21:53:28 +02:00
|
|
|
migration and rerun the command after rebasing. Remember to
|
2024-05-20 22:16:21 +02:00
|
|
|
`git rebase` to do this in the commit that changed `models/*.py`
|
2021-09-08 00:23:24 +02:00
|
|
|
if you have a multi-commit branch.
|
2021-08-20 21:45:39 +02:00
|
|
|
- If you wrote code as part of preparing your migrations, or prefer
|
2019-11-08 22:49:24 +01:00
|
|
|
this workflow, you can use run `./tools/renumber-migrations`,
|
|
|
|
which renumbers your migration(s) and fixes up the "dependencies"
|
2021-08-20 21:53:28 +02:00
|
|
|
entries in your migration(s). The tool could use a bit of work to
|
2019-11-08 22:49:24 +01:00
|
|
|
prompt unnecessarily less, but it will update the working tree for
|
|
|
|
you automatically (you still need to do all the `git add`
|
|
|
|
commands, etc.).
|
2021-10-05 06:31:03 +02:00
|
|
|
- **Release branches**: When a release branch needs a migration, but
|
|
|
|
`main` already has new migrations, the migration graph must fork.
|
|
|
|
The migration should be named and numbered in `main` to follow in
|
|
|
|
usual sequence, but its dependency should be set to the last
|
2021-10-05 07:16:55 +02:00
|
|
|
migration that exists on the release branch. This should be
|
2021-10-05 06:31:03 +02:00
|
|
|
followed, on `main`, by a migration which merges the two resulting
|
2021-10-05 07:16:55 +02:00
|
|
|
tips; you can make such a merge with `manage.py makemigrations --merge`.
|
2024-07-04 12:33:43 +02:00
|
|
|
- **Large tables**: For our very largest tables (e.g., Message and
|
2020-01-17 09:00:09 +01:00
|
|
|
UserMessage), we often need to take precautions when adding columns
|
|
|
|
to the table, performing data backfills, or building indexes. We
|
|
|
|
have a `zerver/lib/migrate.py` library to help with adding columns
|
2021-05-08 15:58:54 +02:00
|
|
|
and backfilling data.
|
2022-02-23 08:34:40 +01:00
|
|
|
- **Adding indexes**. Django's regular `AddIndex` operation (corresponding
|
|
|
|
to `CREATE INDEX` in SQL) locks writes to the affected table. This can be
|
2021-05-08 15:58:54 +02:00
|
|
|
problematic when dealing with larger tables in particular and we've
|
2022-02-23 08:34:40 +01:00
|
|
|
generally preferred to use `AddIndexConcurrently` (corresponding to
|
|
|
|
`CREATE INDEX CONCURRENTLY`) to allow the index to be built while
|
|
|
|
the server is active.
|
2021-08-20 21:53:28 +02:00
|
|
|
- **Atomicity**. By default, each Django migration is run atomically
|
|
|
|
inside a transaction. This can be problematic if one wants to do
|
2017-02-23 08:35:37 +01:00
|
|
|
something in a migration that touches a lot of data and would best
|
2024-07-09 17:45:31 +02:00
|
|
|
be done in batches of, for example, 1000 objects (e.g., a `Message` or
|
2021-08-20 21:53:28 +02:00
|
|
|
`UserMessage` table change). There is a [useful Django
|
2020-01-17 09:00:09 +01:00
|
|
|
feature][migrations-non-atomic] that makes it possible to add
|
2017-02-23 08:35:37 +01:00
|
|
|
`atomic=False` at the top of a `Migration` class and thus not have
|
2021-08-20 21:53:28 +02:00
|
|
|
the entire migration in a transaction. This should make it possible
|
2017-02-23 08:35:37 +01:00
|
|
|
to use the batch update tools in `zerver/lib/migrate.py` (originally
|
|
|
|
written to work with South) for doing larger database migrations.
|
2022-10-31 20:29:07 +01:00
|
|
|
- **No-op migrations**. Django detects model changes that does not
|
|
|
|
necessarily lead to a schema change in the database.
|
|
|
|
For example, field validators are a part of the Django ORM, but they
|
|
|
|
are not stored in the database. When removing such validators from
|
|
|
|
an existing model, nothing gets dropped from the database, but Django
|
|
|
|
would still generate a migration for that. We prefer to avoid adding
|
|
|
|
this kind of no-op migrations. Instead of generating a new migration,
|
|
|
|
you'll want to modify the latest migration affecting the field.
|
2017-02-23 08:35:37 +01:00
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- **Accessing code and models in RunPython migrations**. When writing
|
2017-03-15 03:48:55 +01:00
|
|
|
a migration that includes custom python code (aka `RunPython`), you
|
|
|
|
almost never want to import code from `zerver` or anywhere else in
|
|
|
|
the codebase. If you imagine the process of upgrading a Zulip
|
|
|
|
server, it goes as follows: first a server admin checks out a recent
|
|
|
|
version of the code, and then runs any migrations that were added
|
|
|
|
between the last time they upgraded and the current check out. Note
|
|
|
|
that for each migration, this means the migration is run using the
|
|
|
|
code in the server admin's check out, and not the code that was there at the
|
|
|
|
time the migration was written. This can be a difference of
|
|
|
|
thousands of commits for installations that are only upgraded
|
|
|
|
occasionally. It is hard to reason about the effect of a code change
|
|
|
|
on a migration that imported it so long ago, so we recommend just
|
|
|
|
copying any code you're tempted to import into the migration file
|
|
|
|
directly, and have a linter rule enforcing this.
|
|
|
|
|
|
|
|
There is one special case where this doesn't work: you can't copy
|
|
|
|
the definition of a model (like `Realm`) into a migration, and you
|
|
|
|
can't import it from `zerver.models` for the reasons above. In this
|
|
|
|
situation you should use Django's `apps.get_model` to get access to
|
|
|
|
a model as it is at the time of a migration. Note that this will
|
|
|
|
work for doing something like `Realm.objects.filter(..)`, but
|
2020-01-17 09:00:09 +01:00
|
|
|
shouldn't be used for accessing properties like `Realm.subdomain` or
|
|
|
|
anything not related to the Django ORM.
|
2017-03-05 02:29:08 +01:00
|
|
|
|
2021-05-08 16:08:31 +02:00
|
|
|
Another important note is that making changes to the data in a table
|
|
|
|
via `RunPython` code and `ALTER TABLE` operations within a single,
|
|
|
|
atomic migration don't mix well. If you encounter an error such as
|
2021-08-20 22:54:08 +02:00
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```text
|
2021-05-08 16:08:31 +02:00
|
|
|
django.db.utils.OperationalError: cannot ALTER TABLE "table_name" because it has pending trigger events
|
|
|
|
```
|
2021-08-20 22:54:08 +02:00
|
|
|
|
2021-05-08 16:08:31 +02:00
|
|
|
when testing the migration, the reason is often that these operations
|
|
|
|
were incorrectly mixed. To resolve this, consider making the migration
|
|
|
|
non-atomic, splitting it into two migration files (recommended), or replacing the
|
|
|
|
`RunPython` logic with pure SQL (though this can generally be difficult).
|
|
|
|
|
2021-08-20 21:53:28 +02:00
|
|
|
- **Making large migrations work**. Major migrations should have a
|
2021-08-20 22:54:08 +02:00
|
|
|
few properties:
|
2017-02-23 08:43:36 +01:00
|
|
|
|
2021-08-20 21:53:28 +02:00
|
|
|
- **Unit tests**. You'll want to carefully test these, so you might
|
2017-02-23 08:43:36 +01:00
|
|
|
as well write some unit tests to verify the migration works
|
2021-08-20 21:53:28 +02:00
|
|
|
correctly, rather than doing everything by hand. This often saves
|
2017-02-23 08:43:36 +01:00
|
|
|
a lot of time in re-testing the migration process as we make
|
|
|
|
adjustments to the plan.
|
2021-08-20 21:53:28 +02:00
|
|
|
- **Run in batches**. Updating more than 1K-10K rows (depending on
|
|
|
|
type) in a single transaction can lock up a database. It's best
|
2017-02-23 08:43:36 +01:00
|
|
|
to do lots of small batches, potentially with a brief sleep in
|
|
|
|
between, so that we don't block other operations from finishing.
|
2021-08-20 21:53:28 +02:00
|
|
|
- **Rerunnability/idempotency**. Good migrations are ones where if
|
2024-07-04 12:33:43 +02:00
|
|
|
operational concerns (e.g., it taking down the Zulip server for
|
2017-02-23 08:43:36 +01:00
|
|
|
users) interfere with it finishing, it's easy to restart the
|
2021-08-20 21:53:28 +02:00
|
|
|
migration without doing a bunch of hand investigation. Ideally,
|
2017-02-23 08:43:36 +01:00
|
|
|
the migration can even continue where it left off, without needing
|
|
|
|
to redo work.
|
2021-08-20 21:53:28 +02:00
|
|
|
- **Multi-step migrations**. For really big migrations, one wants
|
2022-02-08 00:13:33 +01:00
|
|
|
to split the transition into several commits that are each
|
2021-08-20 22:54:08 +02:00
|
|
|
individually correct, and can each be deployed independently:
|
2017-02-23 08:43:36 +01:00
|
|
|
|
|
|
|
1. First, do a migration to add the new column to the Message table
|
2021-08-20 22:54:08 +02:00
|
|
|
and start writing to that column (but don't use it for anything)
|
2017-02-23 08:43:36 +01:00
|
|
|
2. Second, do a migration to copy values from the old column to
|
2021-08-20 22:54:08 +02:00
|
|
|
the new column, to ensure that the two data stores agree.
|
2017-02-23 08:43:36 +01:00
|
|
|
3. Third, a commit that stops writing to the old field.
|
2024-07-04 12:33:43 +02:00
|
|
|
4. Any cleanup work, e.g., if the old field were a column, we'd do
|
2017-02-23 08:43:36 +01:00
|
|
|
a migration to remove it entirely here.
|
|
|
|
|
|
|
|
This multi-step process is how most migrations on large database
|
|
|
|
tables are done in large-scale systems, since it ensures that the
|
|
|
|
system can continue running happily during the migration.
|
|
|
|
|
2018-05-21 18:56:45 +02:00
|
|
|
## Automated testing for migrations
|
|
|
|
|
|
|
|
Zulip has support for writing automated tests for your database
|
2021-08-20 21:53:28 +02:00
|
|
|
migrations, using the `MigrationsTestCase` test class. This system is
|
2018-05-21 18:56:45 +02:00
|
|
|
inspired by [a great blog post][django-migration-test-blog-post] on
|
|
|
|
the subject.
|
|
|
|
|
|
|
|
We have integrated this system with our test framework so that if you
|
|
|
|
use the `use_db_models` decorator, you can use some helper methods
|
|
|
|
from `test_classes.py` and friends from inside the tests (which is
|
|
|
|
normally not possible in Django's migrations framework).
|
|
|
|
|
|
|
|
If you find yourself writing logic in a `RunPython` migration, we
|
2021-08-20 21:53:28 +02:00
|
|
|
highly recommend adding a test using this framework. We may end up
|
2018-05-21 18:56:45 +02:00
|
|
|
deleting the test later (they can get slow once they are many
|
|
|
|
migrations away from current), but it can help prevent disaster where
|
|
|
|
an incorrect migration messes up a database in a way that's impossible
|
|
|
|
to undo without going to backups.
|
|
|
|
|
|
|
|
[django-migration-test-blog-post]: https://www.caktusgroup.com/blog/2016/02/02/writing-unit-tests-django-migrations/
|
2024-05-24 16:57:31 +02:00
|
|
|
[migrations-non-atomic]: https://docs.djangoproject.com/en/5.0/howto/writing-migrations/#non-atomic-migrations
|
2020-01-17 09:00:09 +01:00
|
|
|
|
|
|
|
## Schema and initial data changes
|
|
|
|
|
|
|
|
If you follow the processes described above, `tools/provision` and
|
|
|
|
`tools/test-backend` should detect any changes to the declared
|
|
|
|
migrations and run migrations on (`./manage.py migrate`) or rebuild
|
2020-03-17 13:57:10 +01:00
|
|
|
the relevant database automatically as appropriate.
|
2020-01-17 09:00:09 +01:00
|
|
|
|
2024-03-23 09:05:12 +01:00
|
|
|
Notably, both `manage.py migrate` (`git grep post_migrate.connect` for
|
|
|
|
details) and restarting `tools/run-dev` will flush `memcached`, so you
|
|
|
|
shouldn't have to worry about cached objects from a previous database
|
|
|
|
schema.
|
2020-04-21 22:03:12 +02:00
|
|
|
|
2024-03-23 09:05:12 +01:00
|
|
|
While developing migrations, you may accidentally corrupt your
|
|
|
|
databases while debugging your new code. You can always rebuild these
|
|
|
|
databases from scratch:
|
2020-04-21 22:03:12 +02:00
|
|
|
|
2024-03-23 09:05:12 +01:00
|
|
|
- Use `tools/rebuild-test-database` to rebuild the database
|
|
|
|
used for `test-backend` and other automated tests.
|
|
|
|
|
|
|
|
- Use `tools/rebuild-dev-database` to rebuild the database
|
|
|
|
used in [manual testing](../development/using.md).
|