fix postgres database ... already exists error

This commit is contained in:
2025-07-22 18:11:41 +02:00
parent 2c26002507
commit 39676fe94a

View File

@@ -75,11 +75,20 @@ impl Postgres {
.await .await
.context("Failed to connect to the PostgreSQL database")?; .context("Failed to connect to the PostgreSQL database")?;
// If this fails it's probably because the database already exists, which is exactly what match sqlx::query("SELECT datname FROM pg_database WHERE datname = $1")
// we want .bind(&config.database_name)
let _ = sqlx::query(&format!("CREATE DATABASE {}", config.database_name)) .fetch_one(&mut connection)
.execute(&mut connection) .await
.await; {
Ok(_) => (),
Err(sqlx::Error::RowNotFound) => {
sqlx::query(&format!("CREATE DATABASE {}", config.database_name))
.execute(&mut connection)
.await?;
}
Err(e) => return Err(e.into()),
};
connection.close().await?; connection.close().await?;
let pool = PgPoolOptions::new() let pool = PgPoolOptions::new()