node-pg-migrate
Advanced tools
Comparing version 2.2.1 to 2.3.0
# Change Log | ||
## [2.3.0] (2017-06-20) | ||
### Added | ||
- JSON config and type shorthands [see](README.md#json-configuration) #91 | ||
## [2.2.1] (2017-05-26) | ||
@@ -4,0 +10,0 @@ |
@@ -17,2 +17,4 @@ 'use strict'; | ||
exports.default = function (connection_string) { | ||
var log = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : console.error; | ||
var client = new _pg2.default.Client(connection_string); | ||
@@ -26,3 +28,3 @@ var client_active = false; | ||
if (err) { | ||
console.error('could not connect to postgres', err); | ||
log('could not connect to postgres', err); | ||
reject(err); | ||
@@ -55,3 +57,3 @@ } else { | ||
var padding = ' '.repeat(position - startLineWrapPos - 1); | ||
console.error(`Error executing: | ||
log(`Error executing: | ||
${stringStart} | ||
@@ -63,3 +65,3 @@ ${padding}^^^^${stringEnd} | ||
} else { | ||
console.error(`Error executing: | ||
log(`Error executing: | ||
${string} | ||
@@ -98,3 +100,3 @@ ${err} | ||
return promise.then(listener).catch(function (err) { | ||
return console.err(err.stack ? err.stack : err); | ||
return log(err.stack || err); | ||
}); | ||
@@ -101,0 +103,0 @@ }, Promise.resolve()).then(function () { |
@@ -46,2 +46,4 @@ 'use strict'; | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
_classCallCheck(this, MigrationBuilder); | ||
@@ -72,7 +74,7 @@ | ||
this.createTable = wrap(tables.create); | ||
this.createTable = wrap(tables.create(options.typeShorthands)); | ||
this.dropTable = wrap(tables.drop); | ||
this.renameTable = wrap(tables.renameTable); | ||
this.addColumns = wrap(tables.addColumns); | ||
this.addColumns = wrap(tables.addColumns(options.typeShorthands)); | ||
this.dropColumns = wrap(tables.dropColumns); | ||
@@ -79,0 +81,0 @@ this.renameColumn = wrap(tables.renameColumn); |
@@ -45,3 +45,2 @@ 'use strict'; | ||
// class method that creates a new migration file by cloning the migration template | ||
@@ -135,3 +134,3 @@ value: function create(name, directory) { | ||
value: function applyUp() { | ||
var pgm = new _migrationBuilder2.default(); | ||
var pgm = new _migrationBuilder2.default(this.options); | ||
@@ -143,3 +142,3 @@ return this._apply(this.up, pgm); | ||
value: function applyDown() { | ||
var pgm = new _migrationBuilder2.default(); | ||
var pgm = new _migrationBuilder2.default(this.options); | ||
@@ -146,0 +145,0 @@ if (this.down === false) { |
@@ -27,2 +27,6 @@ 'use strict'; | ||
var default_type_shorthands = { | ||
id: { type: 'serial', primaryKey: true } // convenience type for serial primary keys | ||
}; | ||
// some convenience adapters -- see above | ||
@@ -40,2 +44,5 @@ var applyTypeAdapters = function applyTypeAdapters(type) { | ||
function parseColumns(columns, table_name) { | ||
var extending_type_shorthands = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var type_shorthands = _extends({}, default_type_shorthands, extending_type_shorthands); | ||
var columnsWithOptions = _lodash2.default.mapValues(columns, function () { | ||
@@ -45,5 +52,4 @@ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
if (typeof options === 'string') { | ||
options = options === 'id' // eslint-disable-line no-param-reassign | ||
// convenience type for serial primary keys | ||
? { type: 'serial', primaryKey: true } : { type: options }; | ||
options = type_shorthands[options] // eslint-disable-line no-param-reassign | ||
? type_shorthands[options] : { type: options }; | ||
} | ||
@@ -98,15 +104,17 @@ | ||
var create = exports.create = function create(table_name, columns) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var create = exports.create = function create(type_shorthands) { | ||
return function (table_name, columns) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
/* | ||
columns - hash of columns | ||
Options | ||
table_name - the name of the table | ||
columns - see column options | ||
options.inherits - table to inherit from (optional) | ||
*/ | ||
var columnsString = parseColumns(columns, table_name).replace(/^/gm, ' '); | ||
var inherits = options.inherits ? ` INHERITS ${options.inherits}` : ''; | ||
return _utils.template`CREATE TABLE "${table_name}" (\n${columnsString}\n)${inherits};`; | ||
/* | ||
columns - hash of columns | ||
Options | ||
table_name - the name of the table | ||
columns - see column options | ||
options.inherits - table to inherit from (optional) | ||
*/ | ||
var columnsString = parseColumns(columns, table_name, type_shorthands).replace(/^/gm, ' '); | ||
var inherits = options.inherits ? ` INHERITS ${options.inherits}` : ''; | ||
return _utils.template`CREATE TABLE "${table_name}" (\n${columnsString}\n)${inherits};`; | ||
}; | ||
}; | ||
@@ -118,4 +126,6 @@ | ||
var addColumns = exports.addColumns = function addColumns(table_name, columns) { | ||
return _utils.template`ALTER TABLE "${table_name}"\n${parseColumns(columns, table_name).replace(/^/gm, ' ADD ')};`; | ||
var addColumns = exports.addColumns = function addColumns(type_shorthands) { | ||
return function (table_name, columns) { | ||
return _utils.template`ALTER TABLE "${table_name}"\n${parseColumns(columns, table_name, type_shorthands).replace(/^/gm, ' ADD ')};`; | ||
}; | ||
}; | ||
@@ -122,0 +132,0 @@ |
@@ -6,6 +6,15 @@ 'use strict'; | ||
}); | ||
exports.unlockRunner = undefined; | ||
exports.unlockRunner = exports.PgLiteral = undefined; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
var _utils = require('./utils'); | ||
Object.defineProperty(exports, 'PgLiteral', { | ||
enumerable: true, | ||
get: function get() { | ||
return _utils.PgLiteral; | ||
} | ||
}); | ||
var _lodash = require('lodash'); | ||
@@ -35,4 +44,2 @@ | ||
var _utils = require('./utils'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -87,3 +94,3 @@ | ||
return db.query(`BEGIN`).then(function () { | ||
return db.query('BEGIN').then(function () { | ||
return db.query(`LOCK "${schema}"."${options.migrations_table}" IN ACCESS EXCLUSIVE MODE`); | ||
@@ -97,3 +104,3 @@ }).then(getCurrentLockName).then(function (currentLockName) { | ||
}).then(function () { | ||
return db.query(`COMMIT`); | ||
return db.query('COMMIT'); | ||
}); | ||
@@ -100,0 +107,0 @@ }; |
@@ -93,3 +93,3 @@ 'use strict'; | ||
var errHandler = function errHandler(innerErr) { | ||
console.err(innerErr.stack ? innerErr.stack : innerErr); | ||
console.error(innerErr.stack ? innerErr.stack : innerErr); | ||
throw err; | ||
@@ -96,0 +96,0 @@ }; |
@@ -11,2 +11,3 @@ { | ||
}, | ||
"main": "dist/runner.js", | ||
"keywords": [ | ||
@@ -22,3 +23,3 @@ "db", | ||
], | ||
"version": "2.2.1", | ||
"version": "2.3.0", | ||
"engines": { | ||
@@ -46,8 +47,8 @@ "node": ">=4.0.0" | ||
"babel-preset-stage-3": "^6.24.0", | ||
"chai": "^3.5.0", | ||
"chai-as-promised": "^6.0.0", | ||
"chai": "^4.0.0", | ||
"chai-as-promised": "^7.0.0", | ||
"config": ">=1.0.0", | ||
"cross-env": "^5.0.0", | ||
"dotenv": ">=1.0.0", | ||
"eslint": "^3.17.0", | ||
"eslint": "^3.19.0", | ||
"eslint-config-airbnb-base": "11.2.0", | ||
@@ -72,4 +73,4 @@ "eslint-plugin-import": "^2.2.0", | ||
"lintfix": "npm run lint -- --fix", | ||
"prepublish": "npm run compile" | ||
"prepare": "npm run compile" | ||
} | ||
} |
@@ -51,2 +51,4 @@ # pg-migrate | ||
You can specify custom JSON file with config (format is same as for `db` entry of [config](https://www.npmjs.com/package/config) file) | ||
If a .env file exists, it will be loaded using [dotenv](https://www.npmjs.com/package/dotenv) (if installed) when running the pg-migrate binary. | ||
@@ -69,2 +71,3 @@ | ||
* `config-file` (`f`) - The file with migration JSON config (defaults to undefined) | ||
* `schema` (`s`) - The schema on which migration will be run (defaults to `public`) | ||
@@ -81,3 +84,3 @@ * `database-url-var` (`d`) - Name of env variable with database url string (defaults to `DATABASE_URL`) | ||
Most of configuration options can be also specified in `node-config` configuration file. | ||
Most of configuration options can be also specified in [config](https://www.npmjs.com/package/config) file. | ||
@@ -87,2 +90,17 @@ For SSL connection to DB you can set `PGSSLMODE` environment variable to value from [list](https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNECT-SSLMODE) other then `disable`. | ||
#### JSON Configuration | ||
You can use [config](https://www.npmjs.com/package/config) or your own json file with configuration (`config-file` command line option). | ||
Available options are: | ||
* `migrations-dir`, `migrations-schema`, `migrations-table`, `check-order` - same as above | ||
* either `url` or [`user`], [`password`], `host` (defaults to localhost), `port` (defaults to 5432), `name` - for connection details | ||
* `type-shorthands` - for column type shorthands | ||
You can specify custom types which will be expanded to column definition (e.g. for `module.exports = { "type-shorthands": { id: { type: 'uuid', primaryKey: true }, createdAt: { type: 'timestamp', notNull: true, default: new require('node-pg-migrate').PgLiteral('current_timestamp') } } }` | ||
it will in `pgm.createTable('test', { id: 'id', createdAt: 'createdAt' });` produce SQL `CREATE TABLE "test" ("id" uuid PRIMARY KEY, "createdAt" timestamp DEFAULT current_timestamp NOT NULL);`). | ||
### Locking | ||
@@ -89,0 +107,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
127199
834
425