Skip to main content

Crate haste_migrations

Crate haste_migrations 

Source
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.rs

run 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 .sql files of sql/document-storage.
repository
The repository’s migrations: the .sql files of sql/repository, with the steps of those that have code.
search
The PG search index’s migrations: the .sql files of sql/search.

Structs§

Step
The Rust code of a migration.

Constants§

CONNECTIONS
The connections run needs its pool to allow.

Functions§

run
Applies the migrations of migrator that the database of pool lacks, in version order, each with its step if it has one. context is what the steps are given.

Type Aliases§

StepFuture
What the code of a step returns.