diff --git a/src/db.rs b/src/db.rs index 35945f11..56e9c855 100644 --- a/src/db.rs +++ b/src/db.rs @@ -151,7 +151,7 @@ fn cert() { make_certificates(); } -pub async fn run_migrations(client: &DbClient) -> anyhow::Result<()> { +pub async fn run_migrations(client: &mut DbClient) -> anyhow::Result<()> { client .execute( "CREATE TABLE IF NOT EXISTS database_versions ( @@ -182,17 +182,19 @@ pub async fn run_migrations(client: &DbClient) -> anyhow::Result<()> { for (idx, migration) in MIGRATIONS.iter().enumerate() { if idx >= migration_idx { - client + let tx = client.transaction().await.context("Cannot create migration transactin")?; + tx .execute(*migration, &[]) .await .with_context(|| format!("executing {}th migration", idx))?; - client + tx .execute( "UPDATE database_versions SET migration_counter = $1", &[&(idx as i32 + 1)], ) .await .with_context(|| format!("updating migration counter to {}", idx))?; + tx.commit().await.context("Cannot commit migration transaction")?; } } diff --git a/src/main.rs b/src/main.rs index 2d59fc87..362977e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -243,7 +243,7 @@ async fn serve_req( async fn run_server(addr: SocketAddr) -> anyhow::Result<()> { let pool = db::ClientPool::new(); - db::run_migrations(&*pool.get().await) + db::run_migrations(&mut *pool.get().await) .await .context("database migrations")?;