-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate.ts
executable file
·165 lines (148 loc) · 4.14 KB
/
migrate.ts
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
#!/usr/bin/env -S deno run -A
import { join } from "$std/path/mod.ts";
import postgres from "postgresjs/mod.js";
import type { Notice, Options } from "postgresjs/types/index.d.ts";
// lower 64 bits of sha3-256("migrations")
const LOCK_ID = -6902354483765142115n;
const DATABASE_URL = Deno.env.get("DATABASE_URL");
let notice: Notice | undefined = undefined;
const pg_opts: Options<Record<string, never>> = {
max: 1,
onnotice: (n) => notice = n,
};
const sql = DATABASE_URL ? postgres(DATABASE_URL, pg_opts) : postgres(pg_opts);
async function init() {
await sql`
CREATE TABLE IF NOT EXISTS migrations(
id SERIAL PRIMARY KEY,
name character varying,
date timestamp with time zone NOT NULL DEFAULT now()
)
`;
if (notice) {
if (notice.code && notice.code === "42P07") {
console.warn("migrations table already exists");
} else {
console.warn(`unexpected: ${notice.message}`);
}
} else {
console.log("sucessfully initialized migrations table");
}
}
async function lock() {
let attempts = 0;
while (true) {
const [lock] = await sql`
SELECT pg_try_advisory_lock(${LOCK_ID})
`;
if (lock && lock.pg_try_advisory_lock) {
break;
} else {
attempts += 1;
if (attempts > 15) {
throw new Error("failed to get database lock after 15s");
}
}
await new Promise((res) => setTimeout(res, 1000));
}
}
async function unlock() {
await sql`
SELECT pg_advisory_unlock(${LOCK_ID})
`;
}
async function appliedMigrations(): Promise<{ name: string; date: Date }[]> {
return await sql`
SELECT name, date FROM migrations
`;
}
async function migrate() {
await lock();
try {
const applied = new Set(
(await appliedMigrations()).map(({ name }) => name),
);
const unapplied = [];
const migrations_dir = Deno.args[1] ?? "migrations";
for await (const file of Deno.readDir(migrations_dir)) {
if (file.isFile && !applied.has(file.name)) {
const migration = await Deno.readTextFile(
join(migrations_dir, file.name),
);
unapplied.push({ name: file.name, migration });
}
}
unapplied.sort(({ name: a }, { name: b }) =>
(a < b) ? -1 : (a > b) ? 1 : 0
);
if (unapplied.length === 0) {
console.log("all migrations up to date");
} else {
for (const migration of unapplied) {
await sql.begin(async (sql) => {
await sql`
INSERT INTO migrations (name) VALUES (${migration.name})
`;
await sql.unsafe(migration.migration);
});
console.log(`successfully applied ${migration.name}`);
}
}
} finally {
await unlock();
}
}
async function status() {
await lock();
try {
const applied = await appliedMigrations();
const local = new Set();
const migrations_dir = Deno.args[1] ?? "migrations";
for await (const file of Deno.readDir(migrations_dir)) {
if (file.isFile) {
local.add(file.name);
}
}
const missing = new Set();
for (const migration of applied) {
if (!local.has(migration.name)) {
missing.add(migration.name);
}
}
console.log(`Total migrations: ${local.size + missing.size}`);
console.log(`Applied: ${applied.length}`);
console.log(`Unapplied: ${local.size - (applied.length - missing.size)}`);
console.log(`Missing: ${missing.size}`);
applied.sort(({ date: a }, { date: b }) => (a < b) ? 1 : (a > b) ? -1 : 0);
console.log(`Most recent: ${applied[0].name} at ${applied[0].date}`);
} finally {
await unlock();
}
}
function usage() {
console.log(
"usage: ./migrate.ts init | migrate [migration_dir] | status [migration_dir]",
);
}
const commands: Record<string, () => Promise<void>> = {
init: init,
migrate: migrate,
status: status,
};
const args = Deno.args;
if (
args.length !== 0 && commands[args[0]] &&
((args[0] === "init" && args.length === 1) ||
(args[0] !== "init" && args.length <= 2))
) {
try {
await commands[Deno.args[0]]();
await sql.end();
} catch (e) {
console.log(`./migrate.ts: ${e.message}`);
Deno.exit(1);
}
} else {
usage();
Deno.exit(1);
}