use tern::{ContextOptions, SqlxPgExecutor, error::TernResult};
use tern::{MigrationContext, MigrationSource};
#[derive(MigrationContext, MigrationSource)]
#[tern(source = "migrations", table = "example_schema_history")]
pub struct ExampleContext {
#[tern(executor_via)]
pub executor: SqlxPgExecutor,
pub env: GetEnvVar,
}
impl ExampleContext {
pub async fn new(db_url: &str) -> TernResult<Self> {
let executor = SqlxPgExecutor::new(db_url).await?;
Ok(Self { executor, env: GetEnvVar })
}
pub async fn max_value(&self) -> anyhow::Result<i64> {
let max_val: i64 = sqlx::query_scalar("SELECT max(x) FROM dmd_test;")
.fetch_optional(&self.executor.pool())
.await?
.unwrap_or_default();
Ok(max_val)
}
}
pub struct GetEnvVar;
impl GetEnvVar {
pub fn get_var(&self, key: &str) -> Option<String> {
std::env::var(key).ok()
}
}
pub struct ExampleOptions;
impl ContextOptions for ExampleOptions {
type Ctx = ExampleContext;
async fn connect(&self, db_url: &str) -> TernResult<ExampleContext> {
ExampleContext::new(db_url).await
}
}