mirror of
https://github.com/LukeHagar/dokploy.git
synced 2025-12-06 04:19:37 +00:00
Reapply "refactor: update database connection handling and remove unused migra…"
This reverts commit 17f333ac2a.
This commit is contained in:
@@ -7,6 +7,10 @@ function prepareDefine(config: DotenvParseOutput | undefined) {
|
||||
const define = {};
|
||||
// @ts-ignore
|
||||
for (const [key, value] of Object.entries(config)) {
|
||||
// Skip DATABASE_URL to allow runtime environment variable override
|
||||
if (key === "DATABASE_URL") {
|
||||
continue;
|
||||
}
|
||||
// @ts-ignore
|
||||
define[`process.env.${key}`] = JSON.stringify(value);
|
||||
}
|
||||
@@ -14,6 +18,7 @@ function prepareDefine(config: DotenvParseOutput | undefined) {
|
||||
}
|
||||
|
||||
const define = prepareDefine(result.parsed);
|
||||
|
||||
try {
|
||||
esbuild
|
||||
.build({
|
||||
|
||||
@@ -1,149 +0,0 @@
|
||||
// import { drizzle } from "drizzle-orm/postgres-js";
|
||||
// import { nanoid } from "nanoid";
|
||||
// import postgres from "postgres";
|
||||
// import * as schema from "./server/db/schema";
|
||||
|
||||
// const connectionString = process.env.DATABASE_URL!;
|
||||
|
||||
// const sql = postgres(connectionString, { max: 1 });
|
||||
// const db = drizzle(sql, {
|
||||
// schema,
|
||||
// });
|
||||
|
||||
// await db
|
||||
// .transaction(async (db) => {
|
||||
// const admins = await db.query.admins.findMany({
|
||||
// with: {
|
||||
// auth: true,
|
||||
// users: {
|
||||
// with: {
|
||||
// auth: true,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// });
|
||||
// for (const admin of admins) {
|
||||
// const user = await db
|
||||
// .insert(schema.users_temp)
|
||||
// .values({
|
||||
// id: admin.adminId,
|
||||
// email: admin.auth.email,
|
||||
// token: admin.auth.token || "",
|
||||
// emailVerified: true,
|
||||
// updatedAt: new Date(),
|
||||
// role: "admin",
|
||||
// serverIp: admin.serverIp,
|
||||
// image: admin.auth.image,
|
||||
// certificateType: admin.certificateType,
|
||||
// host: admin.host,
|
||||
// letsEncryptEmail: admin.letsEncryptEmail,
|
||||
// sshPrivateKey: admin.sshPrivateKey,
|
||||
// enableDockerCleanup: admin.enableDockerCleanup,
|
||||
// enableLogRotation: admin.enableLogRotation,
|
||||
// enablePaidFeatures: admin.enablePaidFeatures,
|
||||
// metricsConfig: admin.metricsConfig,
|
||||
// cleanupCacheApplications: admin.cleanupCacheApplications,
|
||||
// cleanupCacheOnPreviews: admin.cleanupCacheOnPreviews,
|
||||
// cleanupCacheOnCompose: admin.cleanupCacheOnCompose,
|
||||
// stripeCustomerId: admin.stripeCustomerId,
|
||||
// stripeSubscriptionId: admin.stripeSubscriptionId,
|
||||
// serversQuantity: admin.serversQuantity,
|
||||
// })
|
||||
// .returning()
|
||||
// .then((user) => user[0]);
|
||||
|
||||
// await db.insert(schema.account).values({
|
||||
// providerId: "credential",
|
||||
// userId: user?.id || "",
|
||||
// password: admin.auth.password,
|
||||
// is2FAEnabled: admin.auth.is2FAEnabled || false,
|
||||
// createdAt: new Date(admin.auth.createdAt) || new Date(),
|
||||
// updatedAt: new Date(admin.auth.createdAt) || new Date(),
|
||||
// });
|
||||
|
||||
// const organization = await db
|
||||
// .insert(schema.organization)
|
||||
// .values({
|
||||
// name: "My Organization",
|
||||
// slug: nanoid(),
|
||||
// ownerId: user?.id || "",
|
||||
// createdAt: new Date(admin.createdAt) || new Date(),
|
||||
// })
|
||||
// .returning()
|
||||
// .then((organization) => organization[0]);
|
||||
|
||||
// for (const member of admin.users) {
|
||||
// const userTemp = await db
|
||||
// .insert(schema.users_temp)
|
||||
// .values({
|
||||
// id: member.userId,
|
||||
// email: member.auth.email,
|
||||
// token: member.token || "",
|
||||
// emailVerified: true,
|
||||
// updatedAt: new Date(admin.createdAt) || new Date(),
|
||||
// role: "user",
|
||||
// image: member.auth.image,
|
||||
// createdAt: admin.createdAt,
|
||||
// canAccessToAPI: member.canAccessToAPI || false,
|
||||
// canAccessToDocker: member.canAccessToDocker || false,
|
||||
// canAccessToGitProviders: member.canAccessToGitProviders || false,
|
||||
// canAccessToSSHKeys: member.canAccessToSSHKeys || false,
|
||||
// canAccessToTraefikFiles: member.canAccessToTraefikFiles || false,
|
||||
// canCreateProjects: member.canCreateProjects || false,
|
||||
// canCreateServices: member.canCreateServices || false,
|
||||
// canDeleteProjects: member.canDeleteProjects || false,
|
||||
// canDeleteServices: member.canDeleteServices || false,
|
||||
// accessedProjects: member.accessedProjects || [],
|
||||
// accessedServices: member.accessedServices || [],
|
||||
// })
|
||||
// .returning()
|
||||
// .then((userTemp) => userTemp[0]);
|
||||
|
||||
// await db.insert(schema.account).values({
|
||||
// providerId: "credential",
|
||||
// userId: member?.userId || "",
|
||||
// password: member.auth.password,
|
||||
// is2FAEnabled: member.auth.is2FAEnabled || false,
|
||||
// createdAt: new Date(member.auth.createdAt) || new Date(),
|
||||
// updatedAt: new Date(member.auth.createdAt) || new Date(),
|
||||
// });
|
||||
|
||||
// await db.insert(schema.member).values({
|
||||
// organizationId: organization?.id || "",
|
||||
// userId: userTemp?.id || "",
|
||||
// role: "admin",
|
||||
// createdAt: new Date(member.createdAt) || new Date(),
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// })
|
||||
// .then(() => {
|
||||
// console.log("Migration finished");
|
||||
// })
|
||||
// .catch((error) => {
|
||||
// console.error(error);
|
||||
// });
|
||||
|
||||
// await db
|
||||
// .transaction(async (db) => {
|
||||
// const projects = await db.query.projects.findMany({
|
||||
// with: {
|
||||
// user: {
|
||||
// with: {
|
||||
// organizations: true,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// });
|
||||
// for (const project of projects) {
|
||||
// const _user = await db.update(schema.projects).set({
|
||||
// organizationId: project.user.organizations[0]?.id || "",
|
||||
// });
|
||||
// }
|
||||
// })
|
||||
// .then(() => {
|
||||
// console.log("Migration finished");
|
||||
// })
|
||||
// .catch((error) => {
|
||||
// console.error(error);
|
||||
// });
|
||||
@@ -6,14 +6,18 @@ declare global {
|
||||
var db: PostgresJsDatabase<typeof schema> | undefined;
|
||||
}
|
||||
|
||||
const dbUrl =
|
||||
process.env.DATABASE_URL ||
|
||||
"postgres://dokploy:amukds4wi9001583845717ad2@dokploy-postgres:5432/dokploy";
|
||||
|
||||
export let db: PostgresJsDatabase<typeof schema>;
|
||||
if (process.env.NODE_ENV === "production") {
|
||||
db = drizzle(postgres(process.env.DATABASE_URL!), {
|
||||
db = drizzle(postgres(dbUrl!), {
|
||||
schema,
|
||||
});
|
||||
} else {
|
||||
if (!global.db)
|
||||
global.db = drizzle(postgres(process.env.DATABASE_URL!), {
|
||||
global.db = drizzle(postgres(dbUrl!), {
|
||||
schema,
|
||||
});
|
||||
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
import { drizzle } from "drizzle-orm/postgres-js";
|
||||
import postgres from "postgres";
|
||||
|
||||
const connectionString = process.env.DATABASE_URL!;
|
||||
|
||||
const pg = postgres(connectionString, { max: 1 });
|
||||
const _db = drizzle(pg);
|
||||
|
||||
async function seed() {
|
||||
console.log("> Seed:", process.env.DATABASE_PATH, "\n");
|
||||
|
||||
// const authenticationR = await db
|
||||
// .insert(users)
|
||||
// .values([
|
||||
// {
|
||||
// email: "user1@hotmail.com",
|
||||
// password: password("12345671"),
|
||||
// },
|
||||
// ])
|
||||
// .onConflictDoNothing()
|
||||
// .returning();
|
||||
|
||||
// console.log("\nSemillas Update:", authenticationR.length);
|
||||
}
|
||||
|
||||
seed().catch((e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -111,4 +111,4 @@
|
||||
"node": "^20.16.0",
|
||||
"pnpm": ">=9.12.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
import { defineConfig } from "drizzle-kit";
|
||||
|
||||
export default defineConfig({
|
||||
schema: "./server/db/schema/index.ts",
|
||||
dialect: "postgresql",
|
||||
dbCredentials: {
|
||||
url: process.env.DATABASE_URL!,
|
||||
},
|
||||
out: "drizzle",
|
||||
migrations: {
|
||||
table: "migrations",
|
||||
schema: "public",
|
||||
},
|
||||
});
|
||||
@@ -1,21 +0,0 @@
|
||||
// import { drizzle } from "drizzle-orm/postgres-js";
|
||||
// import { migrate } from "drizzle-orm/postgres-js/migrator";
|
||||
// import postgres from "postgres";
|
||||
|
||||
// const connectionString = process.env.DATABASE_URL!;
|
||||
|
||||
// const sql = postgres(connectionString, { max: 1 });
|
||||
// const db = drizzle(sql);
|
||||
|
||||
// export const migration = async () =>
|
||||
// await migrate(db, { migrationsFolder: "drizzle" })
|
||||
// .then(() => {
|
||||
// console.log("Migration complete");
|
||||
// sql.end();
|
||||
// })
|
||||
// .catch((error) => {
|
||||
// console.log("Migration failed", error);
|
||||
// })
|
||||
// .finally(() => {
|
||||
// sql.end();
|
||||
// });
|
||||
@@ -1,23 +0,0 @@
|
||||
import { sql } from "drizzle-orm";
|
||||
// Credits to Louistiti from Drizzle Discord: https://discord.com/channels/1043890932593987624/1130802621750448160/1143083373535973406
|
||||
import { drizzle } from "drizzle-orm/postgres-js";
|
||||
import postgres from "postgres";
|
||||
|
||||
const connectionString = process.env.DATABASE_URL!;
|
||||
|
||||
const pg = postgres(connectionString, { max: 1 });
|
||||
const db = drizzle(pg);
|
||||
|
||||
const clearDb = async (): Promise<void> => {
|
||||
try {
|
||||
const tablesQuery = sql<string>`DROP SCHEMA public CASCADE; CREATE SCHEMA public; DROP schema drizzle CASCADE;`;
|
||||
const tables = await db.execute(tablesQuery);
|
||||
console.log(tables);
|
||||
await pg.end();
|
||||
} catch (error) {
|
||||
console.error("Error cleaning database", error);
|
||||
} finally {
|
||||
}
|
||||
};
|
||||
|
||||
clearDb();
|
||||
@@ -1,35 +0,0 @@
|
||||
// import bc from "bcrypt";
|
||||
// import { drizzle } from "drizzle-orm/postgres-js";
|
||||
// import postgres from "postgres";
|
||||
// import { users } from "./schema";
|
||||
|
||||
// const connectionString = process.env.DATABASE_URL!;
|
||||
|
||||
// const pg = postgres(connectionString, { max: 1 });
|
||||
// const db = drizzle(pg);
|
||||
|
||||
// function password(txt: string) {
|
||||
// return bc.hashSync(txt, 10);
|
||||
// }
|
||||
|
||||
// async function seed() {
|
||||
// console.log("> Seed:", process.env.DATABASE_PATH, "\n");
|
||||
|
||||
// // const authenticationR = await db
|
||||
// // .insert(users)
|
||||
// // .values([
|
||||
// // {
|
||||
// // email: "user1@hotmail.com",
|
||||
// // password: password("12345671"),
|
||||
// // },
|
||||
// // ])
|
||||
// // .onConflictDoNothing()
|
||||
// // .returning();
|
||||
|
||||
// // console.log("\nSemillas Update:", authenticationR.length);
|
||||
// }
|
||||
|
||||
// seed().catch((e) => {
|
||||
// console.error(e);
|
||||
// process.exit(1);
|
||||
// });
|
||||
Reference in New Issue
Block a user