haste_repository/pg/
migrate.rs

1use haste_fhir_model::r4::generated::terminology::IssueType;
2use haste_fhir_operation_error::OperationOutcomeError;
3use sqlx;
4
5use crate::admin::Migrate;
6
7impl Migrate for super::PGConnection {
8    async fn migrate(&self) -> Result<(), OperationOutcomeError> {
9        match self {
10            super::PGConnection::Pool(pool, _) => {
11                sqlx::migrate!("./pg-migrations")
12                    .run(pool)
13                    .await
14                    .map_err(|e| {
15                        OperationOutcomeError::fatal(
16                            IssueType::Exception(None),
17                            format!("Failed to migrate repository schema: {}", e),
18                        )
19                    })?;
20                Ok(())
21            }
22            super::PGConnection::Transaction(_, _) => {
23                return Err(OperationOutcomeError::fatal(
24                    IssueType::Exception(None),
25                    "Cannot run migrations in a transaction.".to_string(),
26                ));
27            }
28        }
29    }
30}