Skip to content

Commit

Permalink
feat(server): check PG version before running migrations (#1868)
Browse files Browse the repository at this point in the history
  • Loading branch information
xvello authored Mar 12, 2024
1 parent 3e2e729 commit c330636
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atuin-server-postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
21 changes: 21 additions & 0 deletions atuin-server-postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c330636

Please sign in to comment.