-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.test.ts
More file actions
209 lines (170 loc) · 5.65 KB
/
migrate.test.ts
File metadata and controls
209 lines (170 loc) · 5.65 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
import assert from "node:assert";
import fs from "node:fs/promises";
import path from "node:path";
import { afterEach, beforeEach, describe, it } from "node:test";
import {
availableMigrations,
createMigration,
createMigrationsTable,
insertMigration,
latestMigration,
lockMigrations,
type MigrationRow,
migrationRegex,
pendingMigrations,
runMigration,
unlockMigrations,
} from "./migrate.ts";
import { testDB } from "./test-help.ts";
const db = testDB();
type Org = {
id: number;
name: string;
};
const basicMigrations = [
{
path: path.join(
process.cwd(),
"fixtures/migrations/basic/20241231011345-create-orgs.js",
),
id: "20241231011345",
},
{
path: path.join(
process.cwd(),
"fixtures/migrations/basic/20250302090823-create-docs.ts",
),
id: "20250302090823",
},
{
path: path.join(
process.cwd(),
"fixtures/migrations/basic/20250307212006-create-users.mjs",
),
id: "20250307212006",
},
];
describe("migrations", () => {
beforeEach(async () => {
await db.query`DROP TABLE IF EXISTS migrations`;
});
describe("createMigrationsTable", () => {
it("creates a migrations table", async () => {
await createMigrationsTable(db);
const result = await db.one<{
count: number;
}>`SELECT count(id)::int FROM migrations`;
assert.ok(result !== undefined);
assert.equal(result.count, 0);
});
});
describe("insertMigration", () => {
it("inserts a migration", async () => {
await createMigrationsTable(db);
await db.query`INSERT INTO migrations (id) VALUES ('a')`;
const result =
await db.one<MigrationRow>`SELECT * FROM migrations WHERE id = 'a'`;
assert.ok(result !== undefined);
assert.equal(result.id, "a");
});
});
describe("latestMigration", () => {
it("gets latest MigrationRow", async () => {
await createMigrationsTable(db);
await db.query`INSERT INTO migrations (id) VALUES ('a'), ('b'), ('c')`;
const result = await latestMigration(db);
assert.ok(result !== undefined);
assert.equal(result.id, "c");
});
});
describe("availableMigrations", () => {
it("gets available migrations", async () => {
const migrations = await availableMigrations(
"./fixtures/migrations/basic/",
);
assert.equal(migrations.length, 3);
assert.deepEqual(migrations, basicMigrations);
});
});
describe("pendingMigrations", () => {
it("gets pending migrations", async () => {
await createMigrationsTable(db);
await db.query`INSERT INTO migrations (id) VALUES ('20241231011345')`;
const migrations = await availableMigrations(
"./fixtures/migrations/basic/",
);
assert.equal(migrations.length, 3);
const latest = await latestMigration(db);
assert.ok(latest !== undefined);
const pending = pendingMigrations(migrations, latest);
assert.equal(pending.length, 2);
assert.deepEqual(pending, [basicMigrations[1], basicMigrations[2]]);
});
it("returns all migrations if none have been run", async () => {
await createMigrationsTable(db);
const migrations = await availableMigrations(
"./fixtures/migrations/basic/",
);
const pending = pendingMigrations(migrations, undefined);
assert.equal(pending.length, 3);
assert.deepEqual(pending, basicMigrations);
});
});
describe("createMigration", () => {
it("creates a migration", async () => {
const filename = await createMigration("./tmp", "create-works");
assert.ok(filename !== undefined);
const base = path.basename(filename);
assert.ok(migrationRegex.test(base));
assert.ok(base.endsWith("create-works.js"));
const stat = await fs.stat(filename);
assert.ok(stat.isFile());
await fs.rm(filename);
});
it("returns undefined if migration already exists", async (t) => {
t.mock.timers.enable({ apis: ["Date"] });
const filename = await createMigration("./tmp", "colission");
assert.ok(filename !== undefined);
assert.equal(await createMigration("./tmp", "colission"), undefined);
await fs.rm(filename);
});
});
describe("runMigration", () => {
afterEach(async () => {
await db.query`DROP TABLE IF EXISTS orgs CASCADE`;
});
it("runs a migration", async () => {
const migrationPath = {
path: "./fixtures/migrations/basic/20241231011345-create-orgs.js",
id: "20241231011345",
};
await runMigration(db, migrationPath);
const result = await db.one<MigrationRow>`SELECT * FROM migrations`;
assert.ok(result !== undefined);
assert.equal(result.id, migrationPath.id);
assert.notEqual(result.createdAt, null);
const org =
await db.one<Org>`INSERT INTO orgs (name) VALUES ('Migration Runners') RETURNING *`;
assert.ok(org !== undefined);
assert.equal(org.name, "Migration Runners");
});
it("throws if migration has already been run", async () => {
const migrationPath = {
path: "./fixtures/migrations/basic/20241231011345-create-orgs.js",
id: "20241231011345",
};
await createMigrationsTable(db);
await insertMigration(db, migrationPath.id);
await assert.rejects(runMigration(db, migrationPath));
});
it("throws if migration is locked", async () => {
const migrationPath = {
path: "./fixtures/migrations/basic/20241231011345-create-orgs.js",
id: "20241231011345",
};
await lockMigrations(db);
await assert.rejects(runMigration(testDB(), migrationPath));
await unlockMigrations(db);
});
});
});