File size: 2,522 Bytes
b80fc11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { db } from "@dokploy/server/db";
import {
	mariadb,
	mongo,
	mysql,
	postgres,
	redis,
} from "@dokploy/server/db/schema";
import { deployMariadb } from "@dokploy/server/services/mariadb";
import { deployMongo } from "@dokploy/server/services/mongo";
import { deployMySql } from "@dokploy/server/services/mysql";
import { deployPostgres } from "@dokploy/server/services/postgres";
import { deployRedis } from "@dokploy/server/services/redis";
import { eq } from "drizzle-orm";
import { removeService } from "../docker/utils";
import { execAsyncRemote } from "../process/execAsync";
import { execAsync } from "../process/execAsync";

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,
			},
		});
	}
};