Compare commits

..

5 Commits

Author SHA1 Message Date
charlesgauthereau 7fc4e59a4c chore(release): 0.1.2-rc.1 2026-01-08 09:52:24 +01:00
charlesgauthereau 766abe697a fix: postgres.rs methods and add tokio-postgres 2026-01-08 09:52:05 +01:00
charlesgauthereau a279993fa5 chore: Cargo.lock 2026-01-07 22:31:49 +01:00
charlesgauthereau bb3f254e6c chore(release): 0.1.1 2026-01-07 22:31:20 +01:00
charlesgauthereau 5d1841cdfe fix: psql error for ping method in postgres 2026-01-07 22:30:46 +01:00
7 changed files with 383 additions and 214 deletions
Vendored
BIN
View File
Binary file not shown.
+2 -2
View File
@@ -22,5 +22,5 @@ keywords:
- self-hosted
- portabase
license: Apache-2.0
version: 0.1.1-rc.8
date-released: "2026-01-07"
version: 0.1.2-rc.1
date-released: "2026-01-08"
Generated
+282 -2
View File
@@ -86,12 +86,27 @@ version = "2.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3"
[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
dependencies = [
"generic-array",
]
[[package]]
name = "bumpalo"
version = "3.19.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5dd9dc738b7a8311c7ade152424974d8115f2cdad61e8dab8dac9f2362298510"
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "bytes"
version = "1.11.0"
@@ -191,6 +206,15 @@ version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "cpufeatures"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280"
dependencies = [
"libc",
]
[[package]]
name = "crc32fast"
version = "1.5.0"
@@ -211,6 +235,27 @@ dependencies = [
"winnow 0.6.26",
]
[[package]]
name = "crypto-common"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "digest"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
"subtle",
]
[[package]]
name = "displaydoc"
version = "0.2.5"
@@ -259,6 +304,12 @@ dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "fallible-iterator"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7"
[[package]]
name = "fastrand"
version = "2.3.0"
@@ -379,6 +430,16 @@ dependencies = [
"slab",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getrandom"
version = "0.2.16"
@@ -437,6 +498,15 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "hmac"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e"
dependencies = [
"digest",
]
[[package]]
name = "http"
version = "1.4.0"
@@ -760,7 +830,7 @@ checksum = "3d0b95e02c851351f877147b7deea7b1afb1df71b63aa5f8270716e0c5720616"
dependencies = [
"bitflags",
"libc",
"redox_syscall",
"redox_syscall 0.7.0",
]
[[package]]
@@ -775,6 +845,15 @@ version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6373607a59f0be73a39b6fe456b8192fcc3585f602af20751600e974dd455e77"
[[package]]
name = "lock_api"
version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965"
dependencies = [
"scopeguard",
]
[[package]]
name = "log"
version = "0.4.29"
@@ -787,6 +866,16 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "112b39cec0b298b6c1999fee3e31427f74f676e4cb9879ed1a121b43661a4154"
[[package]]
name = "md-5"
version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf"
dependencies = [
"cfg-if",
"digest",
]
[[package]]
name = "memchr"
version = "2.7.6"
@@ -917,12 +1006,54 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "parking_lot"
version = "0.12.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1"
dependencies = [
"cfg-if",
"libc",
"redox_syscall 0.5.18",
"smallvec",
"windows-link",
]
[[package]]
name = "percent-encoding"
version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
[[package]]
name = "phf"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1562dc717473dbaa4c1f85a36410e03c047b2e7df7f45ee938fbef64ae7fadf"
dependencies = [
"phf_shared",
"serde",
]
[[package]]
name = "phf_shared"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e57fef6bc5981e38c2ce2d63bfa546861309f875b8a75f092d1d54ae2d64f266"
dependencies = [
"siphasher",
]
[[package]]
name = "pin-project-lite"
version = "0.2.16"
@@ -943,7 +1074,7 @@ checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
[[package]]
name = "portabase-agent"
version = "0.1.1-rc.7"
version = "0.1.1"
dependencies = [
"anyhow",
"async-trait",
@@ -964,11 +1095,41 @@ dependencies = [
"tempfile",
"thiserror 2.0.17",
"tokio",
"tokio-postgres",
"toml",
"tracing",
"tracing-subscriber",
]
[[package]]
name = "postgres-protocol"
version = "0.6.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbef655056b916eb868048276cfd5d6a7dea4f81560dfd047f97c8c6fe3fcfd4"
dependencies = [
"base64",
"byteorder",
"bytes",
"fallible-iterator",
"hmac",
"md-5",
"memchr",
"rand",
"sha2",
"stringprep",
]
[[package]]
name = "postgres-types"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef4605b7c057056dd35baeb6ac0c0338e4975b1f2bef0f65da953285eb007095"
dependencies = [
"bytes",
"fallible-iterator",
"postgres-protocol",
]
[[package]]
name = "potential_utf"
version = "0.1.4"
@@ -1120,6 +1281,15 @@ dependencies = [
"xxhash-rust",
]
[[package]]
name = "redox_syscall"
version = "0.5.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d"
dependencies = [
"bitflags",
]
[[package]]
name = "redox_syscall"
version = "0.7.0"
@@ -1310,6 +1480,12 @@ dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "security-framework"
version = "3.5.1"
@@ -1391,6 +1567,17 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbfa15b3dddfee50a0fff136974b3e1bde555604ba463834a7eb7deb6417705d"
[[package]]
name = "sha2"
version = "0.10.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]]
name = "sharded-slab"
version = "0.1.7"
@@ -1412,6 +1599,12 @@ version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e320a6c5ad31d271ad523dcf3ad13e2767ad8b1cb8f047f75a8aeaf8da139da2"
[[package]]
name = "siphasher"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56199f7ddabf13fe5074ce809e7d3f42b42ae711800501b5b16ea82ad029c39d"
[[package]]
name = "slab"
version = "0.4.11"
@@ -1440,6 +1633,17 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596"
[[package]]
name = "stringprep"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b4df3d392d81bd458a8a621b8bffbd2302a12ffe288a9d931670948749463b1"
dependencies = [
"unicode-bidi",
"unicode-normalization",
"unicode-properties",
]
[[package]]
name = "subtle"
version = "2.6.1"
@@ -1622,6 +1826,32 @@ dependencies = [
"syn",
]
[[package]]
name = "tokio-postgres"
version = "0.7.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b40d66d9b2cfe04b628173409368e58247e8eddbbd3b0e6c6ba1d09f20f6c9e"
dependencies = [
"async-trait",
"byteorder",
"bytes",
"fallible-iterator",
"futures-channel",
"futures-util",
"log",
"parking_lot",
"percent-encoding",
"phf",
"pin-project-lite",
"postgres-protocol",
"postgres-types",
"rand",
"socket2",
"tokio",
"tokio-util",
"whoami",
]
[[package]]
name = "tokio-rustls"
version = "0.26.4"
@@ -1792,18 +2022,45 @@ version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
[[package]]
name = "typenum"
version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "562d481066bde0658276a35467c4af00bdc6ee726305698a55b86e61d7ad82bb"
[[package]]
name = "unicase"
version = "2.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75b844d17643ee918803943289730bec8aac480150456169e647ed0b576ba539"
[[package]]
name = "unicode-bidi"
version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c1cb5db39152898a79168971543b1cb5020dff7fe43c8dc468b0885f5e29df5"
[[package]]
name = "unicode-ident"
version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
[[package]]
name = "unicode-normalization"
version = "0.1.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fd4f6878c9cb28d874b009da9e8d183b5abc80117c40bbd187a1fde336be6e8"
dependencies = [
"tinyvec",
]
[[package]]
name = "unicode-properties"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7df058c713841ad818f1dc5d3fd88063241cc61f49f5fbea4b951e8cf5a8d71d"
[[package]]
name = "untrusted"
version = "0.9.0"
@@ -1840,6 +2097,12 @@ version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "walkdir"
version = "2.5.0"
@@ -1874,6 +2137,12 @@ dependencies = [
"wit-bindgen",
]
[[package]]
name = "wasite"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b"
[[package]]
name = "wasm-bindgen"
version = "0.2.106"
@@ -1961,6 +2230,17 @@ dependencies = [
"rustls-pki-types",
]
[[package]]
name = "whoami"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d4a4db5077702ca3015d3d02d74974948aba2ad9e12ab7df718ee64ccd7e97d"
dependencies = [
"libredox",
"wasite",
"web-sys",
]
[[package]]
name = "winapi-util"
version = "0.1.11"
+2 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "portabase-agent"
version = "0.1.1-rc.8"
version = "0.1.2-rc.1"
edition = "2024"
[dependencies]
@@ -26,6 +26,7 @@ openssl = "0.10.75"
hex = "0.4.3"
flate2 = "1.1.5"
tar = "0.4.44"
tokio-postgres = "0.7.15"
[[bin]]
name = "app"
BIN
View File
Binary file not shown.
-118
View File
@@ -1,125 +1,8 @@
## =========================
## Base image (shared)
## =========================
#FROM rust:1.92.0 AS base
#
#RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
# pkg-config \
# libssl-dev \
# tzdata \
# redis-server \
# ca-certificates \
# curl \
# && apt-get clean \
# && rm -rf /var/lib/apt/lists/*
#
#
#
## ========= Install PostgreSQL client binaries (versions 12-18) =========
## Pre-downloaded binaries from assets/tools/ - no network download needed
#ARG TARGETARCH
#RUN mkdir -p /usr/lib/postgresql/12/bin /usr/lib/postgresql/13/bin \
# /usr/lib/postgresql/14/bin /usr/lib/postgresql/15/bin \
# /usr/lib/postgresql/16/bin /usr/lib/postgresql/17/bin \
# /usr/lib/postgresql/18/bin
#
## Copy pre-downloaded PostgreSQL binaries based on architecture
#COPY assets/tools/x64/postgresql/ /tmp/pg-x64/
#COPY assets/tools/arm/postgresql/ /tmp/pg-arm/
#RUN if [ "$TARGETARCH" = "amd64" ]; then \
# cp -r /tmp/pg-x64/postgresql-12/bin/* /usr/lib/postgresql/12/bin/ && \
# cp -r /tmp/pg-x64/postgresql-13/bin/* /usr/lib/postgresql/13/bin/ && \
# cp -r /tmp/pg-x64/postgresql-14/bin/* /usr/lib/postgresql/14/bin/ && \
# cp -r /tmp/pg-x64/postgresql-15/bin/* /usr/lib/postgresql/15/bin/ && \
# cp -r /tmp/pg-x64/postgresql-16/bin/* /usr/lib/postgresql/16/bin/ && \
# cp -r /tmp/pg-x64/postgresql-17/bin/* /usr/lib/postgresql/17/bin/ && \
# cp -r /tmp/pg-x64/postgresql-18/bin/* /usr/lib/postgresql/18/bin/; \
# elif [ "$TARGETARCH" = "arm64" ]; then \
# cp -r /tmp/pg-arm/postgresql-12/bin/* /usr/lib/postgresql/12/bin/ && \
# cp -r /tmp/pg-arm/postgresql-13/bin/* /usr/lib/postgresql/13/bin/ && \
# cp -r /tmp/pg-arm/postgresql-14/bin/* /usr/lib/postgresql/14/bin/ && \
# cp -r /tmp/pg-arm/postgresql-15/bin/* /usr/lib/postgresql/15/bin/ && \
# cp -r /tmp/pg-arm/postgresql-16/bin/* /usr/lib/postgresql/16/bin/ && \
# cp -r /tmp/pg-arm/postgresql-17/bin/* /usr/lib/postgresql/17/bin/ && \
# cp -r /tmp/pg-arm/postgresql-18/bin/* /usr/lib/postgresql/18/bin/; \
# fi && \
# rm -rf /tmp/pg-x64 /tmp/pg-arm && \
# chmod +x /usr/lib/postgresql/*/bin/*
#
#
#
#WORKDIR /app
#
## =========================
## Development image
## =========================
#FROM base AS dev
#
#RUN cargo install cargo-watch
#
## Pre-cache dependencies
#COPY Cargo.toml Cargo.lock ./
#RUN mkdir src && echo "fn main() {}" > src/main.rs
#RUN cargo build
#RUN rm -rf src
#
#COPY entrypoint.sh /entrypoint.sh
#RUN chmod +x /entrypoint.sh
#
#CMD ["/entrypoint.sh"]
#
## =========================
## Builder (production)
## =========================
#FROM base AS builder
#
#COPY . .
#RUN cargo build --release
#
#RUN echo "APP_VERSION=$(cargo pkgid | awk -F# '{print $2}')" > /app/version.env
#
## =========================
## Runtime (production)
## =========================
#FROM debian:bookworm-slim AS prod
##FROM ubuntu:24.04 AS prod
#
#
#RUN apt-get update && apt-get install -y \
# redis-server \
# ca-certificates \
# tzdata \
# postgresql-client \
# libpq5 \
# mariadb-client \
# && rm -rf /var/lib/apt/lists/*
#
#WORKDIR /app
#
#COPY --from=builder /app/target/release/app /usr/local/bin/app
#COPY --from=builder /app/version.env /app/version.env
#COPY entrypoint.sh /entrypoint.sh
#RUN chmod +x /entrypoint.sh
#
#ENV APP_ENV=production
#
#CMD ["/entrypoint.sh"]
# =========================
# Base image (shared)
# =========================
FROM rust:1.92.0 AS base
# Install common dependencies
#RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
# pkg-config \
# libssl-dev \
# tzdata \
# redis-server \
# ca-certificates \
# libpq5 \
# curl \
# && apt-get clean \
# && rm -rf /var/lib/apt/lists/*
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
pkg-config \
libssl-dev \
@@ -135,7 +18,6 @@ RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
&& rm -rf /var/lib/apt/lists/*
# =========================
# PostgreSQL client binaries (versions 12-18)
# =========================
+97 -91
View File
@@ -2,13 +2,14 @@
use crate::domain::factory::Database;
use crate::services::config::DatabaseConfig;
use anyhow::{Context as AnyhowContext, Result};
use anyhow::Result;
use async_trait::async_trait;
use flate2::Compression;
use flate2::write::GzEncoder;
use log::info;
use std::path::{Path, PathBuf};
use std::process::Command;
use tokio_postgres::{Client, NoTls};
use tracing::debug;
#[derive(Clone, Copy)]
@@ -37,58 +38,60 @@ impl PostgresDatabase {
}
pub async fn detect_format_from_size(cfg: &DatabaseConfig) -> PostgresDumpFormat {
let url = format!(
info!(
"Detecting database format {:?} - {:?}",
cfg.name, cfg.generated_id
);
let client = match PostgresDatabase::connect(cfg).await {
Ok(c) => c,
Err(_) => return PostgresDumpFormat::Fc,
};
let row = match client
.query_one("SELECT pg_database_size(current_database());", &[])
.await
{
Ok(r) => r,
Err(_) => return PostgresDumpFormat::Fc,
};
let size_bytes: i64 = row.get(0);
info!("Size of database is {} bytes", size_bytes);
// > 1 Go
if size_bytes > 1_000_000_000 {
info!("Using -Fd format");
PostgresDumpFormat::Fd
} else {
info!("Using -Fc format");
PostgresDumpFormat::Fc
}
}
async fn connect(cfg: &DatabaseConfig) -> anyhow::Result<Client> {
let dsn = format!(
"host={} port={} user={} password={} dbname={}",
cfg.host, cfg.port, cfg.username, cfg.password, cfg.database
);
let output = std::process::Command::new("psql")
.arg(&url)
.arg("-t")
.arg("-c")
.arg("SELECT pg_database_size(current_database());")
.output();
match output {
Ok(out) if out.status.success() => {
let size_bytes: i64 = String::from_utf8_lossy(&out.stdout)
.trim()
.parse()
.unwrap_or(0);
// > 1 Go
if size_bytes > 1_000_000_000 {
PostgresDumpFormat::Fd
} else {
PostgresDumpFormat::Fc
}
let (client, connection) = tokio_postgres::connect(&dsn, NoTls).await?;
tokio::spawn(async move {
if let Err(e) = connection.await {
tracing::error!("Postgres connection error: {}", e);
}
_ => PostgresDumpFormat::Fc, // fallback legacy
}
});
Ok(client)
}
fn get_postgres_server_version(cfg: &DatabaseConfig) -> Result<String, anyhow::Error> {
let output = Command::new("/usr/lib/postgresql/16/bin/psql")
.arg("-U")
.arg(&cfg.username)
.arg("-h")
.arg(&cfg.host)
.arg("-p")
.arg(cfg.port.to_string())
.arg("-d")
.arg(&cfg.database)
.arg("-t") // only return value
.arg("-c")
.arg("SHOW server_version;")
.env("PGPASSWORD", &cfg.password)
.output()?;
async fn get_postgres_server_version(cfg: &DatabaseConfig) -> anyhow::Result<String> {
let client = Self::connect(cfg).await?;
if !output.status.success() {
anyhow::bail!("Failed to get PostgreSQL version");
}
let version_str = String::from_utf8_lossy(&output.stdout).trim().to_string();
// Query the version string
let row = client.query_one("SHOW server_version;", &[]).await?;
let version_str: String = row.get(0);
// Parse the major version
let major_version: u32 = version_str
.split('.')
.next()
@@ -110,6 +113,27 @@ impl PostgresDatabase {
let major = version.split('.').next().unwrap_or("17"); // default to 17
PathBuf::from(format!("/usr/lib/postgresql/{}/bin", major))
}
async fn terminate_connections(cfg: &DatabaseConfig) -> anyhow::Result<()> {
let mut admin_cfg = cfg.clone();
admin_cfg.database = "postgres".to_string();
let client = Self::connect(&admin_cfg).await?;
client
.execute(
"
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = $1
AND pid <> pg_backend_pid();
",
&[&cfg.database],
)
.await?;
Ok(())
}
}
#[async_trait]
@@ -121,34 +145,35 @@ impl Database for PostgresDatabase {
}
}
// async fn ping(&self) -> Result<bool> {
// let server_version = PostgresDatabase::get_postgres_server_version(&self.cfg).await?;
// let pg_path = PostgresDatabase::select_pg_path(&server_version.to_string());
// let pg_isready_path = format!("{}/pg_isready", pg_path.display());
//
// debug!("Server version: {} -> {}", server_version, pg_isready_path);
//
// let url = format!(
// "postgresql://{}:{}@{}:{}/{}",
// self.cfg.username, self.cfg.password, self.cfg.host, self.cfg.port, self.cfg.database
// );
//
// let status = Command::new(pg_isready_path)
// .arg("--dbname")
// .arg(url)
// .status()
// .context("Failed to ping Postgres")?;
// Ok(status.success())
// }
async fn ping(&self) -> Result<bool> {
let server_version = PostgresDatabase::get_postgres_server_version(&self.cfg)?;
let pg_path = PostgresDatabase::select_pg_path(&server_version);
let pg_isready_path = format!("{}/pg_isready", pg_path.display());
debug!("Server version: {}", server_version);
debug!("pg_isready_path: {}", pg_isready_path);
let url = format!(
"postgresql://{}:{}@{}:{}/{}",
self.cfg.username, self.cfg.password, self.cfg.host, self.cfg.port, self.cfg.database
);
let status = Command::new(pg_isready_path)
.arg("--dbname")
.arg(url)
.status()
.context("Failed to ping Postgres")?;
Ok(status.success())
Ok(Self::connect(&self.cfg).await.is_ok())
}
async fn backup(&self, backup_dir: &Path) -> Result<PathBuf> {
let server_version = PostgresDatabase::get_postgres_server_version(&self.cfg)?;
let pg_path = PostgresDatabase::select_pg_path(&server_version);
let server_version = PostgresDatabase::get_postgres_server_version(&self.cfg).await?;
let pg_path = PostgresDatabase::select_pg_path(&server_version.to_string());
let pg_dump_path = format!("{}/pg_dump", pg_path.display());
debug!("Server version: {}", server_version);
debug!("pg_dump_path: {}", pg_dump_path);
debug!("Server version: {} -> {}", server_version, pg_dump_path);
match self.format {
PostgresDumpFormat::Fc => {
@@ -192,7 +217,7 @@ impl Database for PostgresDatabase {
self.cfg.port,
self.cfg.database
);
let status = Command::new("pg_dump")
let status = Command::new(pg_dump_path)
.arg("--dbname")
.arg(url)
.arg("-Fd")
@@ -218,38 +243,19 @@ impl Database for PostgresDatabase {
}
async fn restore(&self, restore_file: &Path) -> Result<()> {
let server_version = PostgresDatabase::get_postgres_server_version(&self.cfg)?;
let pg_path = PostgresDatabase::select_pg_path(&server_version);
info!("Restoring database \"{}\"", self.cfg.database);
let server_version = PostgresDatabase::get_postgres_server_version(&self.cfg).await?;
let pg_path = PostgresDatabase::select_pg_path(&server_version.to_string());
let pg_restore_path = format!("{}/pg_restore", pg_path.display());
let psql_path = format!("{}/psql", pg_path.display());
debug!("Server version: {}", server_version);
debug!("pg_restore_path: {}", pg_restore_path);
debug!("psql_path: {}", psql_path);
debug!("Server version: {} -> {}", server_version, pg_restore_path);
let url = format!(
"postgresql://{}:{}@{}:{}/{}",
self.cfg.username, self.cfg.password, self.cfg.host, self.cfg.port, "postgres"
);
// Terminate connections
let terminate_cmd = format!(
"SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname='{}' AND pid<>pg_backend_pid();",
self.cfg.database
);
Command::new(psql_path)
.arg("-U")
.arg(&self.cfg.username)
.arg("-d")
.arg("postgres")
.arg("-h")
.arg(&self.cfg.host)
.arg("-p")
.arg(self.cfg.port.to_string())
.arg("-c")
.arg(&terminate_cmd)
.env("PGPASSWORD", &self.cfg.password)
.status()?;
PostgresDatabase::terminate_connections(&self.cfg).await?;
match self.format {
PostgresDumpFormat::Fc => {