Expand description
Haste Health’s database migrations, with the Rust code some of them have, and what runs them.
A migration is a .sql file in a folder of sql/, one folder for each set
of tables: sql/repository, sql/search (the PG search index) and
sql/document-storage. sqlx applies the files of a folder in version order
and records each in the folder’s migrations table. The crates that read and
write the tables hold no migrations of their own.
Some work is more than SQL can do: putting a stored secret under another
key takes the server’s keys. A migration with such work is still a .sql
file in its folder, and its code is a step: a module named after the
migration, with an m in front since a module name cannot start with a
digit, in the module of its folder.
sql/repository/20270101120000_reencrypt_values.sql
src/repository/m20270101120000_reencrypt_values.rsrun runs a step when it comes to the step’s migration: after every
earlier migration, in the transaction that then applies the migration’s SQL
and records it. The step’s work, the SQL and the record commit together or
not at all, so a step runs once for a database, and a step that fails
leaves nothing behind but its migration, still to run.
This crate depends on whatever the steps need, the server included, so
nothing depends on it but the haste-health binary, whose admin migrate
runs the migrations: repository, search and document_storage.
When writing a step:
- Use SQL and row types of the step’s own, not the repository’s. A step runs against the schema as the earlier migrations leave it, which on a new database is not the schema of today.
- Keep it to what one transaction can do. Rewriting many rows is a job, between a migration that adds the new shape and one that drops the old.
- A step stays for as long as its migration does, since a new database runs every migration.
- Its migration can be applied without it, by sqlx’s CLI or
#[sqlx::test(migrations = ...)], and is then recorded without the step’s work. That is harmless on an empty database only.
Modules§
- document_
storage - The Postgres document storage backend’s migrations: the
.sqlfiles ofsql/document-storage. - repository
- The repository’s migrations: the
.sqlfiles ofsql/repository, with the steps of those that have code. - search
- The PG search index’s migrations: the
.sqlfiles ofsql/search.
Structs§
- Step
- The Rust code of a migration.
Constants§
- CONNECTIONS
- The connections
runneeds its pool to allow.
Functions§
- run
- Applies the migrations of
migratorthat the database ofpoollacks, in version order, each with its step if it has one.contextis what the steps are given.
Type Aliases§
- Step
Future - What the code of a step returns.