mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
feat(databases): add database rebuild functionality
- Implement RebuildDatabase component for all database types - Create ShowDatabaseAdvancedSettings component to consolidate advanced settings - Add rebuild API endpoints for Postgres, MySQL, MariaDB, MongoDB, and Redis - Implement server-side database rebuild utility with volume and service removal - Enhance database management with a dangerous zone for complete database reset
This commit is contained in:
@@ -146,3 +146,9 @@ export const apiUpdateMariaDB = createSchema
|
||||
mariadbId: z.string().min(1),
|
||||
})
|
||||
.omit({ serverId: true });
|
||||
|
||||
export const apiRebuildMariadb = createSchema
|
||||
.pick({
|
||||
mariadbId: true,
|
||||
})
|
||||
.required();
|
||||
|
||||
@@ -141,3 +141,9 @@ export const apiResetMongo = createSchema
|
||||
appName: true,
|
||||
})
|
||||
.required();
|
||||
|
||||
export const apiRebuildMongo = createSchema
|
||||
.pick({
|
||||
mongoId: true,
|
||||
})
|
||||
.required();
|
||||
|
||||
@@ -144,3 +144,9 @@ export const apiUpdateMySql = createSchema
|
||||
mysqlId: z.string().min(1),
|
||||
})
|
||||
.omit({ serverId: true });
|
||||
|
||||
export const apiRebuildMysql = createSchema
|
||||
.pick({
|
||||
mysqlId: true,
|
||||
})
|
||||
.required();
|
||||
|
||||
@@ -140,3 +140,9 @@ export const apiUpdatePostgres = createSchema
|
||||
postgresId: z.string().min(1),
|
||||
})
|
||||
.omit({ serverId: true });
|
||||
|
||||
export const apiRebuildPostgres = createSchema
|
||||
.pick({
|
||||
postgresId: true,
|
||||
})
|
||||
.required();
|
||||
|
||||
@@ -133,3 +133,9 @@ export const apiUpdateRedis = createSchema
|
||||
redisId: z.string().min(1),
|
||||
})
|
||||
.omit({ serverId: true });
|
||||
|
||||
export const apiRebuildRedis = createSchema
|
||||
.pick({
|
||||
redisId: true,
|
||||
})
|
||||
.required();
|
||||
|
||||
@@ -30,7 +30,7 @@ export * from "./services/github";
|
||||
export * from "./services/gitlab";
|
||||
export * from "./services/server";
|
||||
export * from "./services/application";
|
||||
|
||||
export * from "./utils/databases/rebuild";
|
||||
export * from "./setup/config-paths";
|
||||
export * from "./setup/postgres-setup";
|
||||
export * from "./setup/redis-setup";
|
||||
|
||||
99
packages/server/src/utils/databases/rebuild.ts
Normal file
99
packages/server/src/utils/databases/rebuild.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import { deployPostgres } from "@dokploy/server/services/postgres";
|
||||
import { execAsyncRemote } from "../process/execAsync";
|
||||
import { execAsync } from "../process/execAsync";
|
||||
import { deployMySql } from "@dokploy/server/services/mysql";
|
||||
import { deployMariadb } from "@dokploy/server/services/mariadb";
|
||||
import { deployMongo } from "@dokploy/server/services/mongo";
|
||||
import { deployRedis } from "@dokploy/server/services/redis";
|
||||
import { removeService } from "../docker/utils";
|
||||
import { db } from "@dokploy/server/db";
|
||||
import {
|
||||
postgres,
|
||||
mysql,
|
||||
mariadb,
|
||||
mongo,
|
||||
redis,
|
||||
} from "@dokploy/server/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
type DatabaseType = "postgres" | "mysql" | "mariadb" | "mongo" | "redis";
|
||||
|
||||
export const rebuildDatabase = async (
|
||||
databaseId: string,
|
||||
type: DatabaseType,
|
||||
) => {
|
||||
const database = await findDatabaseById(databaseId, type);
|
||||
|
||||
if (!database) {
|
||||
throw new Error("Database not found");
|
||||
}
|
||||
|
||||
await removeService(database.appName, database.serverId);
|
||||
await new Promise((resolve) => setTimeout(resolve, 6000));
|
||||
|
||||
for (const mount of database.mounts) {
|
||||
if (mount.type === "volume") {
|
||||
const command = `docker volume rm ${mount?.volumeName} --force`;
|
||||
if (database.serverId) {
|
||||
await execAsyncRemote(database.serverId, command);
|
||||
} else {
|
||||
await execAsync(command);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (type === "postgres") {
|
||||
await deployPostgres(databaseId);
|
||||
} else if (type === "mysql") {
|
||||
await deployMySql(databaseId);
|
||||
} else if (type === "mariadb") {
|
||||
await deployMariadb(databaseId);
|
||||
} else if (type === "mongo") {
|
||||
await deployMongo(databaseId);
|
||||
} else if (type === "redis") {
|
||||
await deployRedis(databaseId);
|
||||
}
|
||||
};
|
||||
|
||||
const findDatabaseById = async (databaseId: string, type: DatabaseType) => {
|
||||
if (type === "postgres") {
|
||||
return await db.query.postgres.findFirst({
|
||||
where: eq(postgres.postgresId, databaseId),
|
||||
with: {
|
||||
mounts: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
if (type === "mysql") {
|
||||
return await db.query.mysql.findFirst({
|
||||
where: eq(mysql.mysqlId, databaseId),
|
||||
with: {
|
||||
mounts: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
if (type === "mariadb") {
|
||||
return await db.query.mariadb.findFirst({
|
||||
where: eq(mariadb.mariadbId, databaseId),
|
||||
with: {
|
||||
mounts: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
if (type === "mongo") {
|
||||
return await db.query.mongo.findFirst({
|
||||
where: eq(mongo.mongoId, databaseId),
|
||||
with: {
|
||||
mounts: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
if (type === "redis") {
|
||||
return await db.query.redis.findFirst({
|
||||
where: eq(redis.redisId, databaseId),
|
||||
with: {
|
||||
mounts: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user