From 73822945bfe62ff8b29fd3c999c3e7069c19f508 Mon Sep 17 00:00:00 2001 From: dal Date: Tue, 7 Jan 2025 22:21:28 -0700 Subject: [PATCH] Refactor Docker Compose and API for enhanced functionality and migration support - Simplified the API service build configuration in `docker-compose.yml` by consolidating the build context and Dockerfile path. - Added `diesel_migrations` dependency to `Cargo.toml` for database migration management. - Implemented database migration logic in `main.rs`, including error handling and logging for migration success or failure. - Introduced a new mail service in `supabase/docker-compose.yml` for handling SMTP, POP3, and web interface. - Removed version specification from `supabase/dev/docker-compose.dev.yml` for cleaner configuration. These changes improve the overall structure and functionality of the application, facilitating better database management and service orchestration. --- api/Cargo.toml | 1 + api/src/main.rs | 28 ++++++++++++++++++++++++++++ docker-compose.yml | 4 +--- supabase/dev/docker-compose.dev.yml | 2 -- supabase/docker-compose.yml | 8 ++++++++ 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/api/Cargo.toml b/api/Cargo.toml index ddf29a92a..391197450 100644 --- a/api/Cargo.toml +++ b/api/Cargo.toml @@ -88,6 +88,7 @@ tokio-postgres-rustls = "0.13" tokio-postgres = "0.7" futures-util = "0.3" rayon = "1.10.0" +diesel_migrations = "2.0.0" [profile.release] debug = false diff --git a/api/src/main.rs b/api/src/main.rs index de9324a62..fbaaa4ed8 100644 --- a/api/src/main.rs +++ b/api/src/main.rs @@ -9,12 +9,17 @@ use std::sync::Arc; use axum::{middleware, Extension, Router}; use buster_middleware::{auth::auth, cors::cors}; +use database::lib::get_pg_pool; +use diesel::{Connection, PgConnection}; +use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness}; use dotenv::dotenv; use rustls::crypto::ring; use tokio::sync::broadcast; use tower_http::{compression::CompressionLayer, trace::TraceLayer}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter}; +pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!(); + #[tokio::main] #[allow(unused)] async fn main() { @@ -46,6 +51,15 @@ async fn main() { return; } + tracing::info!("Running database migrations"); + + if let Err(e) = run_migrations().await { + tracing::error!("Failed to run database migrations: {}", e); + return; + } + + tracing::info!("Successfully ran database migrations"); + let protected_router = Router::new().nest("/api/v1", routes::protected_router()); let public_router = Router::new().route("/health", axum::routing::get(|| async { "OK" })); @@ -83,3 +97,17 @@ async fn main() { } } } + +async fn run_migrations() -> Result<(), anyhow::Error> { + let database_url = std::env::var("DATABASE_URL") + .map_err(|e| anyhow::anyhow!("Failed to get DATABASE_URL: {}", e))?; + + let mut connection = PgConnection::establish(&database_url) + .map_err(|e| anyhow::anyhow!("Failed to establish database connection: {}", e))?; + + connection + .run_pending_migrations(MIGRATIONS) + .map_err(|e| anyhow::anyhow!("Failed to run migrations: {}", e))?; + + Ok(()) +} diff --git a/docker-compose.yml b/docker-compose.yml index a5be2c11b..eef45f0ad 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,9 +14,7 @@ services: retries: 30 api: - build: - context: ./api - dockerfile: Dockerfile + build: ./api env_file: - .env ports: diff --git a/supabase/dev/docker-compose.dev.yml b/supabase/dev/docker-compose.dev.yml index ca19a0ad7..bd30dd97b 100644 --- a/supabase/dev/docker-compose.dev.yml +++ b/supabase/dev/docker-compose.dev.yml @@ -1,5 +1,3 @@ -version: "3.8" - services: studio: build: diff --git a/supabase/docker-compose.yml b/supabase/docker-compose.yml index 333e75781..d3a6e5636 100644 --- a/supabase/docker-compose.yml +++ b/supabase/docker-compose.yml @@ -8,6 +8,14 @@ name: supabase services: + mail: + container_name: supabase-mail + image: inbucket/inbucket:3.0.3 + ports: + - '2500:2500' # SMTP + - '9000:9000' # web interface + - '1100:1100' # POP3 + studio: container_name: supabase-studio image: supabase/studio:20241202-71e5240