mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-23 04:18:45 +00:00
Revert "refactor: update database connection handling and remove unused migra…"
This commit is contained in:
@@ -7,10 +7,6 @@ function prepareDefine(config: DotenvParseOutput | undefined) {
|
|||||||
const define = {};
|
const define = {};
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
for (const [key, value] of Object.entries(config)) {
|
for (const [key, value] of Object.entries(config)) {
|
||||||
// Skip DATABASE_URL to allow runtime environment variable override
|
|
||||||
if (key === "DATABASE_URL") {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
define[`process.env.${key}`] = JSON.stringify(value);
|
define[`process.env.${key}`] = JSON.stringify(value);
|
||||||
}
|
}
|
||||||
@@ -18,7 +14,6 @@ function prepareDefine(config: DotenvParseOutput | undefined) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const define = prepareDefine(result.parsed);
|
const define = prepareDefine(result.parsed);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
esbuild
|
esbuild
|
||||||
.build({
|
.build({
|
||||||
|
|||||||
@@ -0,0 +1,149 @@
|
|||||||
|
// 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,18 +6,14 @@ declare global {
|
|||||||
var db: PostgresJsDatabase<typeof schema> | undefined;
|
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>;
|
export let db: PostgresJsDatabase<typeof schema>;
|
||||||
if (process.env.NODE_ENV === "production") {
|
if (process.env.NODE_ENV === "production") {
|
||||||
db = drizzle(postgres(dbUrl!), {
|
db = drizzle(postgres(process.env.DATABASE_URL!), {
|
||||||
schema,
|
schema,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
if (!global.db)
|
if (!global.db)
|
||||||
global.db = drizzle(postgres(dbUrl!), {
|
global.db = drizzle(postgres(process.env.DATABASE_URL!), {
|
||||||
schema,
|
schema,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
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: "[email protected]",
|
||||||
|
// 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",
|
"node": "^20.16.0",
|
||||||
"pnpm": ">=9.12.0"
|
"pnpm": ">=9.12.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
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",
|
||||||
|
},
|
||||||
|
});
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
// 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();
|
||||||
|
// });
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
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();
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
// 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: "[email protected]",
|
||||||
|
// // 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