rake-db 2.3.6 → 2.3.8
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/CHANGELOG.md +15 -0
- package/dist/index.d.ts +214 -7
- package/dist/index.js +41 -24
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +41 -25
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/commands/migrateOrRollback.test.ts +4 -2
- package/src/commands/migrateOrRollback.ts +9 -3
- package/src/migration/changeTable.ts +3 -3
- package/src/migration/createTable.ts +3 -3
- package/src/migration/migration.ts +57 -43
- package/src/migration/migrationUtils.ts +2 -3
- package/src/test-utils.ts +3 -3
- package/src/pull/getColumnByType.ts +0 -15
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rake-db",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.8",
|
|
4
4
|
"description": "Migrations tool for Postgresql DB",
|
|
5
5
|
"homepage": "https://orchid-orm.netlify.app/guide/migration-setup-and-overview.html",
|
|
6
6
|
"repository": {
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"enquirer": "^2.3.6",
|
|
44
44
|
"pluralize": "^8.0.0",
|
|
45
|
-
"pqb": "0.9.
|
|
45
|
+
"pqb": "0.9.6"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@swc/core": "^1.2.210",
|
|
@@ -2,7 +2,6 @@ import { migrate, rollback } from './migrateOrRollback';
|
|
|
2
2
|
import { createSchemaMigrations, migrationConfigDefaults } from '../common';
|
|
3
3
|
import { getMigrationFiles } from '../common';
|
|
4
4
|
import { Adapter, noop, TransactionAdapter } from 'pqb';
|
|
5
|
-
import { Migration } from '../migration/migration';
|
|
6
5
|
import { change } from '../migration/change';
|
|
7
6
|
import { asMock } from '../test-utils';
|
|
8
7
|
|
|
@@ -32,7 +31,7 @@ Adapter.prototype.transaction = (cb) => {
|
|
|
32
31
|
};
|
|
33
32
|
|
|
34
33
|
const transactionQueryMock = jest.fn();
|
|
35
|
-
|
|
34
|
+
TransactionAdapter.prototype.query = transactionQueryMock;
|
|
36
35
|
|
|
37
36
|
const requireTsMock = jest.fn();
|
|
38
37
|
const config = {
|
|
@@ -82,9 +81,11 @@ describe('migrateOrRollback', () => {
|
|
|
82
81
|
|
|
83
82
|
expect(transactionQueryMock).toBeCalledWith(
|
|
84
83
|
`INSERT INTO "schemaMigrations" VALUES ('2')`,
|
|
84
|
+
undefined,
|
|
85
85
|
);
|
|
86
86
|
expect(transactionQueryMock).toBeCalledWith(
|
|
87
87
|
`INSERT INTO "schemaMigrations" VALUES ('3')`,
|
|
88
|
+
undefined,
|
|
88
89
|
);
|
|
89
90
|
|
|
90
91
|
expect(config.logger.log).toBeCalledWith('file2 migrated');
|
|
@@ -181,6 +182,7 @@ describe('migrateOrRollback', () => {
|
|
|
181
182
|
expect(transactionQueryMock).toBeCalledTimes(1);
|
|
182
183
|
expect(transactionQueryMock).toBeCalledWith(
|
|
183
184
|
`DELETE FROM "schemaMigrations" WHERE version = '2'`,
|
|
185
|
+
undefined,
|
|
184
186
|
);
|
|
185
187
|
|
|
186
188
|
expect(config.logger.log).toBeCalledTimes(1);
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
change,
|
|
15
15
|
getCurrentChangeCallback,
|
|
16
16
|
} from '../migration/change';
|
|
17
|
-
import {
|
|
17
|
+
import { createMigrationInterface } from '../migration/migration';
|
|
18
18
|
|
|
19
19
|
const migrateOrRollback = async (
|
|
20
20
|
options: MaybeArray<AdapterOptions>,
|
|
@@ -78,7 +78,13 @@ const processMigration = async (
|
|
|
78
78
|
appCodeUpdaterCache: object,
|
|
79
79
|
) => {
|
|
80
80
|
await db.transaction(async (tx) => {
|
|
81
|
-
const db =
|
|
81
|
+
const db = createMigrationInterface(
|
|
82
|
+
tx,
|
|
83
|
+
up,
|
|
84
|
+
config,
|
|
85
|
+
options,
|
|
86
|
+
appCodeUpdaterCache,
|
|
87
|
+
);
|
|
82
88
|
setCurrentMigration(db);
|
|
83
89
|
setCurrentMigrationUp(up);
|
|
84
90
|
|
|
@@ -92,7 +98,7 @@ const processMigration = async (
|
|
|
92
98
|
|
|
93
99
|
await getCurrentPromise();
|
|
94
100
|
await (up ? saveMigratedVersion : removeMigratedVersion)(
|
|
95
|
-
db,
|
|
101
|
+
db.adapter,
|
|
96
102
|
file.version,
|
|
97
103
|
config,
|
|
98
104
|
);
|
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
ChangeTableOptions,
|
|
19
19
|
ColumnComment,
|
|
20
20
|
DropMode,
|
|
21
|
-
|
|
21
|
+
MigrationBase,
|
|
22
22
|
MigrationColumnTypes,
|
|
23
23
|
runCodeUpdater,
|
|
24
24
|
} from './migration';
|
|
@@ -198,7 +198,7 @@ export type TableChangeData = Record<
|
|
|
198
198
|
>;
|
|
199
199
|
|
|
200
200
|
export const changeTable = async (
|
|
201
|
-
migration:
|
|
201
|
+
migration: MigrationBase,
|
|
202
202
|
up: boolean,
|
|
203
203
|
tableName: string,
|
|
204
204
|
options: ChangeTableOptions,
|
|
@@ -216,7 +216,7 @@ export const changeTable = async (
|
|
|
216
216
|
|
|
217
217
|
const queries = astToQueries(ast);
|
|
218
218
|
for (const query of queries) {
|
|
219
|
-
await migration.query(query);
|
|
219
|
+
await migration.adapter.query(query);
|
|
220
220
|
}
|
|
221
221
|
|
|
222
222
|
await runCodeUpdater(migration, ast);
|
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
import {
|
|
13
13
|
ColumnComment,
|
|
14
14
|
ColumnsShapeCallback,
|
|
15
|
-
|
|
15
|
+
MigrationBase,
|
|
16
16
|
runCodeUpdater,
|
|
17
17
|
TableOptions,
|
|
18
18
|
} from './migration';
|
|
@@ -33,7 +33,7 @@ const types = Object.assign(Object.create(columnTypes), {
|
|
|
33
33
|
});
|
|
34
34
|
|
|
35
35
|
export const createTable = async (
|
|
36
|
-
migration:
|
|
36
|
+
migration: MigrationBase,
|
|
37
37
|
up: boolean,
|
|
38
38
|
tableName: string,
|
|
39
39
|
options: TableOptions,
|
|
@@ -54,7 +54,7 @@ export const createTable = async (
|
|
|
54
54
|
|
|
55
55
|
const queries = astToQueries(ast);
|
|
56
56
|
for (const query of queries) {
|
|
57
|
-
await migration.query(query);
|
|
57
|
+
await migration.adapter.query(query);
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
await runCodeUpdater(migration, ast);
|
|
@@ -7,17 +7,16 @@ import {
|
|
|
7
7
|
IndexOptions,
|
|
8
8
|
logParamToLogObject,
|
|
9
9
|
MaybeArray,
|
|
10
|
-
QueryArraysResult,
|
|
11
10
|
QueryInput,
|
|
12
11
|
QueryLogObject,
|
|
13
|
-
QueryResult,
|
|
14
|
-
QueryResultRow,
|
|
15
12
|
Sql,
|
|
16
13
|
TransactionAdapter,
|
|
17
|
-
TypeParsers,
|
|
18
14
|
raw,
|
|
19
15
|
TextColumn,
|
|
20
16
|
AdapterOptions,
|
|
17
|
+
createDb,
|
|
18
|
+
columnTypes,
|
|
19
|
+
DbResult,
|
|
21
20
|
} from 'pqb';
|
|
22
21
|
import { createTable } from './createTable';
|
|
23
22
|
import { changeTable, TableChangeData, TableChanger } from './changeTable';
|
|
@@ -64,38 +63,53 @@ export type ExtensionOptions = {
|
|
|
64
63
|
cascade?: boolean;
|
|
65
64
|
};
|
|
66
65
|
|
|
67
|
-
export
|
|
68
|
-
public log?: QueryLogObject;
|
|
66
|
+
export type Migration = DbResult<typeof columnTypes> & MigrationBase;
|
|
69
67
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
68
|
+
export const createMigrationInterface = (
|
|
69
|
+
tx: TransactionAdapter,
|
|
70
|
+
up: boolean,
|
|
71
|
+
options: RakeDbConfig,
|
|
72
|
+
adapterOptions: AdapterOptions,
|
|
73
|
+
appCodeUpdaterCache: object,
|
|
74
|
+
) => {
|
|
75
|
+
const adapter = new TransactionAdapter(tx, tx.client, tx.types);
|
|
76
|
+
const { query, arrays } = adapter;
|
|
77
|
+
const log = logParamToLogObject(options.logger || console, options.log);
|
|
80
78
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
79
|
+
adapter.query = ((q, types) => {
|
|
80
|
+
return wrapWithLog(log, q, () => query.call(adapter, q, types));
|
|
81
|
+
}) as typeof adapter.query;
|
|
82
|
+
|
|
83
|
+
adapter.arrays = ((q, types) => {
|
|
84
|
+
return wrapWithLog(log, q, () => arrays.call(adapter, q, types));
|
|
85
|
+
}) as typeof adapter.arrays;
|
|
89
86
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
return wrapWithLog(log, query, () => super.arrays(query, types));
|
|
87
|
+
const db = createDb({ adapter, columnTypes }) as unknown as Migration;
|
|
88
|
+
|
|
89
|
+
const { prototype: proto } = MigrationBase;
|
|
90
|
+
for (const key of Object.getOwnPropertyNames(proto)) {
|
|
91
|
+
(db as unknown as Record<string, unknown>)[key] =
|
|
92
|
+
proto[key as keyof typeof proto];
|
|
97
93
|
}
|
|
98
94
|
|
|
95
|
+
return Object.assign(db, {
|
|
96
|
+
adapter,
|
|
97
|
+
log,
|
|
98
|
+
up,
|
|
99
|
+
options,
|
|
100
|
+
adapterOptions,
|
|
101
|
+
appCodeUpdaterCache,
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export class MigrationBase {
|
|
106
|
+
public adapter!: TransactionAdapter;
|
|
107
|
+
public log?: QueryLogObject;
|
|
108
|
+
public up!: boolean;
|
|
109
|
+
public options!: RakeDbConfig;
|
|
110
|
+
public adapterOptions!: AdapterOptions;
|
|
111
|
+
public appCodeUpdaterCache!: object;
|
|
112
|
+
|
|
99
113
|
createTable(
|
|
100
114
|
tableName: string,
|
|
101
115
|
options: TableOptions,
|
|
@@ -158,7 +172,7 @@ export class Migration extends TransactionAdapter {
|
|
|
158
172
|
to: t,
|
|
159
173
|
};
|
|
160
174
|
|
|
161
|
-
await this.query(
|
|
175
|
+
await this.adapter.query(
|
|
162
176
|
`ALTER TABLE ${quoteWithSchema({
|
|
163
177
|
schema: ast.fromSchema,
|
|
164
178
|
name: ast.from,
|
|
@@ -335,7 +349,7 @@ const wrapWithLog = async <Result>(
|
|
|
335
349
|
};
|
|
336
350
|
|
|
337
351
|
const addColumn = (
|
|
338
|
-
migration:
|
|
352
|
+
migration: MigrationBase,
|
|
339
353
|
up: boolean,
|
|
340
354
|
tableName: string,
|
|
341
355
|
columnName: string,
|
|
@@ -347,7 +361,7 @@ const addColumn = (
|
|
|
347
361
|
};
|
|
348
362
|
|
|
349
363
|
const addIndex = (
|
|
350
|
-
migration:
|
|
364
|
+
migration: MigrationBase,
|
|
351
365
|
up: boolean,
|
|
352
366
|
tableName: string,
|
|
353
367
|
columns: MaybeArray<string | IndexColumnOptions>,
|
|
@@ -359,7 +373,7 @@ const addIndex = (
|
|
|
359
373
|
};
|
|
360
374
|
|
|
361
375
|
const addForeignKey = (
|
|
362
|
-
migration:
|
|
376
|
+
migration: MigrationBase,
|
|
363
377
|
up: boolean,
|
|
364
378
|
tableName: string,
|
|
365
379
|
columns: [string, ...string[]],
|
|
@@ -373,7 +387,7 @@ const addForeignKey = (
|
|
|
373
387
|
};
|
|
374
388
|
|
|
375
389
|
const addPrimaryKey = (
|
|
376
|
-
migration:
|
|
390
|
+
migration: MigrationBase,
|
|
377
391
|
up: boolean,
|
|
378
392
|
tableName: string,
|
|
379
393
|
columns: string[],
|
|
@@ -385,7 +399,7 @@ const addPrimaryKey = (
|
|
|
385
399
|
};
|
|
386
400
|
|
|
387
401
|
const createSchema = async (
|
|
388
|
-
migration:
|
|
402
|
+
migration: MigrationBase,
|
|
389
403
|
up: boolean,
|
|
390
404
|
name: string,
|
|
391
405
|
) => {
|
|
@@ -395,7 +409,7 @@ const createSchema = async (
|
|
|
395
409
|
name,
|
|
396
410
|
};
|
|
397
411
|
|
|
398
|
-
await migration.query(
|
|
412
|
+
await migration.adapter.query(
|
|
399
413
|
`${ast.action === 'create' ? 'CREATE' : 'DROP'} SCHEMA "${name}"`,
|
|
400
414
|
);
|
|
401
415
|
|
|
@@ -403,7 +417,7 @@ const createSchema = async (
|
|
|
403
417
|
};
|
|
404
418
|
|
|
405
419
|
const createExtension = async (
|
|
406
|
-
migration:
|
|
420
|
+
migration: MigrationBase,
|
|
407
421
|
up: boolean,
|
|
408
422
|
name: string,
|
|
409
423
|
options: ExtensionOptions & {
|
|
@@ -430,19 +444,19 @@ const createExtension = async (
|
|
|
430
444
|
}${ast.cascade ? ' CASCADE' : ''}`;
|
|
431
445
|
}
|
|
432
446
|
|
|
433
|
-
await migration.query(query);
|
|
447
|
+
await migration.adapter.query(query);
|
|
434
448
|
|
|
435
449
|
await runCodeUpdater(migration, ast);
|
|
436
450
|
};
|
|
437
451
|
|
|
438
452
|
const queryExists = (
|
|
439
|
-
db:
|
|
453
|
+
db: MigrationBase,
|
|
440
454
|
sql: { text: string; values: unknown[] },
|
|
441
455
|
) => {
|
|
442
|
-
return db.query(sql).then(({ rowCount }) => rowCount > 0);
|
|
456
|
+
return db.adapter.query(sql).then(({ rowCount }) => rowCount > 0);
|
|
443
457
|
};
|
|
444
458
|
|
|
445
|
-
export const runCodeUpdater = (migration:
|
|
459
|
+
export const runCodeUpdater = (migration: MigrationBase, ast: RakeDbAst) => {
|
|
446
460
|
return migration.options.appCodeUpdater?.({
|
|
447
461
|
ast,
|
|
448
462
|
options: migration.adapterOptions,
|
|
@@ -284,7 +284,7 @@ export const getPrimaryKeysOfTable = async (
|
|
|
284
284
|
db: Migration,
|
|
285
285
|
tableName: string,
|
|
286
286
|
): Promise<{ name: string; type: string }[]> => {
|
|
287
|
-
const { rows } = await db.query<{ name: string; type: string }>(
|
|
287
|
+
const { rows } = await db.adapter.query<{ name: string; type: string }>(
|
|
288
288
|
{
|
|
289
289
|
text: `SELECT
|
|
290
290
|
pg_attribute.attname AS name,
|
|
@@ -300,8 +300,7 @@ WHERE
|
|
|
300
300
|
indisprimary`,
|
|
301
301
|
values: [tableName],
|
|
302
302
|
},
|
|
303
|
-
db.types,
|
|
304
|
-
undefined,
|
|
303
|
+
db.adapter.types,
|
|
305
304
|
);
|
|
306
305
|
|
|
307
306
|
return rows;
|
package/src/test-utils.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Migration } from './migration/migration';
|
|
1
|
+
import { createMigrationInterface, Migration } from './migration/migration';
|
|
2
2
|
import { MaybeArray, toArray, TransactionAdapter } from 'pqb';
|
|
3
3
|
|
|
4
4
|
export const asMock = (fn: unknown) => fn as jest.Mock;
|
|
@@ -8,7 +8,7 @@ let db: Migration | undefined;
|
|
|
8
8
|
export const getDb = () => {
|
|
9
9
|
if (db) return db;
|
|
10
10
|
|
|
11
|
-
db =
|
|
11
|
+
db = createMigrationInterface(
|
|
12
12
|
{} as unknown as TransactionAdapter,
|
|
13
13
|
true,
|
|
14
14
|
{
|
|
@@ -21,7 +21,7 @@ export const getDb = () => {
|
|
|
21
21
|
{},
|
|
22
22
|
{},
|
|
23
23
|
);
|
|
24
|
-
db.query = queryMock;
|
|
24
|
+
db.adapter.query = queryMock;
|
|
25
25
|
return db;
|
|
26
26
|
};
|
|
27
27
|
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { ColumnType, ColumnTypesBase } from 'pqb';
|
|
2
|
-
|
|
3
|
-
export const getColumnsByTypesMap = (types: ColumnTypesBase) => {
|
|
4
|
-
const map: Record<string, new () => ColumnType> = {};
|
|
5
|
-
for (const key in types) {
|
|
6
|
-
const type = types[key] as unknown as new () => ColumnType;
|
|
7
|
-
if (type instanceof ColumnType) {
|
|
8
|
-
map[type.dataType] = type;
|
|
9
|
-
if (type.typeAlias) {
|
|
10
|
-
map[type.typeAlias] = type;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
return map;
|
|
15
|
-
};
|