use async_trait::async_trait;
use std::path::PathBuf;
#[derive(Debug, Clone)]
pub struct MigrationInfo {
pub module: String,
pub path: PathBuf,
pub count: Option<usize>,
}
#[derive(Debug, Clone)]
pub struct SeedInfo {
pub module: String,
pub path: PathBuf,
}
#[async_trait]
pub trait BackboneModule: Send + Sync {
fn name(&self) -> &'static str;
fn version(&self) -> &'static str;
fn dependencies(&self) -> Vec<&'static str> {
vec![]
}
fn migrations_path(&self) -> Option<PathBuf>;
fn seeds_path(&self) -> Option<PathBuf> {
None
}
async fn on_boot(&self) -> anyhow::Result<()> {
Ok(())
}
async fn on_shutdown(&self) -> anyhow::Result<()> {
Ok(())
}
async fn health_check(&self) -> bool {
true
}
fn migration_info(&self) -> Option<MigrationInfo> {
self.migrations_path().map(|path| MigrationInfo {
module: self.name().to_string(),
path,
count: None,
})
}
fn seed_info(&self) -> Option<SeedInfo> {
self.seeds_path().map(|path| SeedInfo {
module: self.name().to_string(),
path,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
struct TestModule;
impl BackboneModule for TestModule {
fn name(&self) -> &'static str {
"test"
}
fn version(&self) -> &'static str {
"0.1.0"
}
fn dependencies(&self) -> Vec<&'static str> {
vec!["core"]
}
fn migrations_path(&self) -> Option<PathBuf> {
Some(PathBuf::from("libs/modules/test/migrations"))
}
fn seeds_path(&self) -> Option<PathBuf> {
Some(PathBuf::from("libs/modules/test/migrations/seeds"))
}
}
#[test]
fn test_module_metadata() {
let module = TestModule;
assert_eq!(module.name(), "test");
assert_eq!(module.version(), "0.1.0");
assert_eq!(module.dependencies(), vec!["core"]);
}
#[test]
fn test_migration_info() {
let module = TestModule;
let info = module.migration_info();
assert!(info.is_some());
let info = info.unwrap();
assert_eq!(info.module, "test");
assert_eq!(info.path, PathBuf::from("libs/modules/test/migrations"));
}
#[test]
fn test_seed_info() {
let module = TestModule;
let info = module.seed_info();
assert!(info.is_some());
let info = info.unwrap();
assert_eq!(info.module, "test");
assert_eq!(info.path, PathBuf::from("libs/modules/test/migrations/seeds"));
}
}