/ opencodehub / scripts / migrate.ts
Swadhin Biswas Feature/ai code reviewer (#79) · 8/16/2026
cfd400d
72 lines · 2.3 KB
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
/**
 * Apply committed Drizzle migrations to the database.
 *
 * Used by the Docker entrypoint and production deployment to apply schema
 * migrations deterministically. Requires only `drizzle-orm` (a runtime
 * dependency) — not `drizzle-kit` — so it works in slim production images.
 *
 * Usage:  bun scripts/migrate.ts
 * Env:    DATABASE_URL, DATABASE_DRIVER, DATABASE_SSL, DATABASE_SSL_REJECT_UNAUTHORIZED
 */

import { createClient } from "@libsql/client";
import { config } from "dotenv";
import Database from "better-sqlite3";
import pg from "pg";

config();

async function run() {
  const driver = process.env.DATABASE_DRIVER || "postgres";

  if (driver === "postgres") {
    const { drizzle } = await import("drizzle-orm/node-postgres");
    const { migrate } = await import("drizzle-orm/node-postgres/migrator");

    const url =
      process.env.DATABASE_URL ||
      "postgresql://opencodehub:opencodehub@localhost:5432/opencodehub";
    const ssl =
      process.env.DATABASE_SSL === "true"
        ? { rejectUnauthorized: process.env.DATABASE_SSL_REJECT_UNAUTHORIZED !== "false" }
        : undefined;

    const client = new pg.Client({ connectionString: url, ssl });
    await client.connect();
    try {
      const db = drizzle(client);
      await migrate(db, { migrationsFolder: "./drizzle" });
    } finally {
      await client.end();
    }
  } else if (driver === "sqlite") {
    const { drizzle } = await import("drizzle-orm/better-sqlite3");
    const { migrate } = await import("drizzle-orm/better-sqlite3/migrator");

    const url = process.env.DATABASE_URL || "./data/opencodehub.db";
    const client = new Database(url);
    try {
      const db = drizzle(client);
      await migrate(db, { migrationsFolder: "./drizzle" });
    } finally {
      client.close();
    }
  } else {
    // libsql / turso
    const { drizzle } = await import("drizzle-orm/libsql");
    const { migrate } = await import("drizzle-orm/libsql/migrator");

    const url = process.env.DATABASE_URL || "libsql://localhost:8000";
    const client = createClient({ url });
    const db = drizzle(client);
    await migrate(db, { migrationsFolder: "./drizzle" });
  }

  console.log("✅ Database migrations applied");
}

run().catch((err) => {
  console.error("❌ Database migration failed:", err);
  process.exit(1);
});