Skip to content

Commit

Permalink
feat(rust): don't run postgres migrations automatically on startup
Browse files Browse the repository at this point in the history
except the first time for tests
  • Loading branch information
etorreborre committed Jan 14, 2025
1 parent b44b18c commit 1eff413
Showing 1 changed file with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use sqlx::pool::PoolOptions;
use sqlx::{Any, ConnectOptions, Pool};
use sqlx_core::any::AnyConnection;
use sqlx_core::executor::Executor;
use sqlx_core::row::Row;
use tempfile::NamedTempFile;
use tokio_retry::strategy::{jitter, FixedInterval};
use tokio_retry::Retry;
Expand Down Expand Up @@ -196,9 +197,18 @@ impl SqlxDatabase {
})
.await?;

if let Some(migration_set) = migration_set {
let migrator = migration_set.create_migrator()?;
migrator.migrate(&database.pool).await?;
let database_schema_already_created: bool = sqlx::query("SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'identity')")
.fetch_one(&*database.pool)
.await.into_core()?.get(0);

// Only run the postgres migrations if the database has never been created.
// This is mostly for tests. In production the database schema must be created separately
// during the first deployment.
if !database_schema_already_created {
if let Some(migration_set) = migration_set {
let migrator = migration_set.create_migrator()?;
migrator.migrate(&database.pool).await?;
}
}

database
Expand Down

0 comments on commit 1eff413

Please sign in to comment.