rake-db 2.3.8 → 2.3.9
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 +6 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +25 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +25 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/commands/migrateOrRollback.test.ts +20 -4
- package/src/commands/migrateOrRollback.ts +36 -5
- package/src/common.ts +8 -0
package/package.json
CHANGED
|
@@ -71,10 +71,18 @@ describe('migrateOrRollback', () => {
|
|
|
71
71
|
it('should work properly', async () => {
|
|
72
72
|
migrationFiles = files;
|
|
73
73
|
migratedVersions = ['1'];
|
|
74
|
+
const conf = {
|
|
75
|
+
...config,
|
|
76
|
+
beforeMigrate: jest.fn(),
|
|
77
|
+
afterMigrate: jest.fn(),
|
|
78
|
+
};
|
|
74
79
|
|
|
75
|
-
await migrate(options,
|
|
80
|
+
await migrate(options, conf, []);
|
|
76
81
|
|
|
77
|
-
expect(getMigrationFiles).toBeCalledWith(
|
|
82
|
+
expect(getMigrationFiles).toBeCalledWith(conf, true);
|
|
83
|
+
|
|
84
|
+
expect(conf.beforeMigrate).toBeCalled();
|
|
85
|
+
expect(conf.afterMigrate).toBeCalled();
|
|
78
86
|
|
|
79
87
|
expect(requireTsMock).toBeCalledWith('file2');
|
|
80
88
|
expect(requireTsMock).toBeCalledWith('file3');
|
|
@@ -171,10 +179,18 @@ describe('migrateOrRollback', () => {
|
|
|
171
179
|
it('should work properly', async () => {
|
|
172
180
|
migrationFiles = files.reverse();
|
|
173
181
|
migratedVersions = ['1', '2'];
|
|
182
|
+
const conf = {
|
|
183
|
+
...config,
|
|
184
|
+
beforeRollback: jest.fn(),
|
|
185
|
+
afterRollback: jest.fn(),
|
|
186
|
+
};
|
|
174
187
|
|
|
175
|
-
await rollback(options,
|
|
188
|
+
await rollback(options, conf, []);
|
|
176
189
|
|
|
177
|
-
expect(
|
|
190
|
+
expect(conf.beforeRollback).toBeCalled();
|
|
191
|
+
expect(conf.afterRollback).toBeCalled();
|
|
192
|
+
|
|
193
|
+
expect(getMigrationFiles).toBeCalledWith(conf, false);
|
|
178
194
|
|
|
179
195
|
expect(requireTsMock).toBeCalledTimes(1);
|
|
180
196
|
expect(requireTsMock).toBeCalledWith('file2');
|
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
Adapter,
|
|
3
|
+
AdapterOptions,
|
|
4
|
+
columnTypes,
|
|
5
|
+
createDb,
|
|
6
|
+
DbResult,
|
|
7
|
+
MaybeArray,
|
|
8
|
+
toArray,
|
|
9
|
+
} from 'pqb';
|
|
2
10
|
import {
|
|
3
11
|
createSchemaMigrations,
|
|
4
12
|
getMigrationFiles,
|
|
@@ -16,6 +24,8 @@ import {
|
|
|
16
24
|
} from '../migration/change';
|
|
17
25
|
import { createMigrationInterface } from '../migration/migration';
|
|
18
26
|
|
|
27
|
+
const getDb = (adapter: Adapter) => createDb({ adapter, columnTypes });
|
|
28
|
+
|
|
19
29
|
const migrateOrRollback = async (
|
|
20
30
|
options: MaybeArray<AdapterOptions>,
|
|
21
31
|
config: RakeDbConfig,
|
|
@@ -43,8 +53,16 @@ const migrateOrRollback = async (
|
|
|
43
53
|
const appCodeUpdaterCache = {};
|
|
44
54
|
|
|
45
55
|
for (const opts of toArray(options)) {
|
|
46
|
-
const
|
|
47
|
-
|
|
56
|
+
const adapter = new Adapter(opts);
|
|
57
|
+
let db: DbResult<typeof columnTypes> | undefined;
|
|
58
|
+
|
|
59
|
+
if (up) {
|
|
60
|
+
await config.beforeMigrate?.((db ??= getDb(adapter)));
|
|
61
|
+
} else {
|
|
62
|
+
await config.beforeRollback?.((db ??= getDb(adapter)));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const migratedVersions = await getMigratedVersionsMap(adapter, config);
|
|
48
66
|
try {
|
|
49
67
|
for (const file of files) {
|
|
50
68
|
if (
|
|
@@ -56,11 +74,24 @@ const migrateOrRollback = async (
|
|
|
56
74
|
|
|
57
75
|
if (count-- <= 0) break;
|
|
58
76
|
|
|
59
|
-
await processMigration(
|
|
77
|
+
await processMigration(
|
|
78
|
+
adapter,
|
|
79
|
+
up,
|
|
80
|
+
file,
|
|
81
|
+
config,
|
|
82
|
+
opts,
|
|
83
|
+
appCodeUpdaterCache,
|
|
84
|
+
);
|
|
60
85
|
config.logger?.log(`${file.path} ${up ? 'migrated' : 'rolled back'}`);
|
|
61
86
|
}
|
|
87
|
+
|
|
88
|
+
if (up) {
|
|
89
|
+
await config.afterMigrate?.((db ??= getDb(adapter)));
|
|
90
|
+
} else {
|
|
91
|
+
await config.afterRollback?.((db ??= getDb(adapter)));
|
|
92
|
+
}
|
|
62
93
|
} finally {
|
|
63
|
-
await
|
|
94
|
+
await adapter.close();
|
|
64
95
|
}
|
|
65
96
|
// use appCodeUpdater only for the first provided options
|
|
66
97
|
delete config.appCodeUpdater;
|
package/src/common.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Adapter,
|
|
3
3
|
AdapterOptions,
|
|
4
|
+
columnTypes,
|
|
5
|
+
DbResult,
|
|
4
6
|
NoPrimaryKeyOption,
|
|
5
7
|
QueryLogOptions,
|
|
6
8
|
singleQuote,
|
|
@@ -10,6 +12,8 @@ import path from 'path';
|
|
|
10
12
|
import { readdir } from 'fs/promises';
|
|
11
13
|
import { RakeDbAst } from './ast';
|
|
12
14
|
|
|
15
|
+
type Db = DbResult<typeof columnTypes>;
|
|
16
|
+
|
|
13
17
|
export type RakeDbConfig = {
|
|
14
18
|
migrationsPath: string;
|
|
15
19
|
migrationsTable: string;
|
|
@@ -17,6 +21,10 @@ export type RakeDbConfig = {
|
|
|
17
21
|
noPrimaryKey?: NoPrimaryKeyOption;
|
|
18
22
|
appCodeUpdater?: AppCodeUpdater;
|
|
19
23
|
useCodeUpdater?: boolean;
|
|
24
|
+
beforeMigrate?(db: Db): Promise<void>;
|
|
25
|
+
afterMigrate?(db: Db): Promise<void>;
|
|
26
|
+
beforeRollback?(db: Db): Promise<void>;
|
|
27
|
+
afterRollback?(db: Db): Promise<void>;
|
|
20
28
|
} & QueryLogOptions;
|
|
21
29
|
|
|
22
30
|
export type AppCodeUpdater = (params: {
|