use std::sync::Arc;
use gaman_core::{
BoxFuture, DatabaseTrackingStore, EmbeddedMigrations, MigrationRunner, MigrationStore,
StoreError,
};
use crate::conf::Config;
use crate::migration_store::validate_embedded_directory;
mod lazy_executor;
pub use crate::migration_store::{DirectoryMigrationStore, EmbeddedMigrationStore};
pub use lazy_executor::LazyExecutor;
#[derive(Debug, thiserror::Error)]
pub enum NativeRunnerError {
#[error(
"migrations dir mismatch: config has '{configured}', embedded was compiled from '{embedded}'"
)]
EmbeddedDirectoryMismatch {
configured: String,
embedded: String,
},
#[error("cannot resolve current directory: {0}")]
CurrentDirectory(String),
}
pub struct NativeRunnerFactory {
config: Config,
migrations: NativeMigrationStore,
}
impl NativeRunnerFactory {
pub fn from_directory(config: Config) -> Self {
let migrations =
NativeMigrationStore::Directory(DirectoryMigrationStore::new(&config.migrations_dir));
Self { config, migrations }
}
pub fn from_store(config: Config, migrations: Arc<dyn MigrationStore>) -> Self {
Self {
config,
migrations: NativeMigrationStore::Custom(migrations),
}
}
pub fn from_embedded(
config: Config,
migrations: &'static EmbeddedMigrations,
) -> Result<Self, NativeRunnerError> {
validate_embedded_directory(&config.migrations_dir, migrations.dir)?;
Ok(Self {
migrations: NativeMigrationStore::Embedded(EmbeddedMigrationStore::new(
migrations,
DirectoryMigrationStore::new(config.migrations_dir.clone()),
)),
config,
})
}
pub fn config(&self) -> &Config {
&self.config
}
pub fn build(
self,
) -> MigrationRunner<NativeMigrationStore, DatabaseTrackingStore, LazyExecutor> {
let dialect = self.config.dialect;
MigrationRunner::new(
dialect,
self.migrations,
DatabaseTrackingStore,
LazyExecutor::new(self.config),
)
}
}
#[derive(Clone)]
pub enum NativeMigrationStore {
Directory(DirectoryMigrationStore),
Embedded(EmbeddedMigrationStore),
Custom(Arc<dyn MigrationStore>),
}
impl MigrationStore for NativeMigrationStore {
fn load_all<'a>(&'a self) -> BoxFuture<'a, Result<Vec<gaman_core::Migration>, StoreError>> {
match self {
Self::Directory(store) => store.load_all(),
Self::Embedded(store) => store.load_all(),
Self::Custom(store) => store.load_all(),
}
}
fn save<'a>(
&'a self,
migration: &'a gaman_core::Migration,
) -> BoxFuture<'a, Result<(), StoreError>> {
match self {
Self::Directory(store) => store.save(migration),
Self::Embedded(store) => store.save(migration),
Self::Custom(store) => store.save(migration),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use gaman_core::Dialect;
static CHILD: EmbeddedMigrations = EmbeddedMigrations {
files: &[
("0001_auth.yaml", "operations: []\n"),
(
"0002_profile.yaml",
"dependencies:\n- 0001_auth\noperations: []\n",
),
],
dir: "migrations/auth",
children: &[],
};
static TREE: EmbeddedMigrations = EmbeddedMigrations {
files: &[("0001_root.yaml", "operations: []\n")],
dir: "migrations",
children: &[("auth", &CHILD)],
};
static DUPLICATE: EmbeddedMigrations = EmbeddedMigrations {
files: &[],
dir: "migrations",
children: &[("auth", &CHILD), ("auth", &CHILD)],
};
#[tokio::test]
async fn embedded_store_qualifies_child_history() {
let directory = tempfile::tempdir().expect("temporary migration directory");
let store =
EmbeddedMigrationStore::new(&TREE, DirectoryMigrationStore::new(directory.path()));
let migrations = store.load_all().await.expect("load embedded tree");
let ids = migrations
.iter()
.map(|migration| migration.id.as_str())
.collect::<Vec<_>>();
assert_eq!(
ids,
vec!["0001_root", "auth/0001_auth", "auth/0002_profile"]
);
assert_eq!(migrations[2].dependencies, vec!["auth/0001_auth"]);
}
#[tokio::test]
async fn embedded_store_rejects_duplicate_ids() {
let directory = tempfile::tempdir().expect("temporary migration directory");
let store =
EmbeddedMigrationStore::new(&DUPLICATE, DirectoryMigrationStore::new(directory.path()));
let error = store.load_all().await.expect_err("duplicate IDs must fail");
assert!(
error
.to_string()
.contains("duplicate embedded migration id")
);
}
#[test]
fn embedded_factory_rejects_directory_mismatch() {
let config = Config::new(
"sqlite::memory:".to_string(),
"different-migrations".into(),
"schema.yaml".into(),
Dialect::Sqlite,
);
let error = match NativeRunnerFactory::from_embedded(config, &TREE) {
Ok(_) => panic!("directory mismatch must fail"),
Err(error) => error,
};
assert!(error.to_string().contains("migrations dir mismatch"));
}
}