-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit-sqlite.js
More file actions
216 lines (178 loc) · 6.42 KB
/
Copy pathinit-sqlite.js
File metadata and controls
216 lines (178 loc) · 6.42 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* eslint-disable @typescript-eslint/no-require-imports */
const fs = require("node:fs");
const path = require("node:path");
const Database = require("better-sqlite3");
function getDatabasePath() {
const dbUrl = process.env.DATABASE_URL || "file:/app/data/prod.db";
if (!dbUrl.startsWith("file:")) {
throw new Error(`Only file: DATABASE_URL is supported, got: ${dbUrl}`);
}
const raw = dbUrl.slice("file:".length).split("?")[0];
if (!raw) {
throw new Error(`Invalid file: DATABASE_URL, missing path: ${dbUrl}`);
}
if (raw.startsWith("/")) {
return raw;
}
return path.resolve(process.cwd(), raw);
}
function getMigrationSqlFiles() {
const migrationsDir = path.join(process.cwd(), "prisma", "migrations");
if (!fs.existsSync(migrationsDir)) {
throw new Error(`Missing migrations directory: ${migrationsDir}`);
}
return fs
.readdirSync(migrationsDir, { withFileTypes: true })
.filter((entry) => entry.isDirectory())
.map((entry) => path.join(migrationsDir, entry.name, "migration.sql"))
.filter((filePath) => fs.existsSync(filePath))
.sort();
}
function tableInfo(db, tableName) {
return db.prepare(`PRAGMA table_info("${tableName}")`).all();
}
function hasColumn(db, tableName, columnName) {
return tableInfo(db, tableName).some((column) => column.name === columnName);
}
function getColumn(db, tableName, columnName) {
return tableInfo(db, tableName).find((column) => column.name === columnName) || null;
}
function normalizeDefaultValue(value) {
if (value == null) {
return null;
}
return String(value).replace(/^'+|'+$/g, "").toUpperCase();
}
function ensureTransactionSchema(db) {
const hasQuantity = hasColumn(db, "Transaction", "quantity");
const hasRate = hasColumn(db, "Transaction", "rate");
const categoryColumn = getColumn(db, "Transaction", "categoryId");
const categoryIsRequired = Boolean(categoryColumn && categoryColumn.notnull === 1);
if (hasQuantity && hasRate && !categoryIsRequired) {
return;
}
const quantityExpr = hasQuantity ? 'COALESCE("quantity", 1)' : "1";
const rateExpr = hasRate ? '"rate"' : '"amount"';
db.exec("PRAGMA defer_foreign_keys=ON;");
db.exec("PRAGMA foreign_keys=OFF;");
try {
db.exec(`
CREATE TABLE "new_Transaction" (
"id" TEXT NOT NULL PRIMARY KEY,
"userId" TEXT NOT NULL,
"accountId" TEXT NOT NULL,
"categoryId" TEXT,
"type" TEXT NOT NULL,
"amount" REAL NOT NULL,
"quantity" REAL NOT NULL DEFAULT 1,
"rate" REAL,
"date" DATETIME NOT NULL,
"description" TEXT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
CONSTRAINT "Transaction_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "Transaction_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "Account" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "Transaction_categoryId_fkey" FOREIGN KEY ("categoryId") REFERENCES "Category" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);
`);
db.exec(`
INSERT INTO "new_Transaction" (
"id", "userId", "accountId", "categoryId", "type", "amount", "quantity", "rate", "date", "description", "createdAt", "updatedAt"
)
SELECT
"id",
"userId",
"accountId",
"categoryId",
"type",
"amount",
${quantityExpr},
${rateExpr},
"date",
"description",
"createdAt",
"updatedAt"
FROM "Transaction";
`);
db.exec("DROP TABLE \"Transaction\";");
db.exec("ALTER TABLE \"new_Transaction\" RENAME TO \"Transaction\";");
console.log("[DB INIT] Rebuilt Transaction table to latest schema");
} finally {
db.exec("PRAGMA foreign_keys=ON;");
}
}
function ensureAccountDefaults(db) {
const currencyColumn = getColumn(db, "Account", "currency");
const currentDefault = normalizeDefaultValue(currencyColumn ? currencyColumn.dflt_value : null);
const expectedDefault = "BDT";
if (currentDefault === expectedDefault) {
return;
}
db.exec("PRAGMA defer_foreign_keys=ON;");
db.exec("PRAGMA foreign_keys=OFF;");
try {
db.exec(`
CREATE TABLE "new_Account" (
"id" TEXT NOT NULL PRIMARY KEY,
"userId" TEXT NOT NULL,
"name" TEXT NOT NULL,
"type" TEXT NOT NULL,
"currency" TEXT NOT NULL DEFAULT 'BDT',
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
CONSTRAINT "Account_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
`);
db.exec(`
INSERT INTO "new_Account" ("id", "userId", "name", "type", "currency", "createdAt", "updatedAt")
SELECT "id", "userId", "name", "type", "currency", "createdAt", "updatedAt" FROM "Account";
`);
db.exec("DROP TABLE \"Account\";");
db.exec("ALTER TABLE \"new_Account\" RENAME TO \"Account\";");
console.log("[DB INIT] Updated Account.currency default to BDT");
} finally {
db.exec("PRAGMA foreign_keys=ON;");
}
}
function ensureSchemaUpgrades(db) {
const transactionTable = db
.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='Transaction'")
.get();
const accountTable = db
.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='Account'")
.get();
if (transactionTable) {
ensureTransactionSchema(db);
}
if (accountTable) {
ensureAccountDefaults(db);
}
}
function run() {
const dbPath = getDatabasePath();
fs.mkdirSync(path.dirname(dbPath), { recursive: true });
const db = new Database(dbPath);
try {
const userTable = db
.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='User'")
.get();
if (!userTable) {
const migrationFiles = getMigrationSqlFiles();
if (migrationFiles.length === 0) {
throw new Error("No migration.sql files found in prisma/migrations");
}
for (const filePath of migrationFiles) {
const sql = fs.readFileSync(filePath, "utf8");
db.exec(sql);
console.log(`[DB INIT] Applied ${path.relative(process.cwd(), filePath)}`);
}
console.log(`[DB INIT] SQLite schema initialized at ${dbPath}`);
return;
}
console.log(`[DB INIT] Schema already present at ${dbPath}, checking upgrades`);
ensureSchemaUpgrades(db);
} finally {
db.close();
}
}
run();