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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rake-db",
3
- "version": "2.3.8",
3
+ "version": "2.3.9",
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": {
@@ -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, config, []);
80
+ await migrate(options, conf, []);
76
81
 
77
- expect(getMigrationFiles).toBeCalledWith(config, true);
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, config, []);
188
+ await rollback(options, conf, []);
176
189
 
177
- expect(getMigrationFiles).toBeCalledWith(config, false);
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 { Adapter, AdapterOptions, MaybeArray, toArray } from 'pqb';
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 db = new Adapter(opts);
47
- const migratedVersions = await getMigratedVersionsMap(db, config);
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(db, up, file, config, opts, appCodeUpdaterCache);
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 db.close();
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: {