node-pg-migrate
Advanced tools
Comparing version 3.10.1 to 3.11.0
# Change Log | ||
## [3.11.0](2018-09-11) | ||
### Added | ||
- Ability to mark migrations as run [#324](https://github.com/salsita/node-pg-migrate/pull/324) | ||
## [3.10.1](2018-09-05) | ||
@@ -4,0 +10,0 @@ |
@@ -56,2 +56,19 @@ "use strict"; | ||
_getMarkAsRun(action) { | ||
const schema = getMigrationTableSchema(this.options); | ||
const migrationsTable = this.options.migrationsTable; | ||
const name = this.name; | ||
switch (action) { | ||
case this.down: | ||
this.log(`### MIGRATION ${this.name} (DOWN) ###`); | ||
return `DELETE FROM "${schema}"."${migrationsTable}" WHERE name='${name}';`; | ||
case this.up: | ||
this.log(`### MIGRATION ${this.name} (UP) ###`); | ||
return `INSERT INTO "${schema}"."${migrationsTable}" (name, run_on) VALUES ('${name}', NOW());`; | ||
default: | ||
throw new Error("Unknown direction"); | ||
} | ||
} | ||
_apply(action, pgm) { | ||
@@ -71,19 +88,4 @@ var _this = this; | ||
const schema = getMigrationTableSchema(_this.options); | ||
const migrationsTable = _this.options.migrationsTable; | ||
const name = _this.name; | ||
sqlSteps.push(_this._getMarkAsRun(action)); | ||
switch (action) { | ||
case _this.down: | ||
_this.log(`### MIGRATION ${_this.name} (DOWN) ###`); | ||
sqlSteps.push(`DELETE FROM "${schema}"."${migrationsTable}" WHERE name='${name}';`); | ||
break; | ||
case _this.up: | ||
_this.log(`### MIGRATION ${_this.name} (UP) ###`); | ||
sqlSteps.push(`INSERT INTO "${schema}"."${migrationsTable}" (name, run_on) VALUES ('${name}', NOW());`); | ||
break; | ||
default: | ||
throw new Error("Unknown direction"); | ||
} | ||
if (!_this.options.singleTransaction && pgm.isUsingTransaction()) { | ||
@@ -112,23 +114,37 @@ // if not in singleTransaction mode we need to create our own transaction | ||
applyUp() { | ||
const pgm = new MigrationBuilder(this.typeShorthands, this.db); | ||
_getAction(direction) { | ||
if (direction === "down") { | ||
if (this.down === false) { | ||
throw new Error(`User has disabled down migration on file: ${this.name}`); | ||
} | ||
return this._apply(this.up, pgm); | ||
if (this.down === undefined) { | ||
this.down = this.up; | ||
} | ||
} | ||
const action = this[direction]; | ||
if (typeof action !== "function") { | ||
throw new Error(`Unknown value for direction: ${direction}`); | ||
} | ||
return action; | ||
} | ||
applyDown() { | ||
apply(direction) { | ||
const pgm = new MigrationBuilder(this.typeShorthands, this.db); | ||
const action = this._getAction(direction); | ||
if (this.down === false) { | ||
return Promise.reject(new Error(`User has disabled down migration on file: ${this.name}`)); | ||
} | ||
if (this.down === undefined) { | ||
if (this.down === this.up) { | ||
// automatically infer the down migration by running the up migration in reverse mode... | ||
pgm.enableReverseMode(); | ||
this.down = this.up; | ||
} | ||
return this._apply(this.down, pgm); | ||
return this._apply(action, pgm); | ||
} | ||
markAsRun(direction) { | ||
return this.db.query(this._getMarkAsRun(this._getAction(direction))); | ||
} | ||
}; |
@@ -170,3 +170,3 @@ "use strict"; | ||
const runMigrations = (toRun, direction) => toRun.reduce((promise, migration) => promise.then(() => direction === "up" ? migration.applyUp() : migration.applyDown()), Promise.resolve()); | ||
const runMigrations = (toRun, method, direction) => toRun.reduce((promise, migration) => promise.then(() => migration[method](direction)), Promise.resolve()); | ||
@@ -218,6 +218,8 @@ const runner = (() => { | ||
if (options.singleTransaction) { | ||
if (options.fake) { | ||
yield runMigrations(toRun, "markAsRun", options.direction); | ||
} else if (options.singleTransaction) { | ||
yield db.query("BEGIN"); | ||
try { | ||
yield runMigrations(toRun, options.direction); | ||
yield runMigrations(toRun, "apply", options.direction); | ||
yield db.query("COMMIT"); | ||
@@ -230,3 +232,3 @@ } catch (err) { | ||
} else { | ||
yield runMigrations(toRun, options.direction); | ||
yield runMigrations(toRun, "apply", options.direction); | ||
} | ||
@@ -233,0 +235,0 @@ } finally { |
@@ -21,3 +21,4 @@ # Programmatic API | ||
- `noLock` _[boolean]_ - Disables locking mechanism and checks | ||
- `fake` _[boolean]_ - Mark migrations as run without actually performing them (use with caution!) | ||
- `dryRun` _[boolean]_ | ||
- `log` _[function]_ - Redirect log messages to this function, rather than `console.log` |
@@ -79,2 +79,3 @@ # CLI Usage | ||
- `no-lock` - Disables locking mechanism and checks (useful for DBs which does not support SQL commands used for [locking](migrations.md#locking)) | ||
- `fake` - Mark migrations as run without actually performing them (use with caution!) | ||
@@ -81,0 +82,0 @@ See all by running `node-pg-migrate --help`. |
@@ -505,2 +505,3 @@ // Type definitions for node-pg-migrate | ||
noLock?: boolean, | ||
fake?: boolean, | ||
log?: (msg:string) => void; | ||
@@ -507,0 +508,0 @@ } |
@@ -56,11 +56,3 @@ /* | ||
async _apply(action, pgm) { | ||
if (action.length === 2) { | ||
await new Promise(resolve => action(pgm, resolve)); | ||
} else { | ||
await action(pgm); | ||
} | ||
const sqlSteps = pgm.getSqlSteps(); | ||
_getMarkAsRun(action) { | ||
const schema = getMigrationTableSchema(this.options); | ||
@@ -72,16 +64,22 @@ const { migrationsTable } = this.options; | ||
this.log(`### MIGRATION ${this.name} (DOWN) ###`); | ||
sqlSteps.push( | ||
`DELETE FROM "${schema}"."${migrationsTable}" WHERE name='${name}';` | ||
); | ||
break; | ||
return `DELETE FROM "${schema}"."${migrationsTable}" WHERE name='${name}';`; | ||
case this.up: | ||
this.log(`### MIGRATION ${this.name} (UP) ###`); | ||
sqlSteps.push( | ||
`INSERT INTO "${schema}"."${migrationsTable}" (name, run_on) VALUES ('${name}', NOW());` | ||
); | ||
break; | ||
return `INSERT INTO "${schema}"."${migrationsTable}" (name, run_on) VALUES ('${name}', NOW());`; | ||
default: | ||
throw new Error("Unknown direction"); | ||
} | ||
} | ||
async _apply(action, pgm) { | ||
if (action.length === 2) { | ||
await new Promise(resolve => action(pgm, resolve)); | ||
} else { | ||
await action(pgm); | ||
} | ||
const sqlSteps = pgm.getSqlSteps(); | ||
sqlSteps.push(this._getMarkAsRun(action)); | ||
if (!this.options.singleTransaction && pgm.isUsingTransaction()) { | ||
@@ -109,25 +107,39 @@ // if not in singleTransaction mode we need to create our own transaction | ||
applyUp() { | ||
const pgm = new MigrationBuilder(this.typeShorthands, this.db); | ||
_getAction(direction) { | ||
if (direction === "down") { | ||
if (this.down === false) { | ||
throw new Error( | ||
`User has disabled down migration on file: ${this.name}` | ||
); | ||
} | ||
return this._apply(this.up, pgm); | ||
if (this.down === undefined) { | ||
this.down = this.up; | ||
} | ||
} | ||
const action = this[direction]; | ||
if (typeof action !== "function") { | ||
throw new Error(`Unknown value for direction: ${direction}`); | ||
} | ||
return action; | ||
} | ||
applyDown() { | ||
apply(direction) { | ||
const pgm = new MigrationBuilder(this.typeShorthands, this.db); | ||
const action = this._getAction(direction); | ||
if (this.down === false) { | ||
return Promise.reject( | ||
new Error(`User has disabled down migration on file: ${this.name}`) | ||
); | ||
} | ||
if (this.down === undefined) { | ||
if (this.down === this.up) { | ||
// automatically infer the down migration by running the up migration in reverse mode... | ||
pgm.enableReverseMode(); | ||
this.down = this.up; | ||
} | ||
return this._apply(this.down, pgm); | ||
return this._apply(action, pgm); | ||
} | ||
markAsRun(direction) { | ||
return this.db.query(this._getMarkAsRun(this._getAction(direction))); | ||
} | ||
}; |
@@ -174,8 +174,5 @@ const path = require("path"); | ||
const runMigrations = (toRun, direction) => | ||
const runMigrations = (toRun, method, direction) => | ||
toRun.reduce( | ||
(promise, migration) => | ||
promise.then( | ||
() => (direction === "up" ? migration.applyUp() : migration.applyDown()) | ||
), | ||
(promise, migration) => promise.then(() => migration[method](direction)), | ||
Promise.resolve() | ||
@@ -228,6 +225,8 @@ ); | ||
if (options.singleTransaction) { | ||
if (options.fake) { | ||
await runMigrations(toRun, "markAsRun", options.direction); | ||
} else if (options.singleTransaction) { | ||
await db.query("BEGIN"); | ||
try { | ||
await runMigrations(toRun, options.direction); | ||
await runMigrations(toRun, "apply", options.direction); | ||
await db.query("COMMIT"); | ||
@@ -240,3 +239,3 @@ } catch (err) { | ||
} else { | ||
await runMigrations(toRun, options.direction); | ||
await runMigrations(toRun, "apply", options.direction); | ||
} | ||
@@ -243,0 +242,0 @@ } finally { |
{ | ||
"name": "node-pg-migrate", | ||
"version": "3.10.1", | ||
"version": "3.11.0", | ||
"description": "Postgresql database migration management tool for node.js", | ||
@@ -61,3 +61,3 @@ "author": "Theo Ephraim", | ||
"dotenv": ">=1.0.0", | ||
"eslint": "5.4.0", | ||
"eslint": "5.5.0", | ||
"eslint-config-airbnb-base": "13.1.0", | ||
@@ -74,3 +74,3 @@ "eslint-config-prettier": "3.0.1", | ||
"proxyquire": "2.1.0", | ||
"sinon": "6.1.5", | ||
"sinon": "6.2.0", | ||
"sinon-chai": "3.2.0" | ||
@@ -77,0 +77,0 @@ }, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
263082
4550
75