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
.context("Failed to connect to the PostgreSQL database")?;
// If this fails it's probably because the database already exists, which is exactly what
// we want
let _ = sqlx::query(&format!("CREATE DATABASE {}", config.database_name))
.execute(&mut connection)
.await;
match sqlx::query("SELECT datname FROM pg_database WHERE datname = $1")
.bind(&config.database_name)
.fetch_one(&mut connection)
.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?;
let pool = PgPoolOptions::new()