db-migrate-pg
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -0,1 +1,12 @@ | ||
<a name="0.3.1"></a> | ||
## [0.3.1](https://github.com/db-migrate/pg/compare/v0.3.0...v0.3.1) (2018-02-11) | ||
### Bug Fixes | ||
* **timezone:** with timezone being emitted to late ([2d3f10d](https://github.com/db-migrate/pg/commit/2d3f10d)) | ||
* **vuln:** update vulnerable dependency ([c409048](https://github.com/db-migrate/pg/commit/c409048)) | ||
<a name="0.3.0"></a> | ||
@@ -2,0 +13,0 @@ # [0.3.0](https://github.com/db-migrate/pg/compare/v0.2.5...v0.3.0) (2018-01-27) |
919
index.js
@@ -10,183 +10,188 @@ var util = require('util'); | ||
var PgDriver = Base.extend({ | ||
init: function(connection, schema, intern) { | ||
init: function (connection, schema, intern) { | ||
this.log = intern.mod.log; | ||
this.type = intern.mod.type; | ||
this._escapeString = "'"; | ||
this._super(intern); | ||
this.internals = intern; | ||
this.connection = connection; | ||
this.schema = schema || 'public'; | ||
}, | ||
this.log = intern.mod.log; | ||
this.type = intern.mod.type; | ||
this._escapeString = '\''; | ||
this._super(intern); | ||
this.internals = intern; | ||
this.connection = connection; | ||
this.schema = schema || "public"; | ||
}, | ||
startMigration: function (cb) { | ||
if (!this.internals.notransactions) { | ||
return this.runSql('BEGIN;').nodeify(cb); | ||
} else return Promise.resolve().nodeify(cb); | ||
}, | ||
startMigration: function(cb){ | ||
endMigration: function (cb) { | ||
if (!this.internals.notransactions) { | ||
return this.runSql('COMMIT;').nodeify(cb); | ||
} else return Promise.resolve(null).nodeify(cb); | ||
}, | ||
if(!this.internals.notransactions) { | ||
createColumnDef: function (name, spec, options, tableName) { | ||
// add support for datatype timetz, timestamptz | ||
// https://www.postgresql.org/docs/9.5/static/datatype.html | ||
spec.type = spec.type.replace(/^(time|timestamp)tz$/, function ($, type) { | ||
spec.timezone = true; | ||
return type; | ||
}); | ||
var type = | ||
spec.primaryKey && spec.autoIncrement ? '' : this.mapDataType(spec.type); | ||
var len = spec.length ? util.format('(%s)', spec.length) : ''; | ||
var constraint = this.createColumnConstraint( | ||
spec, | ||
options, | ||
tableName, | ||
name | ||
); | ||
if (name.charAt(0) !== '"') { | ||
name = '"' + name + '"'; | ||
} | ||
return this.runSql('BEGIN;').nodeify(cb); | ||
} | ||
else | ||
return Promise.resolve().nodeify(cb); | ||
}, | ||
return { | ||
foreignKey: constraint.foreignKey, | ||
callbacks: constraint.callbacks, | ||
constraints: [name, type, len, constraint.constraints].join(' ') | ||
}; | ||
}, | ||
endMigration: function(cb){ | ||
mapDataType: function (str) { | ||
switch (str) { | ||
case 'json': | ||
case 'jsonb': | ||
return str.toUpperCase(); | ||
case this.type.STRING: | ||
return 'VARCHAR'; | ||
case this.type.DATE_TIME: | ||
return 'TIMESTAMP'; | ||
case this.type.BLOB: | ||
return 'BYTEA'; | ||
} | ||
return this._super(str); | ||
}, | ||
if(!this.internals.notransactions) { | ||
createDatabase: function (dbName, options, callback) { | ||
var spec = ''; | ||
return this.runSql('COMMIT;').nodeify(cb); | ||
} | ||
else | ||
return Promise.resolve(null).nodeify(cb); | ||
}, | ||
if (typeof options === 'function') callback = options; | ||
createColumnDef: function(name, spec, options, tableName) { | ||
// add support for datatype timetz, timestamptz | ||
// https://www.postgresql.org/docs/9.5/static/datatype.html | ||
spec.type = spec.type.replace(/^(time|timestamp)tz$/, function($, type) { | ||
spec.timezone = true | ||
return type | ||
}) | ||
var type = (spec.primaryKey && spec.autoIncrement) ? '' : | ||
this.mapDataType(spec.type); | ||
var len = spec.length ? util.format('(%s)', spec.length) : ''; | ||
var constraint = this.createColumnConstraint(spec, options, tableName, name); | ||
if (name.charAt(0) != '"') { | ||
name = '"' + name + '"'; | ||
} | ||
this.runSql( | ||
util.format('CREATE DATABASE %s %s', this.escapeDDL(dbName), spec), | ||
callback | ||
); | ||
}, | ||
return { foreignKey: constraint.foreignKey, | ||
callbacks: constraint.callbacks, | ||
constraints: [name, type, len, constraint.constraints].join(' ') }; | ||
}, | ||
dropDatabase: function (dbName, options, callback) { | ||
var ifExists = ''; | ||
mapDataType: function(str) { | ||
switch(str) { | ||
case 'json': | ||
case 'jsonb': | ||
return str.toUpperCase() | ||
case this.type.STRING: | ||
return 'VARCHAR'; | ||
case this.type.DATE_TIME: | ||
return 'TIMESTAMP'; | ||
case this.type.BLOB: | ||
return 'BYTEA'; | ||
} | ||
return this._super(str); | ||
}, | ||
if (typeof options === 'function') callback = options; | ||
else { | ||
ifExists = options.ifExists === true ? 'IF EXISTS' : ''; | ||
} | ||
createDatabase: function(dbName, options, callback) { | ||
this.runSql( | ||
util.format('DROP DATABASE %s %s', ifExists, this.escapeDDL(dbName)), | ||
callback | ||
); | ||
}, | ||
var spec = ''; | ||
createSequence: function (sqName, options, callback) { | ||
var spec = ''; | ||
var temp = ''; | ||
if(typeof(options) === 'function') | ||
callback = options; | ||
if (typeof options === 'function') callback = options; | ||
else { | ||
temp = options.temp === true ? 'TEMP' : ''; | ||
} | ||
this.runSql(util.format('CREATE DATABASE %s %s', this.escapeDDL(dbName), | ||
spec), callback); | ||
}, | ||
this.runSql( | ||
util.format('CREATE %s SEQUENCE `%s` %s', temp, sqName, spec), | ||
callback | ||
); | ||
}, | ||
dropDatabase: function(dbName, options, callback) { | ||
var ifExists = ''; | ||
if(typeof(options) === 'function') | ||
callback = options; | ||
else | ||
{ | ||
ifExists = (options.ifExists === true) ? 'IF EXISTS' : ''; | ||
switchDatabase: function (options, callback) { | ||
if (typeof options === 'object') { | ||
if (typeof options.database === 'string') { | ||
this.log.info( | ||
'Ignore database option, not available with postgres. Use schema instead!' | ||
); | ||
this.runSql( | ||
util.format('SET search_path TO `%s`', options.database), | ||
callback | ||
); | ||
} | ||
} else if (typeof options === 'string') { | ||
this.runSql(util.format('SET search_path TO `%s`', options), callback); | ||
} else callback(null); | ||
}, | ||
this.runSql(util.format('DROP DATABASE %s %s', ifExists, this.escapeDDL(dbName)), callback); | ||
}, | ||
dropSequence: function (dbName, options, callback) { | ||
var ifExists = ''; | ||
var rule = ''; | ||
createSequence: function(sqName, options, callback) { | ||
if (typeof options === 'function') callback = options; | ||
else { | ||
ifExists = options.ifExists === true ? 'IF EXISTS' : ''; | ||
var spec = '', | ||
temp = ''; | ||
if (options.cascade === true) rule = 'CASCADE'; | ||
else if (options.restrict === true) rule = 'RESTRICT'; | ||
} | ||
if(typeof(options) === 'function') | ||
callback = options; | ||
else | ||
{ | ||
temp = (options.temp === true) ? 'TEMP' : ''; | ||
} | ||
this.runSql( | ||
util.format('DROP SEQUENCE %s `%s` %s', ifExists, dbName, rule), | ||
callback | ||
); | ||
}, | ||
this.runSql(util.format('CREATE %s SEQUENCE `%s` %s', temp, sqName, spec), callback); | ||
}, | ||
switchDatabase: function(options, callback) { | ||
if(typeof(options) === 'object') | ||
{ | ||
if(typeof(options.database) === 'string') | ||
{ | ||
this.log.info('Ignore database option, not available with postgres. Use schema instead!'); | ||
this.runSql(util.format('SET search_path TO `%s`', options.database), callback); | ||
} | ||
} | ||
else if(typeof(options) === 'string') | ||
{ | ||
this.runSql(util.format('SET search_path TO `%s`', options), callback); | ||
} | ||
else | ||
callback(null); | ||
}, | ||
dropSequence: function(dbName, options, callback) { | ||
var ifExists = '', | ||
rule = ''; | ||
if(typeof(options) === 'function') | ||
callback = options; | ||
else | ||
{ | ||
ifExists = (options.ifExists === true) ? 'IF EXISTS' : ''; | ||
if(options.cascade === true) | ||
rule = 'CASCADE'; | ||
else if(options.restrict === true) | ||
rule = 'RESTRICT'; | ||
} | ||
this.runSql(util.format('DROP SEQUENCE %s `%s` %s', ifExists, dbName, rule), callback); | ||
}, | ||
createMigrationsTable: function(callback) { | ||
var options = { | ||
columns: { | ||
'id': { type: this.type.INTEGER, notNull: true, primaryKey: true, autoIncrement: true }, | ||
'name': { type: this.type.STRING, length: 255, notNull: true}, | ||
'run_on': { type: this.type.DATE_TIME, notNull: true} | ||
createMigrationsTable: function (callback) { | ||
var options = { | ||
columns: { | ||
id: { | ||
type: this.type.INTEGER, | ||
notNull: true, | ||
primaryKey: true, | ||
autoIncrement: true | ||
}, | ||
ifNotExists: false | ||
}; | ||
name: { type: this.type.STRING, length: 255, notNull: true }, | ||
run_on: { type: this.type.DATE_TIME, notNull: true } | ||
}, | ||
ifNotExists: false | ||
}; | ||
return this.all('show server_version') | ||
.then(function(result) { | ||
if (result && result && result.length > 0 && result[0].server_version) { | ||
var version = result[0].server_version; | ||
if(version.split('.').length !== 3){ | ||
version += '.0'; | ||
return this.all('show server_version') | ||
.then( | ||
function (result) { | ||
if ( | ||
result && | ||
result && | ||
result.length > 0 && | ||
result[0].server_version | ||
) { | ||
var version = result[0].server_version; | ||
if (version.split('.').length !== 3) { | ||
version += '.0'; | ||
} | ||
options.ifNotExists = semver.gte(version, '9.1.0'); | ||
} | ||
options.ifNotExists = semver.gte(version, '9.1.0'); | ||
} | ||
// Get the current search path so we can change the current schema | ||
// if necessary | ||
return this.all("SHOW search_path"); | ||
}.bind(this)) | ||
.then(function(result) { | ||
// Get the current search path so we can change the current schema | ||
// if necessary | ||
return this.all('SHOW search_path'); | ||
}.bind(this) | ||
) | ||
.then( | ||
function (result) { | ||
var searchPath; | ||
var searchPathes = result[0].search_path.split(','); | ||
var searchPath, | ||
search_pathes = result[0].search_path.split(','); | ||
for (var i = 0; i < search_pathes.length; ++i) { | ||
if (search_pathes[i].indexOf('"') !== 0) { | ||
search_pathes[i] = '"' + search_pathes[i].trim() + '"'; | ||
for (var i = 0; i < searchPathes.length; ++i) { | ||
if (searchPathes[i].indexOf('"') !== 0) { | ||
searchPathes[i] = '"' + searchPathes[i].trim() + '"'; | ||
} | ||
} | ||
result[0].search_path = search_pathes.join(','); | ||
result[0].search_path = searchPathes.join(','); | ||
@@ -197,19 +202,22 @@ // if the user specified a different schema, prepend it to the | ||
if (this.schema === 'public') { | ||
searchPath = result[0].search_path; | ||
searchPath = result[0].search_path; | ||
} else { | ||
searchPath = '"' + this.schema + '",' + result[0].search_path; | ||
searchPath = '"' + this.schema + '",' + result[0].search_path; | ||
} | ||
return this.all('SET search_path TO ' + searchPath); | ||
}.bind(this)) | ||
.then(function() { | ||
return this.all("SELECT table_name FROM information_schema.tables WHERE table_name = '" + | ||
this.internals.migrationTable + "'" + | ||
((this.schema) ? | ||
" AND table_schema = '" + this.schema + "'" : | ||
'')); | ||
}.bind(this)) | ||
.then(function(result) { | ||
}.bind(this) | ||
) | ||
.then( | ||
function () { | ||
return this.all( | ||
"SELECT table_name FROM information_schema.tables WHERE table_name = '" + | ||
this.internals.migrationTable + | ||
"'" + | ||
(this.schema ? " AND table_schema = '" + this.schema + "'" : '') | ||
); | ||
}.bind(this) | ||
) | ||
.then( | ||
function (result) { | ||
if (result && result && result.length < 1) { | ||
@@ -220,55 +228,66 @@ return this.createTable(this.internals.migrationTable, options); | ||
} | ||
}.bind(this)).nodeify(callback); | ||
}, | ||
}.bind(this) | ||
) | ||
.nodeify(callback); | ||
}, | ||
createSeedsTable: function(callback) { | ||
var options = { | ||
columns: { | ||
'id': { type: this.type.INTEGER, notNull: true, primaryKey: true, autoIncrement: true }, | ||
'name': { type: this.type.STRING, length: 255, notNull: true}, | ||
'run_on': { type: this.type.DATE_TIME, notNull: true} | ||
createSeedsTable: function (callback) { | ||
var options = { | ||
columns: { | ||
id: { | ||
type: this.type.INTEGER, | ||
notNull: true, | ||
primaryKey: true, | ||
autoIncrement: true | ||
}, | ||
ifNotExists: false | ||
}; | ||
name: { type: this.type.STRING, length: 255, notNull: true }, | ||
run_on: { type: this.type.DATE_TIME, notNull: true } | ||
}, | ||
ifNotExists: false | ||
}; | ||
return this.all('select version() as version') | ||
.then(function(result) { | ||
if (result && result && result.length > 0 && result[0].version) { | ||
var version = result[0].version; | ||
var match = version.match(/\d+\.\d+\.\d+/); | ||
if (match && match[0] && semver.gte(match[0], '9.1.0')) { | ||
options.ifNotExists = true; | ||
return this.all('select version() as version') | ||
.then( | ||
function (result) { | ||
if (result && result && result.length > 0 && result[0].version) { | ||
var version = result[0].version; | ||
var match = version.match(/\d+\.\d+\.\d+/); | ||
if (match && match[0] && semver.gte(match[0], '9.1.0')) { | ||
options.ifNotExists = true; | ||
} | ||
} | ||
} | ||
// Get the current search path so we can change the current schema | ||
// if necessary | ||
return this.all("SHOW search_path"); | ||
}.bind(this)) | ||
.then(function(result) { | ||
// Get the current search path so we can change the current schema | ||
// if necessary | ||
return this.all('SHOW search_path'); | ||
}.bind(this) | ||
) | ||
.then( | ||
function (result) { | ||
var searchPath; | ||
// if the user specified a different schema, prepend it to the | ||
// search path. This will make all DDL/DML/SQL operate on the specified | ||
// schema. | ||
if (this.schema === 'public') { | ||
searchPath = result[0].search_path; | ||
} else { | ||
searchPath = '"' + this.schema + '",' + result[0].search_path; | ||
} | ||
// if the user specified a different schema, prepend it to the | ||
// search path. This will make all DDL/DML/SQL operate on the specified | ||
// schema. | ||
if (this.schema === 'public') { | ||
searchPath = result[0].search_path; | ||
} else { | ||
searchPath = '"' + this.schema + '",' + result[0].search_path; | ||
} | ||
return this.all('SET search_path TO ' + searchPath); | ||
}.bind(this)) | ||
.then(function() { | ||
return this.all("SELECT table_name FROM information_schema.tables WHERE table_name = '" + | ||
this.internals.seedTable + "'" + | ||
((this.schema) ? | ||
" AND table_schema = '" + this.schema + "'" : | ||
'')); | ||
}.bind(this)) | ||
.then(function(result) { | ||
}.bind(this) | ||
) | ||
.then( | ||
function () { | ||
return this.all( | ||
"SELECT table_name FROM information_schema.tables WHERE table_name = '" + | ||
this.internals.seedTable + | ||
"'" + | ||
(this.schema ? " AND table_schema = '" + this.schema + "'" : '') | ||
); | ||
}.bind(this) | ||
) | ||
.then( | ||
function (result) { | ||
if (result && result && result.length < 1) { | ||
@@ -279,236 +298,317 @@ return this.createTable(this.internals.seedTable, options); | ||
} | ||
}.bind(this)).nodeify(callback); | ||
}, | ||
}.bind(this) | ||
) | ||
.nodeify(callback); | ||
}, | ||
createColumnConstraint: function(spec, options, tableName, columnName) { | ||
var constraint = [], | ||
callbacks = [], | ||
cb; | ||
createColumnConstraint: function (spec, options, tableName, columnName) { | ||
var constraint = []; | ||
var callbacks = []; | ||
var cb; | ||
if (spec.primaryKey) { | ||
if (spec.autoIncrement) { | ||
constraint.push('SERIAL'); | ||
} | ||
if (spec.timezone) { | ||
constraint.push('WITH TIME ZONE'); | ||
} | ||
if (options.emitPrimaryKey) { | ||
constraint.push('PRIMARY KEY'); | ||
} | ||
} | ||
if (spec.primaryKey) { | ||
if (spec.autoIncrement) { | ||
constraint.push('SERIAL'); | ||
} | ||
if (spec.notNull === true) { | ||
constraint.push('NOT NULL'); | ||
} | ||
if (options.emitPrimaryKey) { | ||
constraint.push('PRIMARY KEY'); | ||
} | ||
} | ||
if (spec.unique) { | ||
constraint.push('UNIQUE'); | ||
} | ||
if (spec.notNull === true) { | ||
constraint.push('NOT NULL'); | ||
} | ||
if (spec.timezone) { | ||
constraint.push('WITH TIME ZONE') | ||
} | ||
if (spec.unique) { | ||
constraint.push('UNIQUE'); | ||
} | ||
if (spec.defaultValue !== undefined) { | ||
constraint.push('DEFAULT'); | ||
if (typeof spec.defaultValue == 'string'){ | ||
constraint.push("'" + spec.defaultValue + "'"); | ||
} else { | ||
constraint.push(spec.defaultValue); | ||
} | ||
} | ||
if (spec.defaultValue !== undefined) { | ||
constraint.push('DEFAULT'); | ||
if (typeof spec.defaultValue === 'string') { | ||
constraint.push("'" + spec.defaultValue + "'"); | ||
} else { | ||
constraint.push(spec.defaultValue); | ||
} | ||
} | ||
// keep foreignKey for backward compatiable, push to callbacks in the future | ||
if (spec.foreignKey) { | ||
// keep foreignKey for backward compatiable, push to callbacks in the future | ||
if (spec.foreignKey) { | ||
cb = this.bindForeignKey(tableName, columnName, spec.foreignKey); | ||
} | ||
if (spec.comment) { | ||
// TODO: create a new function addComment is not callable from here | ||
callbacks.push( | ||
function (tableName, columnName, comment, callback) { | ||
var sql = util.format( | ||
"COMMENT on COLUMN %s.%s IS '%s'", | ||
tableName, | ||
columnName, | ||
comment | ||
); | ||
return this.runSql(sql).nodeify(callback); | ||
}.bind(this, tableName, columnName, spec.comment) | ||
); | ||
} | ||
cb = this.bindForeignKey(tableName, columnName, spec.foreignKey); | ||
} | ||
if (spec.comment) { | ||
// TODO: create a new function addComment is not callable from here | ||
callbacks.push((function(tableName, columnName, comment, callback) { | ||
var sql = util.format("COMMENT on COLUMN %s.%s IS '%s'", tableName, columnName, comment) | ||
return this.runSql(sql).nodeify(callback) | ||
}).bind(this, tableName, columnName, spec.comment)) | ||
} | ||
return { | ||
foreignKey: cb, | ||
callbacks: callbacks, | ||
constraints: constraint.join(' ') | ||
}; | ||
}, | ||
return { foreignKey: cb, callbacks: callbacks, constraints: constraint.join(' ') }; | ||
}, | ||
renameTable: function (tableName, newTableName, callback) { | ||
var sql = util.format( | ||
'ALTER TABLE "%s" RENAME TO "%s"', | ||
tableName, | ||
newTableName | ||
); | ||
return this.runSql(sql).nodeify(callback); | ||
}, | ||
renameTable: function(tableName, newTableName, callback) { | ||
var sql = util.format('ALTER TABLE "%s" RENAME TO "%s"', tableName, newTableName); | ||
return this.runSql(sql).nodeify(callback); | ||
}, | ||
removeColumn: function (tableName, columnName, callback) { | ||
var sql = util.format( | ||
'ALTER TABLE "%s" DROP COLUMN "%s"', | ||
tableName, | ||
columnName | ||
); | ||
removeColumn: function(tableName, columnName, callback) { | ||
var sql = util.format('ALTER TABLE "%s" DROP COLUMN "%s"', tableName, columnName); | ||
return this.runSql(sql).nodeify(callback); | ||
}, | ||
return this.runSql(sql).nodeify(callback); | ||
}, | ||
renameColumn: function (tableName, oldColumnName, newColumnName, callback) { | ||
var sql = util.format( | ||
'ALTER TABLE "%s" RENAME COLUMN "%s" TO "%s"', | ||
tableName, | ||
oldColumnName, | ||
newColumnName | ||
); | ||
return this.runSql(sql).nodeify(callback); | ||
}, | ||
renameColumn: function(tableName, oldColumnName, newColumnName, callback) { | ||
var sql = util.format('ALTER TABLE "%s" RENAME COLUMN "%s" TO "%s"', tableName, oldColumnName, newColumnName); | ||
return this.runSql(sql).nodeify(callback); | ||
}, | ||
changeColumn: function (tableName, columnName, columnSpec, callback) { | ||
return setNotNull.call(this); | ||
changeColumn: function(tableName, columnName, columnSpec, callback) { | ||
return setNotNull.call(this); | ||
function setNotNull () { | ||
var setOrDrop = columnSpec.notNull === true ? 'SET' : 'DROP'; | ||
var sql = util.format( | ||
'ALTER TABLE "%s" ALTER COLUMN "%s" %s NOT NULL', | ||
tableName, | ||
columnName, | ||
setOrDrop | ||
); | ||
function setNotNull() { | ||
var setOrDrop = columnSpec.notNull === true ? 'SET' : 'DROP'; | ||
var sql = util.format('ALTER TABLE "%s" ALTER COLUMN "%s" %s NOT NULL', tableName, columnName, setOrDrop); | ||
return this.runSql(sql).nodeify(setUnique.bind(this)); | ||
} | ||
return this.runSql(sql).nodeify(setUnique.bind(this)); | ||
function setUnique (err) { | ||
if (err) { | ||
return Promise.reject(err); | ||
} | ||
function setUnique(err) { | ||
if (err) { | ||
return Promise.reject(err); | ||
} | ||
var sql; | ||
var constraintName = tableName + '_' + columnName + '_key'; | ||
var sql; | ||
var constraintName = tableName + '_' + columnName + '_key'; | ||
if (columnSpec.unique === true) { | ||
sql = util.format( | ||
'ALTER TABLE "%s" ADD CONSTRAINT "%s" UNIQUE ("%s")', | ||
tableName, | ||
constraintName, | ||
columnName | ||
); | ||
return this.runSql(sql).nodeify(setDefaultValue.bind(this)); | ||
} else if (columnSpec.unique === false) { | ||
sql = util.format( | ||
'ALTER TABLE "%s" DROP CONSTRAINT "%s"', | ||
tableName, | ||
constraintName | ||
); | ||
return this.runSql(sql).nodeify(setDefaultValue.bind(this)); | ||
} else { | ||
return setDefaultValue.call(this); | ||
} | ||
} | ||
if (columnSpec.unique === true) { | ||
sql = util.format('ALTER TABLE "%s" ADD CONSTRAINT "%s" UNIQUE ("%s")', tableName, constraintName, columnName); | ||
return this.runSql(sql).nodeify(setDefaultValue.bind(this)); | ||
} else if (columnSpec.unique === false) { | ||
sql = util.format('ALTER TABLE "%s" DROP CONSTRAINT "%s"', tableName, constraintName); | ||
return this.runSql(sql).nodeify(setDefaultValue.bind(this)); | ||
} else { | ||
return setDefaultValue.call(this); | ||
} | ||
function setDefaultValue (err) { | ||
if (err) { | ||
return Promise.reject(err).nodeify(callback); | ||
} | ||
function setDefaultValue(err) { | ||
if (err) { | ||
return Promise.reject(err).nodeify(callback); | ||
} | ||
var sql; | ||
var sql; | ||
if (columnSpec.defaultValue !== undefined) { | ||
var defaultValue = null; | ||
if (typeof columnSpec.defaultValue == 'string'){ | ||
defaultValue = "'" + columnSpec.defaultValue + "'"; | ||
} else { | ||
defaultValue = columnSpec.defaultValue; | ||
} | ||
sql = util.format('ALTER TABLE "%s" ALTER COLUMN "%s" SET DEFAULT %s', tableName, columnName, defaultValue); | ||
if (columnSpec.defaultValue !== undefined) { | ||
var defaultValue = null; | ||
if (typeof columnSpec.defaultValue === 'string') { | ||
defaultValue = "'" + columnSpec.defaultValue + "'"; | ||
} else { | ||
sql = util.format('ALTER TABLE "%s" ALTER COLUMN "%s" DROP DEFAULT', tableName, columnName); | ||
defaultValue = columnSpec.defaultValue; | ||
} | ||
return this.runSql(sql).then( | ||
setType.bind(this) | ||
).nodeify(callback); | ||
sql = util.format( | ||
'ALTER TABLE "%s" ALTER COLUMN "%s" SET DEFAULT %s', | ||
tableName, | ||
columnName, | ||
defaultValue | ||
); | ||
} else { | ||
sql = util.format( | ||
'ALTER TABLE "%s" ALTER COLUMN "%s" DROP DEFAULT', | ||
tableName, | ||
columnName | ||
); | ||
} | ||
return this.runSql(sql) | ||
.then(setType.bind(this)) | ||
.nodeify(callback); | ||
} | ||
function setType() { | ||
if (columnSpec.type !== undefined){ | ||
var using = columnSpec.using !== undefined ? | ||
columnSpec.using : util.format('USING "%s"::%s', columnName, this.mapDataType(columnSpec.type)); | ||
var len = columnSpec.length ? util.format('(%s)', columnSpec.length) : ''; | ||
var sql = util.format('ALTER TABLE "%s" ALTER COLUMN "%s" TYPE %s %s %s', tableName, columnName, this.mapDataType(columnSpec.type), len, using); | ||
return this.runSql(sql); | ||
} | ||
function setType () { | ||
if (columnSpec.type !== undefined) { | ||
var using = | ||
columnSpec.using !== undefined | ||
? columnSpec.using | ||
: util.format( | ||
'USING "%s"::%s', | ||
columnName, | ||
this.mapDataType(columnSpec.type) | ||
); | ||
var len = columnSpec.length | ||
? util.format('(%s)', columnSpec.length) | ||
: ''; | ||
var sql = util.format( | ||
'ALTER TABLE "%s" ALTER COLUMN "%s" TYPE %s %s %s', | ||
tableName, | ||
columnName, | ||
this.mapDataType(columnSpec.type), | ||
len, | ||
using | ||
); | ||
return this.runSql(sql); | ||
} | ||
}, | ||
} | ||
}, | ||
addForeignKey: function(tableName, referencedTableName, keyName, fieldMapping, rules, callback) { | ||
if(arguments.length === 5 && typeof(rules) === 'function') { | ||
callback = rules; | ||
rules = {}; | ||
} | ||
var columns = Object.keys(fieldMapping); | ||
var referencedColumns = columns.map(function (key) { return '"' + fieldMapping[key] + '"'; }); | ||
var sql = util.format('ALTER TABLE "%s" ADD CONSTRAINT "%s" FOREIGN KEY (%s) REFERENCES "%s" (%s) ON DELETE %s ON UPDATE %s', | ||
tableName, keyName, this.quoteDDLArr(columns), referencedTableName, referencedColumns, rules.onDelete || 'NO ACTION', rules.onUpdate || 'NO ACTION'); | ||
return this.runSql(sql).nodeify(callback); | ||
}, | ||
addForeignKey: function ( | ||
tableName, | ||
referencedTableName, | ||
keyName, | ||
fieldMapping, | ||
rules, | ||
callback | ||
) { | ||
if (arguments.length === 5 && typeof rules === 'function') { | ||
callback = rules; | ||
rules = {}; | ||
} | ||
var columns = Object.keys(fieldMapping); | ||
var referencedColumns = columns.map(function (key) { | ||
return '"' + fieldMapping[key] + '"'; | ||
}); | ||
var sql = util.format( | ||
'ALTER TABLE "%s" ADD CONSTRAINT "%s" FOREIGN KEY (%s) REFERENCES "%s" (%s) ON DELETE %s ON UPDATE %s', | ||
tableName, | ||
keyName, | ||
this.quoteDDLArr(columns), | ||
referencedTableName, | ||
referencedColumns, | ||
rules.onDelete || 'NO ACTION', | ||
rules.onUpdate || 'NO ACTION' | ||
); | ||
return this.runSql(sql).nodeify(callback); | ||
}, | ||
removeForeignKey: function(tableName, keyName, callback) { | ||
var sql = util.format('ALTER TABLE "%s" DROP CONSTRAINT "%s"', tableName, keyName); | ||
return this.runSql(sql).nodeify(callback); | ||
}, | ||
removeForeignKey: function (tableName, keyName, callback) { | ||
var sql = util.format( | ||
'ALTER TABLE "%s" DROP CONSTRAINT "%s"', | ||
tableName, | ||
keyName | ||
); | ||
return this.runSql(sql).nodeify(callback); | ||
}, | ||
insert: function() { | ||
insert: function () { | ||
var index = 1; | ||
var index = 1; | ||
if (arguments.length > 3) { | ||
index = 2; | ||
} | ||
if( arguments.length > 3 ) { | ||
arguments[index] = arguments[index].map(function (value) { | ||
return typeof value === 'string' ? value : JSON.stringify(value); | ||
}); | ||
index = 2; | ||
} | ||
return this._super.apply(this, arguments); | ||
}, | ||
arguments[index] = arguments[index].map(function(value) { | ||
return 'string' === typeof value ? value : JSON.stringify(value); | ||
}); | ||
runSql: function () { | ||
var callback; | ||
var minLength = 1; | ||
var params; | ||
return this._super.apply(this, arguments); | ||
}, | ||
if (typeof arguments[arguments.length - 1] === 'function') { | ||
minLength = 2; | ||
callback = arguments[arguments.length - 1]; | ||
} | ||
runSql: function() { | ||
var callback, | ||
minLength = 1; | ||
params = arguments; | ||
if (params.length > minLength) { | ||
// We have parameters, but db-migrate uses "?" for param substitutions. | ||
// PG uses "$1", "$2", etc so fix up the "?" into "$1", etc | ||
var param = params[0].split('?'); | ||
var newParam = []; | ||
for (var i = 0; i < param.length - 1; i++) { | ||
newParam.push(param[i], '$' + (i + 1)); | ||
} | ||
newParam.push(param[param.length - 1]); | ||
params[0] = newParam.join(''); | ||
} | ||
if(typeof(arguments[arguments.length - 1]) === 'function') | ||
{ | ||
minLength = 2; | ||
callback = arguments[arguments.length - 1]; | ||
} | ||
this.log.sql.apply(null, params); | ||
if (this.internals.dryRun) { | ||
return Promise.resolve().nodeify(callback); | ||
} | ||
params = arguments; | ||
if (params.length > minLength){ | ||
// We have parameters, but db-migrate uses "?" for param substitutions. | ||
// PG uses "$1", "$2", etc so fix up the "?" into "$1", etc | ||
var param = params[0].split('?'), | ||
new_param = []; | ||
for (var i = 0; i < param.length-1; i++){ | ||
new_param.push(param[i], "$" + (i+1)); | ||
} | ||
new_param.push(param[param.length-1]); | ||
params[0] = new_param.join(''); | ||
} | ||
return new Promise( | ||
function (resolve, reject) { | ||
var prCB = function (err, data) { | ||
return err ? reject(err) : resolve(data); | ||
}; | ||
this.log.sql.apply(null, params); | ||
if(this.internals.dryRun) { | ||
return Promise.resolve().nodeify(callback); | ||
} | ||
if (minLength === 2) params[params.length - 1] = prCB; | ||
else params[params.length++] = prCB; | ||
return new Promise(function(resolve, reject) { | ||
var prCB = function(err, data) { | ||
return (err ? reject(err) : resolve(data)); | ||
}; | ||
this.connection.query.apply(this.connection, params); | ||
}.bind(this) | ||
).nodeify(callback); | ||
}, | ||
if( minLength === 2 ) | ||
params[params.length - 1] = prCB; | ||
else | ||
params[params.length++] = prCB; | ||
all: function () { | ||
var params = arguments; | ||
this.connection.query.apply(this.connection, params); | ||
}.bind(this)).nodeify(callback); | ||
}, | ||
this.log.sql.apply(null, params); | ||
all: function() { | ||
params = arguments; | ||
return new Promise( | ||
function (resolve, reject) { | ||
var prCB = function (err, data) { | ||
return err ? reject(err) : resolve(data); | ||
}; | ||
this.log.sql.apply(null, params); | ||
this.connection.query(params[0], function (err, result) { | ||
prCB(err, result ? result.rows : result); | ||
}); | ||
}.bind(this) | ||
).nodeify(params[1]); | ||
}, | ||
return new Promise(function(resolve, reject) { | ||
var prCB = function(err, data) { | ||
return (err ? reject(err) : resolve(data)); | ||
}; | ||
this.connection.query.apply(this.connection, [params[0], function(err, result){ | ||
prCB(err, (result) ? result.rows : result); | ||
}]); | ||
}.bind(this)).nodeify(params[1]); | ||
}, | ||
close: function(callback) { | ||
this.connection.end(); | ||
if( typeof(callback) === 'function' ) | ||
return Promise.resolve().nodeify(callback); | ||
else | ||
return Promise.resolve(); | ||
} | ||
close: function (callback) { | ||
this.connection.end(); | ||
if (typeof callback === 'function') { | ||
return Promise.resolve().nodeify(callback); | ||
} else return Promise.resolve(); | ||
} | ||
}); | ||
@@ -518,17 +618,20 @@ | ||
exports.connect = function(config, intern, callback) { | ||
exports.connect = function (config, intern, callback) { | ||
internals = intern; | ||
internals = intern; | ||
if (config.native) { pg = pg.native; } | ||
if(!config.database) { config.database = 'postgres'; } | ||
var db = config.db || new pg.Client(config); | ||
db.connect(function(err) { | ||
if(err) { | ||
callback(err); | ||
} | ||
callback(null, new PgDriver(db, config.schema, intern)); | ||
}); | ||
if (config.native) { | ||
pg = pg.native; | ||
} | ||
if (!config.database) { | ||
config.database = 'postgres'; | ||
} | ||
var db = config.db || new pg.Client(config); | ||
db.connect(function (err) { | ||
if (err) { | ||
callback(err); | ||
} | ||
callback(null, new PgDriver(db, config.schema, intern)); | ||
}); | ||
}; | ||
exports.base = PgDriver; |
{ | ||
"name": "db-migrate-pg", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "A postgresql driver for db-migrate", | ||
@@ -35,3 +35,3 @@ "main": "index.js", | ||
"db-migrate-base": "^1.5.2", | ||
"pg": "^7.0.0", | ||
"pg": "^7.4.1", | ||
"semver": "^5.0.3" | ||
@@ -38,0 +38,0 @@ }, |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
24360
9
554
1
Updatedpg@^7.4.1