feat(server): check PG version before running migrations (#1868)

This commit is contained in:
Xavier Vello 2024-03-12 19:02:44 +01:00 committed by GitHub
parent 3e2e7292c0
commit c330636766
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 0 deletions

1
Cargo.lock generated
View File

@ -354,6 +354,7 @@ dependencies = [
"async-trait",
"atuin-common",
"atuin-server-database",
"eyre",
"futures-util",
"serde",
"sqlx",

View File

@ -13,6 +13,7 @@ repository = { workspace = true }
atuin-common = { path = "../atuin-common", version = "18.1.0" }
atuin-server-database = { path = "../atuin-server-database", version = "18.1.0" }
eyre = { workspace = true }
tracing = "0.1"
time = { workspace = true }
serde = { workspace = true }

View File

@ -16,6 +16,8 @@ use wrappers::{DbHistory, DbRecord, DbSession, DbUser};
mod wrappers;
const MIN_PG_VERSION: u32 = 14;
#[derive(Clone)]
pub struct Postgres {
pool: sqlx::Pool<sqlx::postgres::Postgres>,
@ -43,6 +45,25 @@ impl Database for Postgres {
.await
.map_err(fix_error)?;
// Call server_version_num to get the DB server's major version number
// The call returns None for servers older than 8.x.
let pg_major_version: u32 = pool
.acquire()
.await
.map_err(fix_error)?
.server_version_num()
.ok_or(DbError::Other(eyre::Report::msg(
"could not get PostgreSQL version",
)))?
/ 10000;
if pg_major_version < MIN_PG_VERSION {
return Err(DbError::Other(eyre::Report::msg(format!(
"unsupported PostgreSQL version {}, minimum required is {}",
pg_major_version, MIN_PG_VERSION
))));
}
sqlx::migrate!("./migrations")
.run(&pool)
.await