rake-db 2.3.29 → 2.3.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +11 -21
- package/.env +0 -1
- package/.env.local +0 -2
- package/.turbo/turbo-test.log +0 -22
- package/.turbo/turbo-test:ci.log +0 -22
- package/CHANGELOG.md +0 -388
- package/app/dbScript.ts +0 -33
- package/app/migrations/20221017181504_createUser.ts +0 -14
- package/app/migrations/20221017200111_createProfile.ts +0 -10
- package/app/migrations/20221017200252_createChat.ts +0 -9
- package/app/migrations/20221017200326_createChatUser.ts +0 -10
- package/app/migrations/20221017200900_createMessage.ts +0 -12
- package/app/migrations/20221017201235_createGeoSchema.ts +0 -5
- package/app/migrations/20221017210011_createCountry.ts +0 -8
- package/app/migrations/20221017210133_createCity.ts +0 -9
- package/app/migrations/20221105202843_createUniqueTable.ts +0 -12
- package/jest-setup.ts +0 -3
- package/rollup.config.js +0 -3
- package/src/ast.ts +0 -130
- package/src/commands/createOrDrop.test.ts +0 -214
- package/src/commands/createOrDrop.ts +0 -151
- package/src/commands/generate.test.ts +0 -136
- package/src/commands/generate.ts +0 -93
- package/src/commands/migrateOrRollback.test.ts +0 -267
- package/src/commands/migrateOrRollback.ts +0 -190
- package/src/common.test.ts +0 -295
- package/src/common.ts +0 -353
- package/src/errors.ts +0 -3
- package/src/index.ts +0 -8
- package/src/migration/change.test.ts +0 -16
- package/src/migration/change.ts +0 -15
- package/src/migration/changeTable.test.ts +0 -897
- package/src/migration/changeTable.ts +0 -566
- package/src/migration/createTable.test.ts +0 -384
- package/src/migration/createTable.ts +0 -193
- package/src/migration/migration.test.ts +0 -430
- package/src/migration/migration.ts +0 -518
- package/src/migration/migrationUtils.ts +0 -307
- package/src/migration/tableMethods.ts +0 -8
- package/src/pull/astToMigration.test.ts +0 -275
- package/src/pull/astToMigration.ts +0 -173
- package/src/pull/dbStructure.test.ts +0 -180
- package/src/pull/dbStructure.ts +0 -413
- package/src/pull/pull.test.ts +0 -115
- package/src/pull/pull.ts +0 -22
- package/src/pull/structureToAst.test.ts +0 -841
- package/src/pull/structureToAst.ts +0 -372
- package/src/rakeDb.test.ts +0 -131
- package/src/rakeDb.ts +0 -84
- package/src/test-utils.ts +0 -64
- package/tsconfig.json +0 -12
|
@@ -1,430 +0,0 @@
|
|
|
1
|
-
import { expectSql, getDb, queryMock, resetDb, toLine } from '../test-utils';
|
|
2
|
-
|
|
3
|
-
const db = getDb();
|
|
4
|
-
|
|
5
|
-
jest.mock('./migrationUtils', () => ({
|
|
6
|
-
...jest.requireActual('./migrationUtils'),
|
|
7
|
-
getPrimaryKeysOfTable: jest.fn(),
|
|
8
|
-
}));
|
|
9
|
-
|
|
10
|
-
describe('migration', () => {
|
|
11
|
-
beforeEach(resetDb);
|
|
12
|
-
|
|
13
|
-
describe('renameTable', () => {
|
|
14
|
-
it('should call appCodeUpdater', async () => {
|
|
15
|
-
await db.renameTable('from', 'to');
|
|
16
|
-
|
|
17
|
-
expect(db.options.appCodeUpdater).toHaveBeenCalled();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it('should rename a table', async () => {
|
|
21
|
-
const fn = () => {
|
|
22
|
-
return db.renameTable('from', 'to');
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
await fn();
|
|
26
|
-
expectSql(`
|
|
27
|
-
ALTER TABLE "from" RENAME TO "to"
|
|
28
|
-
`);
|
|
29
|
-
|
|
30
|
-
db.up = false;
|
|
31
|
-
queryMock.mockClear();
|
|
32
|
-
await fn();
|
|
33
|
-
expectSql(`
|
|
34
|
-
ALTER TABLE "to" RENAME TO "from"
|
|
35
|
-
`);
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('should rename table with schema', async () => {
|
|
40
|
-
const fn = () => {
|
|
41
|
-
return db.renameTable('one.from', 'two.to');
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
await fn();
|
|
45
|
-
expectSql(`
|
|
46
|
-
ALTER TABLE "one"."from" RENAME TO "two"."to"
|
|
47
|
-
`);
|
|
48
|
-
|
|
49
|
-
db.up = false;
|
|
50
|
-
queryMock.mockClear();
|
|
51
|
-
await fn();
|
|
52
|
-
expectSql(`
|
|
53
|
-
ALTER TABLE "two"."to" RENAME TO "one"."from"
|
|
54
|
-
`);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
(['addColumn', 'dropColumn'] as const).forEach((action) => {
|
|
58
|
-
describe(action, () => {
|
|
59
|
-
it(`should use changeTable to ${
|
|
60
|
-
action === 'addColumn' ? 'add' : 'drop'
|
|
61
|
-
} a column`, async () => {
|
|
62
|
-
const fn = () => {
|
|
63
|
-
return db[action]('table', 'column', (t) => t.text());
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
const expectAddColumn = () => {
|
|
67
|
-
expectSql(`
|
|
68
|
-
ALTER TABLE "table"
|
|
69
|
-
ADD COLUMN "column" text NOT NULL
|
|
70
|
-
`);
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const expectDropColumn = () => {
|
|
74
|
-
expectSql(`
|
|
75
|
-
ALTER TABLE "table"
|
|
76
|
-
DROP COLUMN "column"
|
|
77
|
-
`);
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
await fn();
|
|
81
|
-
(action === 'addColumn' ? expectAddColumn : expectDropColumn)();
|
|
82
|
-
|
|
83
|
-
db.up = false;
|
|
84
|
-
queryMock.mockClear();
|
|
85
|
-
await fn();
|
|
86
|
-
(action === 'addColumn' ? expectDropColumn : expectAddColumn)();
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
(['addIndex', 'dropIndex'] as const).forEach((action) => {
|
|
92
|
-
describe(action, () => {
|
|
93
|
-
it(`should use changeTable to ${
|
|
94
|
-
action === 'addIndex' ? 'add' : 'drop'
|
|
95
|
-
} an index`, async () => {
|
|
96
|
-
const fn = () => {
|
|
97
|
-
return db[action](
|
|
98
|
-
'table',
|
|
99
|
-
['id', { column: 'name', order: 'DESC' }],
|
|
100
|
-
{
|
|
101
|
-
name: 'indexName',
|
|
102
|
-
},
|
|
103
|
-
);
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
const expectAddIndex = () => {
|
|
107
|
-
expectSql(`
|
|
108
|
-
CREATE INDEX "indexName" ON "table" ("id", "name" DESC)
|
|
109
|
-
`);
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
const expectDropIndex = () => {
|
|
113
|
-
expectSql(`
|
|
114
|
-
DROP INDEX "indexName"
|
|
115
|
-
`);
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
await fn();
|
|
119
|
-
(action === 'addIndex' ? expectAddIndex : expectDropIndex)();
|
|
120
|
-
|
|
121
|
-
db.up = false;
|
|
122
|
-
queryMock.mockClear();
|
|
123
|
-
await fn();
|
|
124
|
-
(action === 'addIndex' ? expectDropIndex : expectAddIndex)();
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
(['addForeignKey', 'dropForeignKey'] as const).forEach((action) => {
|
|
130
|
-
describe(action, () => {
|
|
131
|
-
it(`should use changeTable to ${
|
|
132
|
-
action === 'addForeignKey' ? 'add' : 'drop'
|
|
133
|
-
} a foreignKey`, async () => {
|
|
134
|
-
const fn = () => {
|
|
135
|
-
return db[action](
|
|
136
|
-
'table',
|
|
137
|
-
['id', 'name'],
|
|
138
|
-
'otherTable',
|
|
139
|
-
['foreignId', 'foreignName'],
|
|
140
|
-
{
|
|
141
|
-
name: 'constraintName',
|
|
142
|
-
match: 'FULL',
|
|
143
|
-
onUpdate: 'CASCADE',
|
|
144
|
-
onDelete: 'CASCADE',
|
|
145
|
-
dropMode: 'CASCADE',
|
|
146
|
-
},
|
|
147
|
-
);
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
const expectAddForeignKey = () => {
|
|
151
|
-
const expectedConstraint = toLine(`
|
|
152
|
-
ADD CONSTRAINT "constraintName"
|
|
153
|
-
FOREIGN KEY ("id", "name")
|
|
154
|
-
REFERENCES "otherTable"("foreignId", "foreignName")
|
|
155
|
-
MATCH FULL
|
|
156
|
-
ON DELETE CASCADE
|
|
157
|
-
ON UPDATE CASCADE
|
|
158
|
-
`);
|
|
159
|
-
expectSql(`
|
|
160
|
-
ALTER TABLE "table"
|
|
161
|
-
${expectedConstraint}
|
|
162
|
-
`);
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
const expectDropForeignKey = () => {
|
|
166
|
-
expectSql(`
|
|
167
|
-
ALTER TABLE "table"
|
|
168
|
-
DROP CONSTRAINT "constraintName" CASCADE
|
|
169
|
-
`);
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
await fn();
|
|
173
|
-
(action === 'addForeignKey'
|
|
174
|
-
? expectAddForeignKey
|
|
175
|
-
: expectDropForeignKey)();
|
|
176
|
-
|
|
177
|
-
db.up = false;
|
|
178
|
-
queryMock.mockClear();
|
|
179
|
-
await fn();
|
|
180
|
-
(action === 'addForeignKey'
|
|
181
|
-
? expectDropForeignKey
|
|
182
|
-
: expectAddForeignKey)();
|
|
183
|
-
});
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
(['addPrimaryKey', 'dropPrimaryKey'] as const).forEach((action) => {
|
|
188
|
-
describe(action, () => {
|
|
189
|
-
it(`should use changeTable to ${
|
|
190
|
-
action === 'addPrimaryKey' ? 'add' : 'drop'
|
|
191
|
-
} primary key`, async () => {
|
|
192
|
-
const fn = () => {
|
|
193
|
-
return db[action]('table', ['id', 'name']);
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
const expectAddPrimaryKey = () => {
|
|
197
|
-
expectSql(`
|
|
198
|
-
ALTER TABLE "table"
|
|
199
|
-
ADD PRIMARY KEY ("id", "name")
|
|
200
|
-
`);
|
|
201
|
-
};
|
|
202
|
-
|
|
203
|
-
const expectDropPrimaryKey = () => {
|
|
204
|
-
expectSql(`
|
|
205
|
-
ALTER TABLE "table"
|
|
206
|
-
DROP CONSTRAINT "table_pkey"
|
|
207
|
-
`);
|
|
208
|
-
};
|
|
209
|
-
|
|
210
|
-
await fn();
|
|
211
|
-
(action === 'addPrimaryKey'
|
|
212
|
-
? expectAddPrimaryKey
|
|
213
|
-
: expectDropPrimaryKey)();
|
|
214
|
-
|
|
215
|
-
db.up = false;
|
|
216
|
-
queryMock.mockClear();
|
|
217
|
-
await fn();
|
|
218
|
-
(action === 'addPrimaryKey'
|
|
219
|
-
? expectDropPrimaryKey
|
|
220
|
-
: expectAddPrimaryKey)();
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
it('should use changeTable to add primary key with constraint name', async () => {
|
|
224
|
-
const fn = () => {
|
|
225
|
-
return db.addPrimaryKey('table', ['id', 'name'], {
|
|
226
|
-
name: 'primaryKeyName',
|
|
227
|
-
});
|
|
228
|
-
};
|
|
229
|
-
|
|
230
|
-
await fn();
|
|
231
|
-
expectSql(`
|
|
232
|
-
ALTER TABLE "table"
|
|
233
|
-
ADD CONSTRAINT "primaryKeyName" PRIMARY KEY ("id", "name")
|
|
234
|
-
`);
|
|
235
|
-
|
|
236
|
-
db.up = false;
|
|
237
|
-
queryMock.mockClear();
|
|
238
|
-
await fn();
|
|
239
|
-
expectSql(`
|
|
240
|
-
ALTER TABLE "table"
|
|
241
|
-
DROP CONSTRAINT "primaryKeyName"
|
|
242
|
-
`);
|
|
243
|
-
});
|
|
244
|
-
});
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
describe('renameColumn', () => {
|
|
248
|
-
it('should use changeTable to rename a column', async () => {
|
|
249
|
-
const fn = () => {
|
|
250
|
-
return db.renameColumn('table', 'from', 'to');
|
|
251
|
-
};
|
|
252
|
-
|
|
253
|
-
await fn();
|
|
254
|
-
expectSql(`
|
|
255
|
-
ALTER TABLE "table"
|
|
256
|
-
RENAME COLUMN "from" TO "to"
|
|
257
|
-
`);
|
|
258
|
-
|
|
259
|
-
db.up = false;
|
|
260
|
-
queryMock.mockClear();
|
|
261
|
-
await fn();
|
|
262
|
-
expectSql(`
|
|
263
|
-
ALTER TABLE "table"
|
|
264
|
-
RENAME COLUMN "to" TO "from"
|
|
265
|
-
`);
|
|
266
|
-
});
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
(['createSchema', 'dropSchema'] as const).forEach((action) => {
|
|
270
|
-
describe(action, () => {
|
|
271
|
-
it('should call appCodeUpdater', async () => {
|
|
272
|
-
await db[action]('schemaName');
|
|
273
|
-
|
|
274
|
-
expect(db.options.appCodeUpdater).toHaveBeenCalled();
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
it(`should ${
|
|
278
|
-
action === 'createSchema' ? 'add' : 'drop'
|
|
279
|
-
} a schema`, async () => {
|
|
280
|
-
const fn = () => {
|
|
281
|
-
return db[action]('schemaName');
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
const expectCreateSchema = () => {
|
|
285
|
-
expectSql(`
|
|
286
|
-
CREATE SCHEMA "schemaName"
|
|
287
|
-
`);
|
|
288
|
-
};
|
|
289
|
-
|
|
290
|
-
const expectDropSchema = () => {
|
|
291
|
-
expectSql(`
|
|
292
|
-
DROP SCHEMA "schemaName"
|
|
293
|
-
`);
|
|
294
|
-
};
|
|
295
|
-
|
|
296
|
-
await fn();
|
|
297
|
-
(action === 'createSchema' ? expectCreateSchema : expectDropSchema)();
|
|
298
|
-
|
|
299
|
-
db.up = false;
|
|
300
|
-
queryMock.mockClear();
|
|
301
|
-
await fn();
|
|
302
|
-
(action === 'createSchema' ? expectDropSchema : expectCreateSchema)();
|
|
303
|
-
});
|
|
304
|
-
});
|
|
305
|
-
});
|
|
306
|
-
|
|
307
|
-
(['createExtension', 'dropExtension'] as const).forEach((action) => {
|
|
308
|
-
describe(action, () => {
|
|
309
|
-
it('should call appCodeUpdater', async () => {
|
|
310
|
-
await db[action]('extensionName');
|
|
311
|
-
|
|
312
|
-
expect(db.options.appCodeUpdater).toHaveBeenCalled();
|
|
313
|
-
});
|
|
314
|
-
|
|
315
|
-
it(`should ${
|
|
316
|
-
action === 'createExtension' ? 'add' : 'drop'
|
|
317
|
-
} an extension`, async () => {
|
|
318
|
-
const fn = () => {
|
|
319
|
-
return db[action]('extensionName', {
|
|
320
|
-
dropIfExists: true,
|
|
321
|
-
createIfNotExists: true,
|
|
322
|
-
schema: 'schemaName',
|
|
323
|
-
version: '123',
|
|
324
|
-
cascade: true,
|
|
325
|
-
});
|
|
326
|
-
};
|
|
327
|
-
|
|
328
|
-
const expectCreateExtension = () => {
|
|
329
|
-
expectSql(`
|
|
330
|
-
CREATE EXTENSION IF NOT EXISTS "extensionName" SCHEMA "schemaName" VERSION '123' CASCADE
|
|
331
|
-
`);
|
|
332
|
-
};
|
|
333
|
-
|
|
334
|
-
const expectDropExtension = () => {
|
|
335
|
-
expectSql(`
|
|
336
|
-
DROP EXTENSION IF EXISTS "extensionName" CASCADE
|
|
337
|
-
`);
|
|
338
|
-
};
|
|
339
|
-
|
|
340
|
-
await fn();
|
|
341
|
-
(action === 'createExtension'
|
|
342
|
-
? expectCreateExtension
|
|
343
|
-
: expectDropExtension)();
|
|
344
|
-
|
|
345
|
-
db.up = false;
|
|
346
|
-
queryMock.mockClear();
|
|
347
|
-
await fn();
|
|
348
|
-
(action === 'createExtension'
|
|
349
|
-
? expectDropExtension
|
|
350
|
-
: expectCreateExtension)();
|
|
351
|
-
});
|
|
352
|
-
});
|
|
353
|
-
});
|
|
354
|
-
|
|
355
|
-
(['createEnum', 'dropEnum'] as const).forEach((action) => {
|
|
356
|
-
describe(action, () => {
|
|
357
|
-
it('should call appCodeUpdater', async () => {
|
|
358
|
-
await db[action]('enumName', ['one']);
|
|
359
|
-
|
|
360
|
-
expect(db.options.appCodeUpdater).toHaveBeenCalled();
|
|
361
|
-
});
|
|
362
|
-
|
|
363
|
-
it(`should ${
|
|
364
|
-
action === 'createEnum' ? 'add' : 'drop'
|
|
365
|
-
} an enum`, async () => {
|
|
366
|
-
const fn = () => {
|
|
367
|
-
return db[action]('enumName', ['one', 'two'], {
|
|
368
|
-
dropIfExists: true,
|
|
369
|
-
schema: 'schemaName',
|
|
370
|
-
cascade: true,
|
|
371
|
-
});
|
|
372
|
-
};
|
|
373
|
-
|
|
374
|
-
const expectCreateExtension = () => {
|
|
375
|
-
expectSql(`
|
|
376
|
-
CREATE TYPE "schemaName"."enumName" AS ENUM ('one', 'two')
|
|
377
|
-
`);
|
|
378
|
-
};
|
|
379
|
-
|
|
380
|
-
const expectDropExtension = () => {
|
|
381
|
-
expectSql(`
|
|
382
|
-
DROP TYPE IF EXISTS "schemaName"."enumName" CASCADE
|
|
383
|
-
`);
|
|
384
|
-
};
|
|
385
|
-
|
|
386
|
-
await fn();
|
|
387
|
-
(action === 'createEnum'
|
|
388
|
-
? expectCreateExtension
|
|
389
|
-
: expectDropExtension)();
|
|
390
|
-
|
|
391
|
-
db.up = false;
|
|
392
|
-
queryMock.mockClear();
|
|
393
|
-
await fn();
|
|
394
|
-
(action === 'createEnum'
|
|
395
|
-
? expectDropExtension
|
|
396
|
-
: expectCreateExtension)();
|
|
397
|
-
});
|
|
398
|
-
});
|
|
399
|
-
});
|
|
400
|
-
|
|
401
|
-
describe('tableExists', () => {
|
|
402
|
-
it('should return boolean', async () => {
|
|
403
|
-
queryMock.mockResolvedValueOnce({ rowCount: 1 });
|
|
404
|
-
expect(await db.tableExists('table')).toBe(true);
|
|
405
|
-
|
|
406
|
-
queryMock.mockResolvedValueOnce({ rowCount: 0 });
|
|
407
|
-
expect(await db.tableExists('table')).toBe(false);
|
|
408
|
-
});
|
|
409
|
-
});
|
|
410
|
-
|
|
411
|
-
describe('columnExists', () => {
|
|
412
|
-
it('should return boolean', async () => {
|
|
413
|
-
queryMock.mockResolvedValueOnce({ rowCount: 1 });
|
|
414
|
-
expect(await db.columnExists('table', 'colum')).toBe(true);
|
|
415
|
-
|
|
416
|
-
queryMock.mockResolvedValueOnce({ rowCount: 0 });
|
|
417
|
-
expect(await db.columnExists('table', 'colum')).toBe(false);
|
|
418
|
-
});
|
|
419
|
-
});
|
|
420
|
-
|
|
421
|
-
describe('constraintExists', () => {
|
|
422
|
-
it('should return boolean', async () => {
|
|
423
|
-
queryMock.mockResolvedValueOnce({ rowCount: 1 });
|
|
424
|
-
expect(await db.constraintExists('constraintName')).toBe(true);
|
|
425
|
-
|
|
426
|
-
queryMock.mockResolvedValueOnce({ rowCount: 0 });
|
|
427
|
-
expect(await db.constraintExists('constraintName')).toBe(false);
|
|
428
|
-
});
|
|
429
|
-
});
|
|
430
|
-
});
|