-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.ts
More file actions
100 lines (85 loc) · 3.7 KB
/
migrate.ts
File metadata and controls
100 lines (85 loc) · 3.7 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
/**
* Usage: yarn migrate up|down|pending|executed [dev|local|staging|prod] [forumType]
*
* If no environment is specified, you can use the environment variables PG_URL
* and SETTINGS_FILE
*/
import type { ITask } from "pg-promise";
// @ts-ignore This is a javascript file without a .d.ts
import { startSshTunnel } from "./scripts/startup/buildUtil";
import { detectForumType, getDatabaseConfigFromModeAndForumType, getSettingsFileName, getSettingsFilePath, initGlobals, isEnvironmentType, normalizeEnvironmentType } from "./scripts/scriptUtil";
(async () => {
const command = process.argv[2];
if (isEnvironmentType(normalizeEnvironmentType(command))) {
console.error("Please specify the command before the mode");
process.exit(1);
}
const isRunCommand = ["up", "down"].includes(command);
let mode = normalizeEnvironmentType(process.argv[3]);
if (!["up", "down", "pending", "executed"].includes(command)) {
mode = "dev";
}
const forumType = detectForumType();
const forumTypeIsSpecified = forumType !== "none";
console.log(`Running with forum type "${forumType}"`);
const dbConf = getDatabaseConfigFromModeAndForumType(mode, forumType);
if (dbConf.postgresUrl) {
process.env.PG_URL = dbConf.postgresUrl;
}
const args = {
postgresUrl: process.env.PG_URL,
settingsFileName: process.env.SETTINGS_FILE || getSettingsFileName(mode, forumType),
shellMode: false,
};
await startSshTunnel(getDatabaseConfigFromModeAndForumType(mode, forumType).sshTunnelCommand);
if (["dev", "local", "staging", "prod", "xpost"].includes(mode)) {
console.log('Running migrations in mode', mode);
args.settingsFileName = getSettingsFilePath(getSettingsFileName(mode, forumType), forumType);
if (command !== "create") {
process.argv = process.argv.slice(0, 3).concat(process.argv.slice(forumTypeIsSpecified ? 5 : 4));
}
} else if (args.postgresUrl && args.settingsFileName) {
console.log('Using PG_URL and SETTINGS_FILE from environment');
} else {
throw new Error('Unable to run migration without a mode or environment (PG_URL and SETTINGS_FILE)');
}
initGlobals(args, mode==="prod");
const { getSqlClientOrThrow, setSqlClient }: typeof import("./packages/lesswrong/server/sql/sqlClient") = require("./packages/lesswrong/server/sql/sqlClient");
const { createSqlConnection }: typeof import("./packages/lesswrong/server/sqlConnection") = require("./packages/lesswrong/server/sqlConnection");
if (isRunCommand) {
const {initServer} = require("./packages/lesswrong/server/serverStartup");
await initServer(args);
}
let exitCode = 0;
const db = isRunCommand
? getSqlClientOrThrow()
: await createSqlConnection(args.postgresUrl);
try {
await db.tx(async (transaction: ITask<{}>) => {
setSqlClient(transaction as unknown as SqlClient);
setSqlClient(db, "noTransaction");
const { createMigrator } = require("./packages/lesswrong/server/migrations/meta/umzug");
const migrator = await createMigrator(transaction, db);
if (command === "create") {
const name = process.argv[3];
if (!name) {
throw new Error("No name provided for new migration");
}
console.log(`Creating new migration with name "${name}"`);
await migrator.create({name});
} else {
const result = await migrator.runAsCLI();
if (!result) {
// If the migration throws an error it will have already been reported,
// but we need to manually propagate it to the exitCode
exitCode = 1;
}
}
});
} catch (e) {
console.error("An error occurred while running migrations:", e);
exitCode = 1;
}
await db.$pool.end();
process.exit(exitCode);
})();