-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathrunner.spec.ts
196 lines (167 loc) · 5.8 KB
/
runner.spec.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
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
import type { ClientBase } from 'pg';
import { describe, expect, it, vi } from 'vitest';
import { runner } from '../src';
describe('runner', () => {
it('should return a function', () => {
expect(runner).toBeTypeOf('function');
});
it('should throw an error when not options passed', async () => {
await expect(
// @ts-expect-error: runner needs options
runner()
).rejects.toThrow(
new TypeError(
"Cannot destructure property 'log' of 'options' as it is undefined."
)
);
});
it.todo(
'should throw an error when no databaseUrl or dbClient passed',
async () => {
await expect(
// @ts-expect-error: runner needs options
runner({ log: console.log })
).rejects.toThrow(
new Error('You must provide either a databaseUrl or a dbClient')
);
}
);
it('should execute a basic up migration', async () => {
const executedMigrations: Array<{
id: number;
name: string;
run_on: Date;
}> = [];
let id = 1;
const dbClient = {
query: vi.fn((query) => {
switch (query) {
case 'SELECT pg_try_advisory_lock(7241865325823964) AS "lockObtained"': {
return Promise.resolve({
rows: [{ lockObtained: true }], // lock obtained
});
}
case "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'pgmigrations'": {
return Promise.resolve({
rows: [], // no migration table
});
}
case 'CREATE TABLE "public"."pgmigrations" ( id SERIAL PRIMARY KEY, name varchar(255) NOT NULL, run_on timestamp NOT NULL)': {
return Promise.resolve({}); // migration table created
}
case 'SELECT name FROM "public"."pgmigrations" ORDER BY run_on, id': {
return Promise.resolve({
rows: executedMigrations,
});
}
case 'BEGIN;': {
return Promise.resolve({}); // transaction started
}
case 'COMMIT;': {
return Promise.resolve({}); // transaction committed
}
default: {
if (
query.startsWith(
'INSERT INTO "public"."pgmigrations" (name, run_on) VALUES'
)
) {
const name: string =
/VALUES \('([^']+)'/.exec(query as string)?.[1] ?? 'failed'; // migration name
// insert migration
executedMigrations.push({
id: id++,
name,
run_on: new Date(),
});
return Promise.resolve({}); // migration inserted
}
break;
}
}
// bypass migration queries
return Promise.resolve({ rows: [{}] });
}),
} as unknown as ClientBase;
await expect(
runner({
dbClient,
migrationsTable: 'pgmigrations',
// We use cockroach migrations for now, as they are more simple
// We either could mock the migration files later or define specific migrations for unit-testing
dir: 'test/cockroach',
direction: 'up',
})
).resolves.not.toThrow();
expect(executedMigrations).toHaveLength(12);
});
it('should execute a basic down migration', async () => {
const executedMigrations: Array<{
id: number;
name: string;
run_on: Date;
}> = [
{ id: 1, name: '004_table', run_on: new Date() },
{ id: 2, name: '006_table_rename', run_on: new Date() },
];
const dbClient = {
query: vi.fn((query) => {
switch (query) {
case 'SELECT pg_try_advisory_lock(7241865325823964) AS "lockObtained"': {
return Promise.resolve({
rows: [{ lockObtained: true }], // lock obtained
});
}
case "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'pgmigrations'": {
return Promise.resolve({
rows: [{}], // migration table exists
});
}
case "SELECT constraint_name FROM information_schema.table_constraints WHERE table_schema = 'public' AND table_name = 'pgmigrations' AND constraint_type = 'PRIMARY KEY'": {
return Promise.resolve({}); // no primary key constraint found
}
case 'ALTER TABLE "public"."pgmigrations" ADD PRIMARY KEY (id)': {
return Promise.resolve({}); // primary key constraint added
}
case 'SELECT name FROM "public"."pgmigrations" ORDER BY run_on, id': {
return Promise.resolve({
rows: executedMigrations,
});
}
case 'BEGIN;': {
return Promise.resolve({}); // transaction started
}
case 'COMMIT;': {
return Promise.resolve({}); // transaction committed
}
default: {
if (
query.startsWith(
'DELETE FROM "public"."pgmigrations" WHERE name='
)
) {
// delete migration
executedMigrations.pop();
return Promise.resolve({}); // migration deleted
}
break;
}
}
// bypass migration queries
return Promise.resolve({ rows: [{}] });
}),
} as unknown as ClientBase;
await expect(
runner({
dbClient,
migrationsTable: 'pgmigrations',
// We use cockroach migrations for now, as they are more simple
// We either could mock the migration files later or define specific migrations for unit-testing
dir: 'test/cockroach',
direction: 'down',
count: 2,
})
).resolves.not.toThrow();
expect(executedMigrations).toHaveLength(0);
});
});