-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmigrate.ts
More file actions
122 lines (117 loc) · 3.32 KB
/
migrate.ts
File metadata and controls
122 lines (117 loc) · 3.32 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
import { spawnSync } from "node:child_process";
import { opendirSync } from "node:fs";
import { join, dirname } from "node:path";
import { getVariant } from "@repo/database/dbDotEnv";
const __dirname = dirname(__filename);
const projectRoot = join(__dirname, "..");
if (
process.env.HOME === "/vercel" ||
(process.env.GITHUB_ACTIONS === "true" && process.env.GITHUB_TEST !== "test")
) {
console.log("Skipping in production environment");
process.exit(0);
}
const argv = process.argv.slice(2);
const includeAll = argv.includes("--include-all");
const reapplyIdx = argv.indexOf("--reapply");
const reapply = reapplyIdx !== -1;
const reapplyVersion = reapply ? (argv[reapplyIdx + 1] ?? "") : "";
if (reapply && !reapplyVersion) {
console.error("Missing <migration timestamp> after --reapply");
process.exit(1);
}
if (getVariant() === "none") {
console.log("Not using the database");
process.exit(0);
}
if (
getVariant() === "production" ||
(getVariant() === "implicit" &&
!(process.env.SUPABASE_URL || "").includes("127.0.0.1"))
) {
console.log("Refusing to update the production database");
process.exit(0);
}
const startResult = spawnSync("supabase", ["start"], {
cwd: projectRoot,
stdio: "inherit",
});
if (startResult.status) {
process.exit(startResult.status);
}
if (reapply) {
const dir = opendirSync(join(projectRoot, "supabase", "migrations"));
const files = [];
const fre = /^\d{14}_.*/;
/* eslint-disable-next-line no-constant-condition */
while (true) {
const f = dir.readSync();
if (f === null) {
dir.closeSync();
break;
}
if (fre.test(f.name)) {
files.push(f.name.substring(0, 14));
}
}
if (!files.length) {
console.error("No migration files found");
process.exit(1);
}
files.sort();
const lastVersion = files[files.length - 1];
if (reapplyVersion !== lastVersion) {
console.error(
`The expected migration version ${reapplyVersion} does not match the latest version ${lastVersion}.`,
);
process.exit(1);
}
console.log("Reverting migration " + reapplyVersion);
const result = spawnSync(
"supabase",
["migration", "repair", "--status", "reverted", "--local", reapplyVersion],
{
cwd: projectRoot,
stdio: "inherit",
},
);
if (result.status) {
process.exit(result.status);
}
}
const migrationArgs = ["migration", "up", "--local"];
if (includeAll) migrationArgs.push("--include-all");
const migrationResult = spawnSync("supabase", migrationArgs, {
cwd: projectRoot,
});
const stderr = migrationResult.stderr?.toString() || "";
if (
!includeAll &&
migrationResult.status &&
stderr.includes("with --include-all")
) {
console.error(stderr);
console.log("Consider trying again");
process.exit(1);
}
if (migrationResult.status) {
console.error(stderr);
process.exit(migrationResult.status);
}
const migrationLines = stderr
.split("\n")
.filter((line) => line.startsWith("Applying migration "));
if (migrationLines.length > 0) {
console.log(migrationLines.join("\n"));
console.log("Migrations were applied, regenerating dbTypes");
const generateResult = spawnSync("pnpm", ["run", "gentypes"], {
cwd: projectRoot,
stdio: "inherit",
});
if (generateResult.status) {
console.error("Failed to generate types");
process.exit(1);
}
} else {
console.log("No migrations were applied");
}