feat: with migrations as well
Some checks failed
continuous-integration/drone/push Build is failing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2023-12-30 22:18:13 +01:00
parent 97b70164d1
commit cf05cf4228
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912

View File

@ -1,4 +1,4 @@
use std::sync::Arc; use std::{path::PathBuf, sync::Arc};
use async_trait::async_trait; use async_trait::async_trait;
use dagger_sdk::Container; use dagger_sdk::Container;
@ -9,22 +9,51 @@ use super::RustService;
pub struct Sqlx { pub struct Sqlx {
client: dagger_sdk::Query, client: dagger_sdk::Query,
migration_path: Option<PathBuf>,
} }
impl Sqlx { impl Sqlx {
pub fn new(client: dagger_sdk::Query) -> Self { pub fn new(client: dagger_sdk::Query) -> Self {
Self { client } Self {
client,
migration_path: None,
}
}
pub fn with_migration_path(mut self, migration_path: impl Into<PathBuf>) -> Self {
self.migration_path = Some(migration_path.into());
self
} }
} }
#[async_trait] #[async_trait]
impl DaggerMiddleware for Sqlx { impl DaggerMiddleware for Sqlx {
async fn handle(&self, container: Container) -> eyre::Result<Container> { async fn handle(&self, container: Container) -> eyre::Result<Container> {
let container = if std::path::PathBuf::from(".sqlx/").exists() {
let src = self.client.host().directory(".sqlx/"); let src = self.client.host().directory(".sqlx/");
Ok(container container
.with_directory(".sqlx", src) .with_directory(".sqlx", src)
.with_env_variable("SQLX_OFFLINE", "true")) .with_env_variable("SQLX_OFFLINE", "true")
} else {
container
};
let container = if let Some(migration_path) = &self.migration_path {
container
.with_directory(
"/mnt/sqlx/migrations",
self.client
.host()
.directory(migration_path.display().to_string()),
)
.with_env_variable("NEFARIOUS_DB_MIGRATION_PATH", "/mnt/sqlx/migrations")
} else {
container
};
Ok(container)
} }
} }
@ -32,6 +61,10 @@ pub trait SqlxExt {
fn with_sqlx(&mut self) -> &mut Self { fn with_sqlx(&mut self) -> &mut Self {
self self
} }
fn with_sqlx_migrations(&mut self, path: impl Into<PathBuf>) -> &mut Self {
self
}
} }
impl SqlxExt for RustService { impl SqlxExt for RustService {
@ -42,4 +75,12 @@ impl SqlxExt for RustService {
self self
} }
fn with_sqlx_migrations(&mut self, path: impl Into<PathBuf>) -> &mut Self {
self.with_stage(super::RustServiceStage::BeforeBuild(Arc::new(
Sqlx::new(self.client.clone()).with_migration_path(path),
)));
self
}
} }