haste_migrations/document_storage.rs
1//! The Postgres document storage backend's migrations: the `.sql` files of
2//! `sql/document-storage`.
3//!
4//! The backend keeps its documents in the repository database unless it is
5//! given one of its own. There the migration also ties a document to its
6//! project, so deleting the project deletes its documents: run the
7//! repository's migrations first.
8
9use haste_fhir_operation_error::OperationOutcomeError;
10use sqlx::{PgPool, migrate::Migrator};
11
12/// Records which document storage migrations ran. Not sqlx's default table,
13/// which the repository's migrations use: the documents may share their
14/// database.
15const MIGRATIONS_TABLE: &str = "_document_storage_migrations";
16
17fn migrator() -> Migrator {
18 let mut migrator = sqlx::migrate!("./sql/document-storage");
19 migrator.dangerous_set_table_name(MIGRATIONS_TABLE);
20 migrator
21}
22
23/// Applies the document storage migrations that the database of `pool` lacks.
24/// None has a step.
25///
26/// # Errors
27///
28/// Returns an error if a migration fails (see [`crate::run`]).
29pub async fn migrate(pool: &PgPool) -> Result<(), OperationOutcomeError> {
30 crate::run(migrator(), pool, &[], &()).await
31}