diff --git a/crates/cuddle-ci/src/rust_service/sqlx.rs b/crates/cuddle-ci/src/rust_service/sqlx.rs index ebd4d0f..b9e7631 100644 --- a/crates/cuddle-ci/src/rust_service/sqlx.rs +++ b/crates/cuddle-ci/src/rust_service/sqlx.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::{path::PathBuf, sync::Arc}; use async_trait::async_trait; use dagger_sdk::Container; @@ -9,22 +9,51 @@ use super::RustService; pub struct Sqlx { client: dagger_sdk::Query, + migration_path: Option, } impl Sqlx { 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) -> Self { + self.migration_path = Some(migration_path.into()); + + self } } #[async_trait] impl DaggerMiddleware for Sqlx { async fn handle(&self, container: Container) -> eyre::Result { - let src = self.client.host().directory(".sqlx/"); + let container = if std::path::PathBuf::from(".sqlx/").exists() { + let src = self.client.host().directory(".sqlx/"); - Ok(container - .with_directory(".sqlx", src) - .with_env_variable("SQLX_OFFLINE", "true")) + container + .with_directory(".sqlx", src) + .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 { self } + + fn with_sqlx_migrations(&mut self, path: impl Into) -> &mut Self { + self + } } impl SqlxExt for RustService { @@ -42,4 +75,12 @@ impl SqlxExt for RustService { self } + + fn with_sqlx_migrations(&mut self, path: impl Into) -> &mut Self { + self.with_stage(super::RustServiceStage::BeforeBuild(Arc::new( + Sqlx::new(self.client.clone()).with_migration_path(path), + ))); + + self + } }