mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2026-07-11 13:35:13 +10:00
lot of bugfixes, better migration script
This commit is contained in:
@@ -9,15 +9,27 @@ export async function resetDatabase() {
|
||||
const pool = new Pool({ connectionString: env.DATABASE_URL });
|
||||
const db = drizzle({ client: pool });
|
||||
|
||||
// Extract the username from a PostgreSQL connection string like: postgresql://postgres:password@localhost:5432/db
|
||||
const username = (() => {
|
||||
try {
|
||||
const match = env.DATABASE_URL.match(/^postgres(?:ql)?:\/\/([^:]+):[^@]+@/);
|
||||
return match ? match[1] : undefined;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
})();
|
||||
|
||||
console.log("🔑 Username:", username);
|
||||
|
||||
try {
|
||||
await db.transaction(async (tx) => {
|
||||
await tx.execute(sql`DROP SCHEMA drizzle CASCADE`);
|
||||
await tx.execute(sql`CREATE SCHEMA drizzle`);
|
||||
await tx.execute(sql`GRANT ALL ON SCHEMA drizzle TO postgres`);
|
||||
await tx.execute(sql.raw(`GRANT ALL ON SCHEMA drizzle TO ${username}`));
|
||||
|
||||
await tx.execute(sql`DROP SCHEMA public CASCADE`);
|
||||
await tx.execute(sql`CREATE SCHEMA public`);
|
||||
await tx.execute(sql`GRANT ALL ON SCHEMA public TO postgres`);
|
||||
await tx.execute(sql.raw(`GRANT ALL ON SCHEMA public TO ${username}`));
|
||||
});
|
||||
|
||||
console.log("✅ Database reset completed");
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
import { drizzle } from "drizzle-orm/node-postgres";
|
||||
import { Pool } from "pg";
|
||||
import { schema } from "@/integrations/drizzle";
|
||||
import { env } from "@/utils/env";
|
||||
import { hashPassword } from "@/utils/password";
|
||||
import { generateId } from "@/utils/string";
|
||||
|
||||
export async function seedDatabase() {
|
||||
console.log("⌛ Seeding database...");
|
||||
|
||||
const pool = new Pool({ connectionString: env.DATABASE_URL });
|
||||
const db = drizzle({ client: pool, schema });
|
||||
|
||||
try {
|
||||
const userId = generateId();
|
||||
|
||||
await db.insert(schema.user).values({
|
||||
id: userId,
|
||||
name: "Test User",
|
||||
email: "test@test.com",
|
||||
username: "test",
|
||||
displayUsername: "test",
|
||||
emailVerified: true,
|
||||
image: "https://i.pravatar.cc/300",
|
||||
});
|
||||
|
||||
await db.insert(schema.account).values({
|
||||
id: generateId(),
|
||||
userId,
|
||||
accountId: userId,
|
||||
password: await hashPassword("password"),
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("🚨 Database seeding failed:", error);
|
||||
} finally {
|
||||
await pool.end();
|
||||
}
|
||||
}
|
||||
|
||||
if (import.meta.main) {
|
||||
await seedDatabase();
|
||||
}
|
||||
Reference in New Issue
Block a user