aboutsummaryrefslogtreecommitdiff
path: root/apps/backend/scripts/migrate.ts
blob: bb8bdcf884eaa0bda65c53d8633c2f84468ce056 (plain) (blame)
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
import { config } from "dotenv";
import { drizzle } from "drizzle-orm/postgres-js";
import { migrate } from "drizzle-orm/postgres-js/migrator";
import process from "node:process";
import postgres from "postgres";

config();

const isProd = process.env.NODE_ENV === "production";
const connectionString = isProd ? process.env.PROD_DATABASE_URL : process.env.DATABASE_URL;

if (!connectionString) {
	throw new Error(`${isProd ? "PROD_DATABASE_URL" : "DATABASE_URL"} is not set`);
}

console.log("Connecting to:", connectionString.replace(/:[^:@]+@/, ":****@")); // Log sanitized connection string

const migrationClient = postgres(connectionString, { max: 1 });

async function main() {
	console.log("Running migrations...");

	try {
		const db = drizzle(migrationClient);
		await migrate(db, { migrationsFolder: "./drizzle" });
		console.log("Migrations completed!");
	} catch (error) {
		console.error("Migration failed:", error);
	} finally {
		await migrationClient.end();
	}
}

main().catch((err) => {
	console.error("Unexpected error:", err);
	process.exit(1);
});