docbox_management/tenant/
get_pending_tenant_storage_migrations.rs1use crate::database::{DatabaseProvider, close_pool_on_drop};
2use docbox_core::{
3 database::{
4 DbErr, ROOT_DATABASE_NAME,
5 models::{tenant::Tenant, tenant_migration::TenantMigration},
6 },
7 storage::{StorageLayerError, StorageLayerFactory},
8 tenant::tenant_options_ext::TenantOptionsExt,
9};
10use thiserror::Error;
11
12#[derive(Debug, Error)]
13pub enum GetPendingTenantMigrationsError {
14 #[error(transparent)]
15 Database(#[from] DbErr),
16
17 #[error("failed to apply migration: {0}")]
18 GetPendingMigrations(StorageLayerError),
19}
20
21#[tracing::instrument(skip(db_provider, storage_factory))]
22pub async fn get_pending_tenant_storage_migrations(
23 db_provider: &impl DatabaseProvider,
24 storage_factory: &StorageLayerFactory,
25 tenant: &Tenant,
26) -> Result<Vec<String>, GetPendingTenantMigrationsError> {
27 let root_db = db_provider.connect(ROOT_DATABASE_NAME).await?;
29 let _guard = close_pool_on_drop(&root_db);
30
31 let applied_migrations =
32 TenantMigration::find_by_tenant(&root_db, tenant.id, &tenant.env).await?;
33 let storage = storage_factory.create_layer(tenant.storage_layer_options());
34 let migrations = storage
35 .get_pending_migrations(
36 applied_migrations
37 .into_iter()
38 .map(|value| value.name)
39 .collect(),
40 )
41 .await
42 .map_err(GetPendingTenantMigrationsError::GetPendingMigrations)?;
43
44 Ok(migrations)
45}