Merge pull request #127 from gbw/fix/always-apply-schema-on-startup
fix: always apply database schema on startup
This commit is contained in:
+10
-30
@@ -1,6 +1,6 @@
|
||||
use crate::common::config::AppConfig;
|
||||
use anyhow::Result;
|
||||
use sqlx::{PgPool, Row, postgres::PgPoolOptions};
|
||||
use sqlx::{PgPool, postgres::PgPoolOptions};
|
||||
use std::time::Duration;
|
||||
|
||||
pub async fn create_database_pool(config: &AppConfig) -> Result<PgPool> {
|
||||
@@ -37,25 +37,16 @@ pub async fn create_database_pool(config: &AppConfig) -> Result<PgPool> {
|
||||
Ok(_) => {
|
||||
tracing::info!("PostgreSQL connection established successfully");
|
||||
|
||||
if !tables_exist(&pool).await {
|
||||
tracing::warn!("Database tables do not exist. Auto-applying schema...");
|
||||
if let Err(e) = apply_schema(&pool).await {
|
||||
return Err(anyhow::anyhow!(
|
||||
"Database schema could not be applied: {}. \
|
||||
Run manually: psql -f db/schema.sql",
|
||||
e
|
||||
));
|
||||
}
|
||||
|
||||
// Verify tables were actually created
|
||||
if !tables_exist(&pool).await {
|
||||
return Err(anyhow::anyhow!(
|
||||
"Database schema was applied but tables still missing. \
|
||||
Check db/schema.sql for errors."
|
||||
));
|
||||
}
|
||||
tracing::info!("Database schema applied and verified successfully");
|
||||
// Always apply schema - it's idempotent (uses IF NOT EXISTS and CREATE OR REPLACE)
|
||||
tracing::info!("Applying database schema...");
|
||||
if let Err(e) = apply_schema(&pool).await {
|
||||
return Err(anyhow::anyhow!(
|
||||
"Database schema could not be applied: {}. \
|
||||
Run manually: psql -f db/schema.sql",
|
||||
e
|
||||
));
|
||||
}
|
||||
tracing::info!("Database schema applied successfully");
|
||||
|
||||
return Ok(pool);
|
||||
}
|
||||
@@ -91,17 +82,6 @@ pub async fn create_database_pool(config: &AppConfig) -> Result<PgPool> {
|
||||
))
|
||||
}
|
||||
|
||||
/// Check whether the core auth tables exist in the database.
|
||||
async fn tables_exist(pool: &PgPool) -> bool {
|
||||
sqlx::query(
|
||||
"SELECT EXISTS (SELECT 1 FROM pg_tables WHERE schemaname = 'auth' AND tablename = 'users')",
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map(|row| row.get::<bool, _>(0))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
/// Apply the embedded schema.sql to the database.
|
||||
/// First tries `raw_sql` (simple query protocol). If that fails, falls back
|
||||
/// to splitting the SQL into individual statements and executing them one by one.
|
||||
|
||||
Reference in New Issue
Block a user