nodedb_types/pg_compat.rs
1// SPDX-License-Identifier: Apache-2.0
2
3//! PostgreSQL wire-compatibility version constants. NodeDB advertises
4//! compatibility with a fixed PostgreSQL major so libpq-based clients gate
5//! features correctly. These are the single source of truth for `version()`,
6//! the `server_version_num` runtime parameter, and startup ParameterStatus.
7
8/// PostgreSQL version NodeDB advertises compatibility with (`server_version` numeric form source).
9pub const PG_COMPAT_VERSION: &str = "15.0";
10
11/// `server_version_num` value: MAJOR*10000 + MINOR*100 + PATCH (15.0 -> 150000).
12pub const PG_COMPAT_VERSION_NUM: &str = "150000";
13
14/// PostgreSQL-compatible `server_version` value announced during pgwire
15/// startup and exposed through runtime settings. libpq parses the leading
16/// numeric version; the suffix preserves NodeDB's build identity.
17pub fn server_version_string(nodedb_version: &str) -> String {
18 format!("{PG_COMPAT_VERSION} (NodeDB {nodedb_version})")
19}
20
21/// The string returned by the SQL `version()` function, mirroring
22/// PostgreSQL's `"PostgreSQL <ver> on <triple> ..."` shape so clients that
23/// parse the leading `"PostgreSQL <major>"` succeed.
24pub fn version_string() -> String {
25 format!(
26 "PostgreSQL {PG_COMPAT_VERSION} (NodeDB) on {}",
27 std::env::consts::ARCH
28 )
29}
30
31#[cfg(test)]
32mod tests {
33 use super::*;
34
35 #[test]
36 fn server_version_starts_numeric_and_preserves_nodedb_build() {
37 assert_eq!(server_version_string("0.4.0"), "15.0 (NodeDB 0.4.0)");
38 }
39
40 #[test]
41 fn version_string_starts_with_postgres_major() {
42 assert!(version_string().starts_with("PostgreSQL 15"));
43 }
44
45 #[test]
46 fn version_num_parses_and_meets_minimum() {
47 let num: i64 = PG_COMPAT_VERSION_NUM.parse().expect("valid integer");
48 assert!(num >= 100000);
49 }
50}