refactor(backends): cleanup code

This commit is contained in:
Nicolas Meienberger
2025-11-20 06:36:47 +01:00
parent b70f973c12
commit 3c2791102f
19 changed files with 295 additions and 544 deletions

View File

@@ -1,69 +1,80 @@
import * as fs from "node:fs/promises";
import { toMessage } from "../../../utils/errors";
import { logger } from "../../../utils/logger";
import { testPostgresConnection } from "../../../utils/database-dump";
import type { VolumeBackend } from "../backend";
import { BACKEND_STATUS, type BackendConfig } from "~/schemas/volumes";
import { VOLUME_MOUNT_BASE } from "../../../core/constants";
const mount = async (config: BackendConfig, volumePath: string) => {
if (config.backend !== "postgres") {
return { status: BACKEND_STATUS.error, error: "Invalid backend type" };
}
logger.info(`Testing PostgreSQL connection to: ${config.host}:${config.port}`);
try {
await testPostgresConnection(config);
await fs.mkdir(volumePath, { recursive: true });
logger.info("PostgreSQL connection successful");
return { status: BACKEND_STATUS.mounted };
} catch (error) {
logger.error("Failed to connect to PostgreSQL:", error);
return { status: BACKEND_STATUS.error, error: toMessage(error) };
}
};
const unmount = async (volumePath: string) => {
logger.info("Cleaning up PostgreSQL dump directory");
try {
await fs.rm(volumePath, { recursive: true, force: true });
return { status: BACKEND_STATUS.unmounted };
} catch (error) {
logger.warn(`Failed to clean up PostgreSQL dump directory: ${toMessage(error)}`);
return { status: BACKEND_STATUS.unmounted };
}
};
import { $ } from "bun";
const checkHealth = async (config: BackendConfig) => {
if (config.backend !== "postgres") {
return { status: BACKEND_STATUS.error, error: "Invalid backend type" };
}
try {
await testPostgresConnection(config);
return { status: BACKEND_STATUS.mounted };
} catch (error) {
logger.error("PostgreSQL health check failed:", error);
return { status: BACKEND_STATUS.error, error: toMessage(error) };
if (config.backend !== "postgres") {
throw new Error("Invalid backend type for PostgreSQL connection test");
}
logger.debug(`Testing PostgreSQL connection to: ${config.host}:${config.port}`);
const args = [
`--host=${config.host}`,
`--port=${config.port}`,
`--username=${config.username}`,
`--dbname=${config.database}`,
"--command=SELECT 1",
"--no-password",
];
const env = {
PGPASSWORD: config.password,
PGSSLMODE: "disable",
};
logger.debug(`Running psql with args: ${args.join(" ")}`);
const res = await $`psql ${args}`.env(env).nothrow();
if (res.exitCode !== 0) {
return { status: BACKEND_STATUS.error, error: res.stderr.toString() };
}
return { status: BACKEND_STATUS.mounted };
};
export const makePostgresBackend = (config: BackendConfig, volumeName: string, volumePath: string): VolumeBackend => ({
mount: () => mount(config, volumePath),
unmount: () => unmount(volumePath),
const getBackupPath = async (config: BackendConfig) => {
if (config.backend !== "postgres") {
throw new Error("Invalid backend type for PostgreSQL dump");
}
const dumpDir = await fs.mkdtemp(`/tmp/ironmount-postgres-`);
const outputPath = `${dumpDir}/${config.dumpFormat === "plain" ? "dump.sql" : "dump.dump"}`;
logger.info(`Starting PostgreSQL dump for database: ${config.database}`);
const args = [
`--host=${config.host}`,
`--port=${config.port}`,
`--username=${config.username}`,
`--dbname=${config.database}`,
`--format=${config.dumpFormat}`,
`--file=${outputPath}`,
"--no-password",
...(config.dumpOptions || []),
];
const env = {
PGPASSWORD: config.password,
PGSSLMODE: "disable",
};
await $`pg_dump ${args}`.env(env);
return outputPath;
};
export const makePostgresBackend = (config: BackendConfig): VolumeBackend => ({
mount: () => Promise.resolve({ status: "mounted" }),
unmount: () => Promise.resolve({ status: "unmounted" }),
checkHealth: () => checkHealth(config),
getVolumePath: () => volumePath,
isDatabaseBackend: () => true,
getDumpPath: () => `${VOLUME_MOUNT_BASE}/${volumeName}/dumps`,
getDumpFilePath: (timestamp: number) => {
const dumpDir = `${VOLUME_MOUNT_BASE}/${volumeName}/dumps`;
const extension = config.backend === "postgres" &&
(config as Extract<BackendConfig, { backend: "postgres" }>).dumpFormat !== "plain"
? "dump"
: "sql";
return `${dumpDir}/${volumeName}-${timestamp}.${extension}`;
},
});
getVolumePath: () => "/tmp",
getBackupPath: () => getBackupPath(config),
});