Skip to content

Commit 7ffc5e0

Browse files
committed
fix 1.10.1 migration script
1 parent a1b2e36 commit 7ffc5e0

File tree

1 file changed

+44
-52
lines changed

1 file changed

+44
-52
lines changed
Lines changed: 44 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,47 @@
1-
import { __DIRNAME, APP_PATH } from "@server/lib/consts";
1+
import { APP_PATH } from "@server/lib/consts";
22
import Database from "better-sqlite3";
33
import path from "path";
44

55
const version = "1.10.1";
66

77
export default async function migration() {
8-
console.log(`Running setup script ${version}...`);
8+
console.log(`Running setup script ${version}...`);
99

10-
const location = path.join(APP_PATH, "db", "db.sqlite");
11-
const db = new Database(location);
10+
const location = path.join(APP_PATH, "db", "db.sqlite");
11+
const db = new Database(location);
1212

13-
try {
14-
db.pragma("foreign_keys = OFF");
13+
try {
14+
db.pragma("foreign_keys = OFF");
1515

16-
db.transaction(() => {
17-
db.exec(`PRAGMA foreign_keys = OFF;
18-
19-
-- 1. Rename the old table
20-
ALTER TABLE targets RENAME TO targets_old;
21-
22-
-- 2. Create the new table
23-
CREATE TABLE targets (
24-
targetId INTEGER PRIMARY KEY AUTOINCREMENT,
25-
resourceId INTEGER NOT NULL,
26-
siteId INTEGER NOT NULL,
27-
ip TEXT NOT NULL,
28-
method TEXT,
29-
port INTEGER NOT NULL,
30-
internalPort INTEGER,
31-
enabled INTEGER NOT NULL DEFAULT 1,
32-
path TEXT,
33-
pathMatchType TEXT,
34-
FOREIGN KEY (resourceId) REFERENCES resources(resourceId) ON DELETE CASCADE,
35-
FOREIGN KEY (siteId) REFERENCES sites(siteId) ON DELETE CASCADE
16+
db.transaction(() => {
17+
db.exec(`ALTER TABLE "targets" RENAME TO "targets_old";
18+
--> statement-breakpoint
19+
CREATE TABLE "targets" (
20+
"targetId" INTEGER PRIMARY KEY AUTOINCREMENT,
21+
"resourceId" INTEGER NOT NULL,
22+
"siteId" INTEGER NOT NULL,
23+
"ip" TEXT NOT NULL,
24+
"method" TEXT,
25+
"port" INTEGER NOT NULL,
26+
"internalPort" INTEGER,
27+
"enabled" INTEGER NOT NULL DEFAULT 1,
28+
"path" TEXT,
29+
"pathMatchType" TEXT,
30+
FOREIGN KEY ("resourceId") REFERENCES "resources"("resourceId") ON UPDATE no action ON DELETE cascade,
31+
FOREIGN KEY ("siteId") REFERENCES "sites"("siteId") ON UPDATE no action ON DELETE cascade
3632
);
37-
38-
-- 3. Copy data *from the old table*
39-
INSERT INTO targets (
40-
targetId,
41-
resourceId,
42-
siteId,
43-
ip,
44-
method,
45-
port,
46-
internalPort,
47-
enabled,
48-
path,
49-
pathMatchType
33+
--> statement-breakpoint
34+
INSERT INTO "targets" (
35+
"targetId",
36+
"resourceId",
37+
"siteId",
38+
"ip",
39+
"method",
40+
"port",
41+
"internalPort",
42+
"enabled",
43+
"path",
44+
"pathMatchType"
5045
)
5146
SELECT
5247
targetId,
@@ -59,19 +54,16 @@ SELECT
5954
enabled,
6055
path,
6156
pathMatchType
62-
FROM targets_old;
63-
64-
-- 4. Drop the old table
65-
DROP TABLE targets_old;
66-
67-
PRAGMA foreign_keys = ON;`);
68-
});
57+
FROM "targets_old";
58+
--> statement-breakpoint
59+
DROP TABLE "targets_old";`);
60+
})();
6961

70-
db.pragma("foreign_keys = ON");
62+
db.pragma("foreign_keys = ON");
7163

72-
console.log(`Migrated database`);
73-
} catch (e) {
74-
console.log("Failed to migrate db:", e);
75-
throw e;
76-
}
64+
console.log(`Migrated database`);
65+
} catch (e) {
66+
console.log("Failed to migrate db:", e);
67+
throw e;
68+
}
7769
}

0 commit comments

Comments
 (0)