|
| 1 | +use crate::{ |
| 2 | + async_trait, |
| 3 | + data::{Document, DocumentProcessor, Handler}, |
| 4 | +}; |
| 5 | +use clap::Parser; |
| 6 | +use sea_orm::DbErr; |
| 7 | +use sea_orm_migration::{MigrationName, MigrationTrait, SchemaManager}; |
| 8 | +use std::{ffi::OsString, sync::LazyLock}; |
| 9 | +use trustify_module_storage::{config::StorageConfig, service::dispatch::DispatchBackend}; |
| 10 | + |
| 11 | +pub struct MigrationWithData<M> |
| 12 | +where |
| 13 | + M: MigrationTraitWithData, |
| 14 | +{ |
| 15 | + pub storage: DispatchBackend, |
| 16 | + pub migration: M, |
| 17 | +} |
| 18 | + |
| 19 | +static STORAGE: LazyLock<DispatchBackend> = LazyLock::new(init_storage); |
| 20 | + |
| 21 | +#[allow(clippy::expect_used)] |
| 22 | +fn init_storage() -> DispatchBackend { |
| 23 | + // create from env-vars only |
| 24 | + let config = StorageConfig::parse_from::<_, OsString>(vec![]); |
| 25 | + |
| 26 | + tokio::runtime::Builder::new_current_thread() |
| 27 | + .enable_all() |
| 28 | + .build() |
| 29 | + .expect("failed to build runtime") |
| 30 | + .block_on(async { |
| 31 | + config |
| 32 | + .into_storage(false) |
| 33 | + .await |
| 34 | + .expect("Failed to create storage") |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +impl<M> MigrationWithData<M> |
| 39 | +where |
| 40 | + M: MigrationTraitWithData, |
| 41 | +{ |
| 42 | + #[allow(clippy::expect_used)] |
| 43 | + pub fn new(migration: M) -> Self { |
| 44 | + Self { |
| 45 | + storage: STORAGE.clone(), |
| 46 | + migration, |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl<M> From<M> for MigrationWithData<M> |
| 52 | +where |
| 53 | + M: MigrationTraitWithData, |
| 54 | +{ |
| 55 | + fn from(value: M) -> Self { |
| 56 | + MigrationWithData::new(value) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +pub struct SchemaDataManager<'c> { |
| 61 | + pub manager: &'c SchemaManager<'c>, |
| 62 | + storage: &'c DispatchBackend, |
| 63 | +} |
| 64 | + |
| 65 | +impl<'c> SchemaDataManager<'c> { |
| 66 | + pub fn new(manager: &'c SchemaManager<'c>, storage: &'c DispatchBackend) -> Self { |
| 67 | + Self { manager, storage } |
| 68 | + } |
| 69 | + |
| 70 | + pub async fn process<D>(&self, f: impl Handler<D>) -> Result<(), DbErr> |
| 71 | + where |
| 72 | + D: Document, |
| 73 | + { |
| 74 | + self.manager.process(self.storage, f).await |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +#[async_trait::async_trait] |
| 79 | +pub trait MigrationTraitWithData { |
| 80 | + async fn up(&self, manager: &SchemaDataManager) -> Result<(), DbErr>; |
| 81 | + async fn down(&self, manager: &SchemaDataManager) -> Result<(), DbErr>; |
| 82 | +} |
| 83 | + |
| 84 | +#[async_trait::async_trait] |
| 85 | +impl<M> MigrationTrait for MigrationWithData<M> |
| 86 | +where |
| 87 | + M: MigrationTraitWithData + MigrationName + Send + Sync, |
| 88 | +{ |
| 89 | + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { |
| 90 | + self.migration |
| 91 | + .up(&SchemaDataManager::new(manager, &self.storage)) |
| 92 | + .await |
| 93 | + } |
| 94 | + |
| 95 | + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { |
| 96 | + self.migration |
| 97 | + .down(&SchemaDataManager::new(manager, &self.storage)) |
| 98 | + .await |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl<M> MigrationName for MigrationWithData<M> |
| 103 | +where |
| 104 | + M: MigrationTraitWithData + MigrationName + Send + Sync, |
| 105 | +{ |
| 106 | + fn name(&self) -> &str { |
| 107 | + self.migration.name() |
| 108 | + } |
| 109 | +} |
0 commit comments