rake-db 1.3.1 → 1.3.2

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.
@@ -1,4 +1,4 @@
1
1
  import Migration from './migration';
2
- export declare const run: (db: Migration, fn: (t: Migration, up: boolean) => void | Promise<void>, version: string) => Promise<unknown[]>;
2
+ export declare const run: (db: Migration, fn: (t: Migration, up: boolean) => void | Promise<void>, version: string) => Promise<void>;
3
3
  export declare const migrate: () => Promise<void>;
4
4
  export declare const rollback: () => Promise<void>;
@@ -73,36 +73,39 @@ const getFiles = (migrationsPath, rollback) => new Promise((resolve, reject) =>
73
73
  resolve(files);
74
74
  });
75
75
  });
76
- const run = async (db, fn, version) => await db.wrapperTransaction(db, async (t) => {
77
- if (fn.toString().startsWith('async')) {
78
- await fn(t, !db.reverse);
79
- }
80
- else {
81
- if (!db.reverse) {
82
- fn(t, true);
76
+ const run = async (db, fn, version) => {
77
+ await db.wrapperTransaction(db, async (t) => {
78
+ if (fn.toString().startsWith('async')) {
79
+ await fn(t, !db.reverse);
83
80
  }
84
81
  else {
85
- const originalExec = t.exec;
86
- const argsList = []; // eslint-disable-line
87
- t.exec = (...args) => {
88
- argsList.push(args);
89
- return Promise.resolve(undefined);
90
- };
91
- await fn(t, false);
92
- t.exec = originalExec;
93
- for (const args of argsList.reverse()) {
94
- await t.exec(...args);
82
+ if (!db.reverse) {
83
+ fn(t, true);
84
+ }
85
+ else {
86
+ const originalExec = t.exec;
87
+ const argsList = []; // eslint-disable-line
88
+ t.exec = (...args) => {
89
+ argsList.push(args);
90
+ return Promise.resolve();
91
+ };
92
+ await fn(t, false);
93
+ t.exec = originalExec;
94
+ for (const [sql, value] of argsList.reverse()) {
95
+ await t.exec(sql, value);
96
+ }
95
97
  }
96
98
  }
97
- }
98
- await t.sync();
99
- if (t.failed)
100
- return;
101
- const sql = db.reverse
102
- ? `DELETE FROM "schemaMigrations" WHERE "version" = '${version}'`
103
- : `INSERT INTO "schemaMigrations" VALUES ('${version}')`;
104
- await t.exec(sql).catch(utils_1.noop);
105
- });
99
+ await t.sync();
100
+ if (t.failed)
101
+ return;
102
+ const sql = db.reverse
103
+ ? `DELETE FROM "schemaMigrations" WHERE "version" = '${version}'`
104
+ : `INSERT INTO "schemaMigrations"
105
+ VALUES ('${version}')`;
106
+ await t.exec(sql).catch(utils_1.noop);
107
+ });
108
+ };
106
109
  exports.run = run;
107
110
  const migrateFile = async (db, migrationsPath, version, file) => {
108
111
  const filePath = path.resolve(migrationsPath, file);
@@ -111,9 +114,17 @@ const migrateFile = async (db, migrationsPath, version, file) => {
111
114
  throw new Error(`Migration ${file} does not contain up or change exports`);
112
115
  else if (!migration.down && !migration.change)
113
116
  throw new Error(`Migration ${file} does not contain down or change exports`);
114
- for (const key in migration)
115
- if (key === (db.reverse ? 'down' : 'up') || key === 'change')
117
+ if (migration.before) {
118
+ await migration.before(db, !db.reverse);
119
+ }
120
+ for (const key in migration) {
121
+ if (key === (db.reverse ? 'down' : 'up') || key === 'change') {
116
122
  await exports.run(db, migration[key], version);
123
+ }
124
+ }
125
+ if (migration.after) {
126
+ await migration.after(db, !db.reverse);
127
+ }
117
128
  console.info(`${filePath} ${db.reverse ? 'rolled back' : 'migrated'}`);
118
129
  };
119
130
  const migrateDb = async (db, migrationsPath, files) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rake-db",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
4
4
  "description": "Postgresql migrations like in Ruby on Rails",
5
5
  "bin": {
6
6
  "rake-db": "dist/rake-db.js"
@@ -25,7 +25,7 @@
25
25
  "prepublish": "tsc",
26
26
  "db": "ts-node src/rake-db.ts",
27
27
  "lint": "eslint --fix src",
28
- "test": "jest test"
28
+ "test": "jest"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@types/jest": "^25.2.1",