rake-db 1.3.0 → 1.3.3

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/README.md CHANGED
@@ -41,6 +41,18 @@ Has an option to generate names of columns, indices and foreign keys in camelCas
41
41
 
42
42
  ## Get started
43
43
 
44
+ Install:
45
+
46
+ ```sh
47
+ npm i rake-db
48
+ ```
49
+
50
+ `rake-db` has peer dependencies on `ts-node` and `typescript`, if you don't have them already in your project need to install:
51
+
52
+ ```sh
53
+ npm i ts-node typescript
54
+ ```
55
+
44
56
  Add a script into `package.json`:
45
57
  ```json
46
58
  {
@@ -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>;
@@ -30,8 +30,8 @@ const migration_1 = __importDefault(require("./migration"));
30
30
  const errorCodes_1 = require("./errorCodes");
31
31
  const ts_node_1 = require("ts-node");
32
32
  ts_node_1.register({ compilerOptions: { module: 'CommonJS' } });
33
- const getMigratedVersionsQuery = (db) => db.value(`SELECT COALESCE(json_agg("schemaMigrations".version ORDER BY version), '[]')` +
34
- `FROM "schemaMigrations"`);
33
+ const getMigratedVersionsQuery = (db) => db.value(`SELECT COALESCE(json_agg(version ORDER BY version), '[]')` +
34
+ `FROM "public"."schemaMigrations"`);
35
35
  const getMigratedVersions = async (db) => {
36
36
  try {
37
37
  return await getMigratedVersionsQuery(db);
@@ -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 "public"."schemaMigrations" WHERE "version" = '${version}'`
104
+ : `INSERT INTO "public"."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.0",
3
+ "version": "1.3.3",
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",
@@ -42,9 +42,12 @@
42
42
  "typescript": "^4.0.3"
43
43
  },
44
44
  "dependencies": {
45
- "dotenv": "8.x",
45
+ "dotenv": "^16.0.1",
46
46
  "enquirer": "^2.3.6",
47
- "pg-adapter": "^1.2.5",
48
- "ts-node": "^9.1.1"
47
+ "pg-adapter": "^1.2.5"
48
+ },
49
+ "peerDependencies": {
50
+ "ts-node": "*",
51
+ "typescript": "*"
49
52
  }
50
53
  }