|
| 1 | +//! PostgreSQL version constants and utilities. |
| 2 | +//! |
| 3 | +//! This module provides version constants for supported PostgreSQL versions and macros |
| 4 | +//! for ergonomic version comparison. Version numbers follow PostgreSQL's internal format: |
| 5 | +//! `MAJOR * 10000 + MINOR * 100 + PATCH`. |
| 6 | +//! |
| 7 | +//! # Supported Versions |
| 8 | +//! |
| 9 | +//! ETL officially supports PostgreSQL versions 14, 15, 16, and 17. |
| 10 | +
|
| 11 | +use std::num::NonZeroI32; |
| 12 | + |
| 13 | +pub const POSTGRES_14: i32 = 140000; |
| 14 | +pub const POSTGRES_15: i32 = 150000; |
| 15 | +pub const POSTGRES_16: i32 = 160000; |
| 16 | +pub const POSTGRES_17: i32 = 170000; |
| 17 | + |
| 18 | +/// Returns [`true`] if the server version meets or exceeds the required version. |
| 19 | +/// |
| 20 | +/// This function handles [`None`] server versions by returning [`false`], making it |
| 21 | +/// safe to use in contexts where version information might not be available. |
| 22 | +pub fn meets_version(server_version: Option<NonZeroI32>, required_version: i32) -> bool { |
| 23 | + server_version.is_some_and(|v| v.get() >= required_version) |
| 24 | +} |
| 25 | + |
| 26 | +/// Checks if the server version meets or exceeds the required version. |
| 27 | +/// |
| 28 | +/// This macro provides ergonomic version checking by accepting various input types |
| 29 | +/// for the server version (Option<NonZeroI32>, NonZeroI32, i32) and comparing against |
| 30 | +/// version constants. |
| 31 | +#[macro_export] |
| 32 | +macro_rules! requires_version { |
| 33 | + ($server_version:expr, $required:expr) => { |
| 34 | + $crate::version::meets_version($server_version, $required) |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +/// Checks if the server version is below the specified version. |
| 39 | +/// |
| 40 | +/// This macro is useful for conditional logic when features are not available |
| 41 | +/// in older PostgreSQL versions. |
| 42 | +#[macro_export] |
| 43 | +macro_rules! below_version { |
| 44 | + ($server_version:expr, $required:expr) => { |
| 45 | + !$crate::version::meets_version($server_version, $required) |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +#[cfg(test)] |
| 50 | +mod tests { |
| 51 | + use super::*; |
| 52 | + |
| 53 | + #[test] |
| 54 | + fn test_version_constants() { |
| 55 | + assert_eq!(POSTGRES_14, 140000); |
| 56 | + assert_eq!(POSTGRES_15, 150000); |
| 57 | + assert_eq!(POSTGRES_16, 160000); |
| 58 | + assert_eq!(POSTGRES_17, 170000); |
| 59 | + } |
| 60 | + |
| 61 | + #[test] |
| 62 | + fn test_meets_version_with_some() { |
| 63 | + let version = NonZeroI32::new(150500); |
| 64 | + assert!(meets_version(version, POSTGRES_14)); |
| 65 | + assert!(meets_version(version, POSTGRES_15)); |
| 66 | + assert!(!meets_version(version, POSTGRES_16)); |
| 67 | + assert!(!meets_version(version, POSTGRES_17)); |
| 68 | + } |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn test_meets_version_with_none() { |
| 72 | + assert!(!meets_version(None, POSTGRES_14)); |
| 73 | + assert!(!meets_version(None, POSTGRES_15)); |
| 74 | + assert!(!meets_version(None, POSTGRES_16)); |
| 75 | + } |
| 76 | + |
| 77 | + #[test] |
| 78 | + fn test_meets_version_exact_match() { |
| 79 | + let version = NonZeroI32::new(POSTGRES_15); |
| 80 | + assert!(meets_version(version, POSTGRES_15)); |
| 81 | + } |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn test_requires_version_macro() { |
| 85 | + let version = NonZeroI32::new(160200); |
| 86 | + assert!(requires_version!(version, POSTGRES_14)); |
| 87 | + assert!(requires_version!(version, POSTGRES_15)); |
| 88 | + assert!(requires_version!(version, POSTGRES_16)); |
| 89 | + assert!(!requires_version!(version, POSTGRES_17)); |
| 90 | + } |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn test_below_version_macro() { |
| 94 | + let version = NonZeroI32::new(140800); |
| 95 | + assert!(!below_version!(version, POSTGRES_14)); |
| 96 | + assert!(below_version!(version, POSTGRES_15)); |
| 97 | + assert!(below_version!(version, POSTGRES_16)); |
| 98 | + assert!(below_version!(version, POSTGRES_17)); |
| 99 | + } |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn test_requires_version_with_none() { |
| 103 | + let version: Option<NonZeroI32> = None; |
| 104 | + assert!(!requires_version!(version, POSTGRES_14)); |
| 105 | + } |
| 106 | +} |
0 commit comments