From 8ca8839d7ed20d0b03da807dd3170a51789b8cf5 Mon Sep 17 00:00:00 2001 From: Bima42 Date: Tue, 18 Nov 2025 19:40:00 +0100 Subject: [PATCH 1/4] fix: update mount path on editing pg image --- apps/dokploy/server/api/routers/postgres.ts | 33 ++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/apps/dokploy/server/api/routers/postgres.ts b/apps/dokploy/server/api/routers/postgres.ts index a05072ab7..9eb7f3af5 100644 --- a/apps/dokploy/server/api/routers/postgres.ts +++ b/apps/dokploy/server/api/routers/postgres.ts @@ -6,6 +6,7 @@ import { deployPostgres, findBackupsByDbId, findEnvironmentById, + findMountsByApplicationId, findPostgresById, findProjectById, IS_CLOUD, @@ -16,6 +17,7 @@ import { startServiceRemote, stopService, stopServiceRemote, + updateMount, updatePostgresById, } from "@dokploy/server"; import { TRPCError } from "@trpc/server"; @@ -37,6 +39,16 @@ import { postgres as postgresTable, } from "@/server/db/schema"; import { cancelJobs } from "@/server/utils/backup"; + +function getMountPath(dockerImage: string): string { + const versionMatch = dockerImage.match(/postgres:(\d+)/); + const version = versionMatch?.[1] + ? Number.parseInt(versionMatch[1], 10) + : 18; + + return `/var/lib/postgresql/${version}/data`; +} + export const postgresRouter = createTRPCRouter({ create: protectedProcedure .input(apiCreatePostgres) @@ -79,11 +91,13 @@ export const postgresRouter = createTRPCRouter({ ); } + const mountPath = getMountPath(input.dockerImage); + await createMount({ serviceId: newPostgres.postgresId, serviceType: "postgres", volumeName: `${newPostgres.appName}-data`, - mountPath: "/var/lib/postgresql/data", + mountPath: mountPath, type: "volume", }); @@ -363,6 +377,23 @@ export const postgresRouter = createTRPCRouter({ message: "You are not authorized to update this Postgres", }); } + + if (rest.dockerImage) { + const mountPath = getMountPath(rest.dockerImage); + const mounts = await findMountsByApplicationId(postgresId, "postgres"); + if (!mounts || mounts.length === 0) { + throw new TRPCError({ + code: "NOT_FOUND", + message: "Mount not found for this Postgres", + }); + } + for (const mount of mounts) { + await updateMount(mount.mountId, { + mountPath: mountPath, + }); + } + } + const service = await updatePostgresById(postgresId, { ...rest, }); From d08fdeb939101a94512f2f7d572ce37a9aee2ba2 Mon Sep 17 00:00:00 2001 From: Bima42 Date: Tue, 18 Nov 2025 19:47:29 +0100 Subject: [PATCH 2/4] fix: only upgrade those that use default pg path --- apps/dokploy/server/api/routers/postgres.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/apps/dokploy/server/api/routers/postgres.ts b/apps/dokploy/server/api/routers/postgres.ts index 9eb7f3af5..67faed6c6 100644 --- a/apps/dokploy/server/api/routers/postgres.ts +++ b/apps/dokploy/server/api/routers/postgres.ts @@ -42,9 +42,7 @@ import { cancelJobs } from "@/server/utils/backup"; function getMountPath(dockerImage: string): string { const versionMatch = dockerImage.match(/postgres:(\d+)/); - const version = versionMatch?.[1] - ? Number.parseInt(versionMatch[1], 10) - : 18; + const version = versionMatch?.[1] ? Number.parseInt(versionMatch[1], 10) : 18; return `/var/lib/postgresql/${version}/data`; } @@ -388,9 +386,11 @@ export const postgresRouter = createTRPCRouter({ }); } for (const mount of mounts) { - await updateMount(mount.mountId, { - mountPath: mountPath, - }); + if (mount.mountPath.startsWith("/var/lib/postgresql")) { + await updateMount(mount.mountId, { + mountPath: mountPath, + }); + } } } From 23b235303ccdb72cb505270f55864570763b272c Mon Sep 17 00:00:00 2001 From: Mauricio Siu Date: Wed, 26 Nov 2025 01:20:40 -0500 Subject: [PATCH 3/4] refactor: move getMountPath function to services and update logic - Moved the getMountPath function from the postgres router to the postgres service for better organization. - Updated the logic to return the correct mount path based on the PostgreSQL version, ensuring compatibility with versions below 18. --- apps/dokploy/server/api/routers/postgres.ts | 8 +------- packages/server/src/services/postgres.ts | 12 ++++++++++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/apps/dokploy/server/api/routers/postgres.ts b/apps/dokploy/server/api/routers/postgres.ts index 67faed6c6..8503384e2 100644 --- a/apps/dokploy/server/api/routers/postgres.ts +++ b/apps/dokploy/server/api/routers/postgres.ts @@ -9,6 +9,7 @@ import { findMountsByApplicationId, findPostgresById, findProjectById, + getMountPath, IS_CLOUD, rebuildDatabase, removePostgresById, @@ -40,13 +41,6 @@ import { } from "@/server/db/schema"; import { cancelJobs } from "@/server/utils/backup"; -function getMountPath(dockerImage: string): string { - const versionMatch = dockerImage.match(/postgres:(\d+)/); - const version = versionMatch?.[1] ? Number.parseInt(versionMatch[1], 10) : 18; - - return `/var/lib/postgresql/${version}/data`; -} - export const postgresRouter = createTRPCRouter({ create: protectedProcedure .input(apiCreatePostgres) diff --git a/packages/server/src/services/postgres.ts b/packages/server/src/services/postgres.ts index 0d900443e..d7016038a 100644 --- a/packages/server/src/services/postgres.ts +++ b/packages/server/src/services/postgres.ts @@ -13,6 +13,18 @@ import { TRPCError } from "@trpc/server"; import { eq, getTableColumns } from "drizzle-orm"; import { validUniqueServerAppName } from "./project"; +export function getMountPath(dockerImage: string): string { + const versionMatch = dockerImage.match(/postgres:(\d+)/); + + if (versionMatch?.[1]) { + const version = Number.parseInt(versionMatch[1], 10); + if (version >= 18) { + return `/var/lib/postgresql/${version}/data`; + } + } + return "/var/lib/postgresql/data"; +} + export type Postgres = typeof postgres.$inferSelect; export const createPostgres = async (input: typeof apiCreatePostgres._type) => { From 736a7320d4ffa2a7ba27967be0bf744d5abe2bd1 Mon Sep 17 00:00:00 2001 From: Mauricio Siu Date: Wed, 26 Nov 2025 01:21:56 -0500 Subject: [PATCH 4/4] refactor: remove unused mount-related logic from postgres router - Removed the findMountsByApplicationId and updateMount functions from the postgres router as they are no longer needed after the recent refactor of the getMountPath function. - Cleaned up the code to streamline the update process for PostgreSQL instances. --- apps/dokploy/server/api/routers/postgres.ts | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/apps/dokploy/server/api/routers/postgres.ts b/apps/dokploy/server/api/routers/postgres.ts index 8503384e2..6cb88385f 100644 --- a/apps/dokploy/server/api/routers/postgres.ts +++ b/apps/dokploy/server/api/routers/postgres.ts @@ -6,7 +6,6 @@ import { deployPostgres, findBackupsByDbId, findEnvironmentById, - findMountsByApplicationId, findPostgresById, findProjectById, getMountPath, @@ -18,7 +17,6 @@ import { startServiceRemote, stopService, stopServiceRemote, - updateMount, updatePostgresById, } from "@dokploy/server"; import { TRPCError } from "@trpc/server"; @@ -370,24 +368,6 @@ export const postgresRouter = createTRPCRouter({ }); } - if (rest.dockerImage) { - const mountPath = getMountPath(rest.dockerImage); - const mounts = await findMountsByApplicationId(postgresId, "postgres"); - if (!mounts || mounts.length === 0) { - throw new TRPCError({ - code: "NOT_FOUND", - message: "Mount not found for this Postgres", - }); - } - for (const mount of mounts) { - if (mount.mountPath.startsWith("/var/lib/postgresql")) { - await updateMount(mount.mountId, { - mountPath: mountPath, - }); - } - } - } - const service = await updatePostgresById(postgresId, { ...rest, });