mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
* refactor: add sidebar * chore: add deps * refactor: update sidebar * refactor: another layout * refactor: update variant * refactor: change layout * refactor: change variant * refactor: enhance sidebar navigation with active state management * feat: add project button to dashboard * Merge branch 'canary' into feat/add-sidebar * refactor: add loader * refactor: update destinations and refactor * refactor: ui refactor certificates * refactor: delete unused files * refactor: remove unused files and duplicate registry * refactor: update style registry * refactor: add new design registry * refactor: enhance git providers * refactor: remove duplicate files * refactor: update * refactor: update users * refactor: delete unused files * refactor: update profile * refactor: apply changes * refactor: update UI * refactor: enhance Docker monitoring UI layout * refactor: add theme toggle and language selection to user navigation (#1083) * refactor: remove unused files * feat: add filter to services * refactor: add active items * refactor: remove tab prop * refactor: remove unused files * refactor: remove duplicated files * refactor: remove unused files * refactor: remove duplicate files * refactor: remove unused files * refactor: delete unused files * refactor: remove unsued files * refactor: delete unused files * refactor: lint * refactor: remove unused secuirty * refactor: delete unused files * refactor: delete unused files * remove imports * refactor: add update button * refactor: delete unused files * refactor: remove unused code * refactor: remove unused files * refactor: update login page * refactor: update login UI * refactor: update ui reset password * refactor: add justify end * feat: add suscriptions * feat: add sheet * feat: add logs for postgres * feat: add logs for all databases * feat: add server logs with drawer logs * refactor: remove unused files * refactor: add refetch when closing * refactor: fix linter * chore: bump node-20 * revert * refactor: fix conflicts * refactor: update * refactor: add missing deps * refactor: delete duplicate files * refactor: delete unsued files * chore: lint * refactor: remove unsued file * refactor: add refetch * refactor: remove duplicated files * refactor: delete unused files * refactor: update setup onboarding * refactor: add breadcrumb * refactor: apply updates * refactor: add faker * refactor: use 0 in validation * refactor: show correct state * refactor: update --------- Co-authored-by: vishalkadam47 <vishal@jeevops.com> Co-authored-by: Vishal kadam <107353260+vishalkadam47@users.noreply.github.com>
159 lines
3.6 KiB
TypeScript
159 lines
3.6 KiB
TypeScript
import { db } from "@dokploy/server/db";
|
|
import {
|
|
type apiCreatePostgres,
|
|
backups,
|
|
postgres,
|
|
} from "@dokploy/server/db/schema";
|
|
import { buildAppName, cleanAppName } from "@dokploy/server/db/schema";
|
|
import { generatePassword } from "@dokploy/server/templates/utils";
|
|
import { buildPostgres } from "@dokploy/server/utils/databases/postgres";
|
|
import { pullImage } from "@dokploy/server/utils/docker/utils";
|
|
import { TRPCError } from "@trpc/server";
|
|
import { eq, getTableColumns } from "drizzle-orm";
|
|
import { validUniqueServerAppName } from "./project";
|
|
|
|
import { execAsyncRemote } from "@dokploy/server/utils/process/execAsync";
|
|
|
|
export type Postgres = typeof postgres.$inferSelect;
|
|
|
|
export const createPostgres = async (input: typeof apiCreatePostgres._type) => {
|
|
const appName = buildAppName("postgres", input.appName);
|
|
|
|
const valid = await validUniqueServerAppName(appName);
|
|
if (!valid) {
|
|
throw new TRPCError({
|
|
code: "CONFLICT",
|
|
message: "Service with this 'AppName' already exists",
|
|
});
|
|
}
|
|
|
|
const newPostgres = await db
|
|
.insert(postgres)
|
|
.values({
|
|
...input,
|
|
databasePassword: input.databasePassword
|
|
? input.databasePassword
|
|
: generatePassword(),
|
|
appName,
|
|
})
|
|
.returning()
|
|
.then((value) => value[0]);
|
|
|
|
if (!newPostgres) {
|
|
throw new TRPCError({
|
|
code: "BAD_REQUEST",
|
|
message: "Error input: Inserting postgresql database",
|
|
});
|
|
}
|
|
|
|
return newPostgres;
|
|
};
|
|
export const findPostgresById = async (postgresId: string) => {
|
|
const result = await db.query.postgres.findFirst({
|
|
where: eq(postgres.postgresId, postgresId),
|
|
with: {
|
|
project: true,
|
|
mounts: true,
|
|
server: true,
|
|
backups: {
|
|
with: {
|
|
destination: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
if (!result) {
|
|
throw new TRPCError({
|
|
code: "NOT_FOUND",
|
|
message: "Postgres not found",
|
|
});
|
|
}
|
|
return result;
|
|
};
|
|
|
|
export const findPostgresByBackupId = async (backupId: string) => {
|
|
const result = await db
|
|
.select({
|
|
...getTableColumns(postgres),
|
|
})
|
|
.from(postgres)
|
|
.innerJoin(backups, eq(postgres.postgresId, backups.postgresId))
|
|
.where(eq(backups.backupId, backupId))
|
|
.limit(1);
|
|
|
|
if (!result || !result[0]) {
|
|
throw new TRPCError({
|
|
code: "NOT_FOUND",
|
|
message: "Postgres not found",
|
|
});
|
|
}
|
|
return result[0];
|
|
};
|
|
|
|
export const updatePostgresById = async (
|
|
postgresId: string,
|
|
postgresData: Partial<Postgres>,
|
|
) => {
|
|
const { appName, ...rest } = postgresData;
|
|
const result = await db
|
|
.update(postgres)
|
|
.set({
|
|
...rest,
|
|
})
|
|
.where(eq(postgres.postgresId, postgresId))
|
|
.returning();
|
|
|
|
return result[0];
|
|
};
|
|
|
|
export const removePostgresById = async (postgresId: string) => {
|
|
const result = await db
|
|
.delete(postgres)
|
|
.where(eq(postgres.postgresId, postgresId))
|
|
.returning();
|
|
|
|
return result[0];
|
|
};
|
|
|
|
export const deployPostgres = async (
|
|
postgresId: string,
|
|
onData?: (data: any) => void,
|
|
) => {
|
|
const postgres = await findPostgresById(postgresId);
|
|
try {
|
|
await updatePostgresById(postgresId, {
|
|
applicationStatus: "running",
|
|
});
|
|
|
|
onData?.("Starting postgres deployment...");
|
|
|
|
if (postgres.serverId) {
|
|
await execAsyncRemote(
|
|
postgres.serverId,
|
|
`docker pull ${postgres.dockerImage}`,
|
|
onData,
|
|
);
|
|
} else {
|
|
await pullImage(postgres.dockerImage, onData);
|
|
}
|
|
|
|
await buildPostgres(postgres);
|
|
|
|
await updatePostgresById(postgresId, {
|
|
applicationStatus: "done",
|
|
});
|
|
|
|
onData?.("Deployment completed successfully!");
|
|
} catch (error) {
|
|
onData?.(`Error: ${error}`);
|
|
await updatePostgresById(postgresId, {
|
|
applicationStatus: "error",
|
|
});
|
|
throw new TRPCError({
|
|
code: "INTERNAL_SERVER_ERROR",
|
|
message: `Error on deploy postgres${error}`,
|
|
});
|
|
}
|
|
return postgres;
|
|
};
|