-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathmigration.spec.ts
298 lines (244 loc) · 8.03 KB
/
migration.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import { isAbsolute, resolve } from 'node:path';
import type { Mock } from 'vitest';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { RunnerOption } from '../src';
import type { DBConnection } from '../src/db';
import type { Logger } from '../src/logger';
import {
getMigrationFilePaths,
getNumericPrefix,
Migration,
} from '../src/migration';
const callbackMigration = '1414549381268_names.js';
const promiseMigration = '1414549381268_names_promise.js';
const reverseMigration = '1739900132875_names_reversed.js';
const migrationsTable = 'pgmigrations';
const actionsCallback = await import(`./${callbackMigration}`);
const actionsPromise = await import(`./${promiseMigration}`);
const reversePromise = await import(`./${reverseMigration}`);
describe('migration', () => {
const dbMock = {} as DBConnection;
const logger: Logger = {
info: () => null,
warn: () => null,
error: () => null,
};
const options = { migrationsTable } as RunnerOption;
let queryMock: Mock;
beforeEach(() => {
queryMock = vi.fn();
dbMock.query = queryMock;
});
describe('getNumericPrefix', () => {
it('should get timestamp for normal timestamp', () => {
const now = Date.now();
expect(getNumericPrefix(String(now), logger)).toBe(now);
});
it('should get timestamp for shortened iso format', () => {
const now = new Date();
expect(
getNumericPrefix(now.toISOString().replace(/\D/g, ''), logger)
).toBe(now.valueOf());
});
});
describe('getMigrationFilePaths', () => {
it('should resolve files directly in `dir`', async () => {
const dir = 'test/migrations';
const resolvedDir = resolve(dir);
const filePaths = await getMigrationFilePaths(dir, { logger });
expect(Array.isArray(filePaths)).toBeTruthy();
expect(filePaths).toHaveLength(93);
expect(filePaths).not.toContainEqual(expect.stringContaining('nested'));
for (const filePath of filePaths) {
expect(filePath).toMatch(resolvedDir);
expect(filePath).toMatch(/\.js$/);
expect(isAbsolute(filePath)).toBeTruthy();
}
});
it('should resolve files directly in `dir` and ignore matching ignorePattern', async () => {
const dir = 'test/migrations';
// ignores those files that have `test` in their name (not in the path, just filename)
const ignorePattern = '.+test.+';
const filePaths = await getMigrationFilePaths(dir, {
ignorePattern,
logger,
});
expect(Array.isArray(filePaths)).toBeTruthy();
expect(filePaths).toHaveLength(68);
for (const filePath of filePaths) {
expect(isAbsolute(filePath)).toBeTruthy();
}
});
it('should resolve files matching `dir` glob (starting from cwd())', async () => {
const dir = 'test/{cockroach,migrations}/**';
const filePaths = await getMigrationFilePaths(dir, {
useGlob: true,
logger,
});
expect(Array.isArray(filePaths)).toBeTruthy();
expect(filePaths).toHaveLength(106);
expect(filePaths).toContainEqual(expect.stringContaining('nested'));
for (const filePath of filePaths) {
expect(isAbsolute(filePath)).toBeTruthy();
}
});
it('should resolve files matching `dir` glob (starting from cwd()) and ignore matching ignorePattern', async () => {
const dir = 'test/{cockroach,migrations}/**';
// ignores those files that have `test` in their name (not in the path, just filename)
const ignorePattern = '*/cockroach/*test*';
const filePaths = await getMigrationFilePaths(dir, {
ignorePattern,
useGlob: true,
logger,
});
expect(Array.isArray(filePaths)).toBeTruthy();
expect(filePaths).toHaveLength(105);
expect(filePaths).toContainEqual(expect.stringContaining('nested'));
for (const filePath of filePaths) {
expect(isAbsolute(filePath)).toBeTruthy();
}
});
});
describe('self.applyUp', () => {
it('should call db.query on normal operations', async () => {
const migration = new Migration(
dbMock,
callbackMigration,
actionsCallback,
options,
{},
logger
);
await migration.apply('up');
expect(queryMock).toHaveBeenCalled();
});
it('should call db.query when returning promise on normal operations', async () => {
const migration = new Migration(
dbMock,
promiseMigration,
actionsPromise,
options,
{},
logger
);
await migration.apply('up');
expect(queryMock).toHaveBeenCalled();
});
it('should perform the inferred reverse operation when reverseMode is enabled', async () => {
const migration = new Migration(
dbMock,
reverseMigration,
reversePromise,
options,
{},
logger
);
await migration.apply('up');
expect(queryMock).toHaveBeenCalledTimes(4);
expect(queryMock).toHaveBeenNthCalledWith(1, 'BEGIN;');
expect(queryMock).toHaveBeenNthCalledWith(
2,
expect.stringMatching('DROP TABLE')
);
});
it('should not call db.query on --dry-run', async () => {
const migration = new Migration(
dbMock,
callbackMigration,
actionsCallback,
{ ...options, dryRun: true },
{},
logger
);
await migration.apply('up');
expect(queryMock).not.toHaveBeenCalled();
});
it('should make proper SQL calls', async () => {
const migration = new Migration(
dbMock,
promiseMigration,
actionsCallback,
options,
{},
logger
);
await migration.apply('up');
expect(queryMock).toHaveBeenCalledTimes(4);
expect(queryMock).toHaveBeenNthCalledWith(1, 'BEGIN;');
expect(queryMock).toHaveBeenNthCalledWith(
2,
expect.stringMatching('CREATE TABLE')
);
expect(queryMock).toHaveBeenNthCalledWith(
3,
expect.stringMatching(`INSERT INTO "public"."${migrationsTable}"`)
);
expect(queryMock).toHaveBeenNthCalledWith(4, 'COMMIT;');
});
it('should fail with an error message if the migration is invalid', () => {
const invalidMigrationName = 'invalid-migration';
const migration = new Migration(
dbMock,
invalidMigrationName,
{},
options,
{},
logger
);
const direction = 'up';
expect(() => migration.apply(direction)).toThrow(
new Error(
`Unknown value for direction: ${direction}. Is the migration ${invalidMigrationName} exporting a '${direction}' function?`
)
);
});
});
describe('self.applyDown', () => {
it('should call db.query on normal operations', async () => {
const migration = new Migration(
dbMock,
callbackMigration,
actionsCallback,
options,
{},
logger
);
await migration.apply('down');
expect(queryMock).toHaveBeenCalled();
});
it('should not call db.query on --dry-run', async () => {
const migration = new Migration(
dbMock,
callbackMigration,
actionsCallback,
{ ...options, dryRun: true },
{},
logger
);
await migration.apply('down');
expect(queryMock).not.toHaveBeenCalled();
});
it('should make proper SQL calls', async () => {
const migration = new Migration(
dbMock,
promiseMigration,
actionsCallback,
options,
{},
logger
);
await migration.apply('down');
expect(queryMock).toHaveBeenCalledTimes(4);
expect(queryMock).toHaveBeenNthCalledWith(1, 'BEGIN;');
expect(queryMock).toHaveBeenNthCalledWith(
2,
expect.stringMatching('DROP TABLE')
);
expect(queryMock).toHaveBeenNthCalledWith(
3,
expect.stringMatching(`DELETE FROM "public"."${migrationsTable}"`)
);
expect(queryMock).toHaveBeenNthCalledWith(4, 'COMMIT;');
});
});
});