-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdocker-entrypoint.js
More file actions
87 lines (73 loc) · 2.27 KB
/
Copy pathdocker-entrypoint.js
File metadata and controls
87 lines (73 loc) · 2.27 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env node
/* eslint-disable @typescript-eslint/no-require-imports */
const Database = require("better-sqlite3");
const fs = require("fs");
const path = require("path");
const { spawn } = require("child_process");
const dbPath = process.env.DATABASE_URL?.replace("file:", "") || "./data/sparqi.db";
const dbDir = path.dirname(dbPath);
const migrationsDir = "./drizzle";
// Ensure data directory exists
if (!fs.existsSync(dbDir)) {
fs.mkdirSync(dbDir, { recursive: true });
console.log(`Created data directory: ${dbDir}`);
}
// Run migrations
console.log("Running database migrations...");
const db = new Database(dbPath);
// Create migrations tracking table if it doesn't exist
db.exec(`
CREATE TABLE IF NOT EXISTS __drizzle_migrations (
id INTEGER PRIMARY KEY,
hash TEXT NOT NULL,
created_at INTEGER
)
`);
// Get applied migrations
const applied = new Set(
db.prepare("SELECT hash FROM __drizzle_migrations").all().map((r) => r.hash)
);
// Get migration files
const migrationFiles = fs
.readdirSync(migrationsDir)
.filter((f) => f.endsWith(".sql"))
.sort();
// Apply pending migrations
for (const file of migrationFiles) {
if (applied.has(file)) {
console.log(` Skipping ${file} (already applied)`);
continue;
}
const sql = fs.readFileSync(path.join(migrationsDir, file), "utf-8");
console.log(` Applying ${file}...`);
try {
db.exec(sql);
db.prepare("INSERT INTO __drizzle_migrations (hash, created_at) VALUES (?, ?)").run(
file,
Date.now()
);
} catch (err) {
// Handle case where tables/columns already exist (e.g., migrating from local dev)
if (err.message.includes("already exists") || err.message.includes("duplicate column")) {
console.log(` Schema already up to date, marking ${file} as applied`);
db.prepare("INSERT INTO __drizzle_migrations (hash, created_at) VALUES (?, ?)").run(
file,
Date.now()
);
} else {
console.error(` Failed to apply ${file}:`, err.message);
process.exit(1);
}
}
}
db.close();
console.log("Migrations complete.");
// Start the Next.js server
console.log("Starting Next.js server...");
const server = spawn("node", ["server.js"], {
stdio: "inherit",
env: process.env,
});
server.on("close", (code) => {
process.exit(code);
});