From 8028f8734c207bca067e6732a9b9ed165741c0a8 Mon Sep 17 00:00:00 2001 From: Tobias Gurtzick Date: Wed, 13 May 2020 10:32:25 +0200 Subject: [PATCH] remove if clause on foreign key Signed-off-by: Tobias Gurtzick --- index.js | 119 +++++++++++++++++++++++++++---------------------------- 1 file changed, 59 insertions(+), 60 deletions(-) diff --git a/index.js b/index.js index fc8e0c2..dd262b4 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ var Base = require('db-migrate-base'); var Promise = require('bluebird'); 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 = "'"; @@ -15,22 +15,22 @@ var PgDriver = Base.extend({ this.schema = schema || 'public'; }, - startMigration: function(cb) { + startMigration: function (cb) { if (!this.internals.notransactions) { return this.runSql('BEGIN;').nodeify(cb); } else return Promise.resolve().nodeify(cb); }, - endMigration: function(cb) { + endMigration: function (cb) { if (!this.internals.notransactions) { return this.runSql('COMMIT;').nodeify(cb); } else return Promise.resolve(null).nodeify(cb); }, - createColumnDef: function(name, spec, options, tableName) { + 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.type = spec.type.replace(/^(time|timestamp)tz$/, function ($, type) { spec.timezone = true; return type; }); @@ -54,7 +54,7 @@ var PgDriver = Base.extend({ }; }, - _translateSpecialDefaultValues: function( + _translateSpecialDefaultValues: function ( spec, options, tableName, @@ -70,7 +70,7 @@ var PgDriver = Base.extend({ } }, - mapDataType: function(str) { + mapDataType: function (str) { switch (str) { case 'json': case 'jsonb': @@ -85,7 +85,7 @@ var PgDriver = Base.extend({ return this._super(str); }, - createDatabase: function(dbName, options, callback) { + createDatabase: function (dbName, options, callback) { var spec = ''; if (typeof options === 'function') callback = options; @@ -96,7 +96,7 @@ var PgDriver = Base.extend({ ); }, - dropDatabase: function(dbName, options, callback) { + dropDatabase: function (dbName, options, callback) { var ifExists = ''; if (typeof options === 'function') callback = options; @@ -110,7 +110,7 @@ var PgDriver = Base.extend({ ); }, - createSequence: function(sqName, options, callback) { + createSequence: function (sqName, options, callback) { var spec = ''; var temp = ''; @@ -125,7 +125,7 @@ var PgDriver = Base.extend({ ); }, - switchDatabase: function(options, callback) { + switchDatabase: function (options, callback) { if (typeof options === 'object') { if (typeof options.database === 'string') { this.log.info( @@ -141,7 +141,7 @@ var PgDriver = Base.extend({ } else callback(null); }, - dropSequence: function(dbName, options, callback) { + dropSequence: function (dbName, options, callback) { var ifExists = ''; var rule = ''; @@ -159,7 +159,7 @@ var PgDriver = Base.extend({ ); }, - createMigrationsTable: function(callback) { + createMigrationsTable: function (callback) { var options = { columns: { id: { @@ -176,7 +176,7 @@ var PgDriver = Base.extend({ return this.all('show server_version_num') .then( - function(result) { + function (result) { if (result && result.length > 0 && result[0].server_version_num) { var version = result[0].server_version_num; var major = Math.floor(version / 10000); @@ -193,9 +193,9 @@ var PgDriver = Base.extend({ ) .catch( // not all DBs support server_version_num, fall back to server_version - function() { + function () { return this.all('show server_version').then( - function(result) { + function (result) { if (result && result.length > 0 && result[0].server_version) { var version = result[0].server_version; // handle versions like “10.2 (Ubuntu 10.2)” @@ -214,7 +214,7 @@ var PgDriver = Base.extend({ }.bind(this) ) .then( - function(result) { + function (result) { var searchPath; var searchPathes = result[0].search_path.split(','); @@ -239,7 +239,7 @@ var PgDriver = Base.extend({ }.bind(this) ) .then( - function() { + function () { return this.all( "SELECT table_name FROM information_schema.tables WHERE table_name = '" + this.internals.migrationTable + @@ -249,7 +249,7 @@ var PgDriver = Base.extend({ }.bind(this) ) .then( - function(result) { + function (result) { if (result && result.length < 1) { return this.createTable(this.internals.migrationTable, options); } else { @@ -260,7 +260,7 @@ var PgDriver = Base.extend({ .nodeify(callback); }, - createSeedsTable: function(callback) { + createSeedsTable: function (callback) { var options = { columns: { id: { @@ -277,7 +277,7 @@ var PgDriver = Base.extend({ return this.all('select version() as version') .then( - function(result) { + function (result) { if (result && result.length > 0 && result[0].version) { var version = result[0].version; var match = version.match(/\d+\.\d+\.\d+/); @@ -292,7 +292,7 @@ var PgDriver = Base.extend({ }.bind(this) ) .then( - function(result) { + function (result) { var searchPath; // if the user specified a different schema, prepend it to the @@ -308,7 +308,7 @@ var PgDriver = Base.extend({ }.bind(this) ) .then( - function() { + function () { return this.all( "SELECT table_name FROM information_schema.tables WHERE table_name = '" + this.internals.seedTable + @@ -318,7 +318,7 @@ var PgDriver = Base.extend({ }.bind(this) ) .then( - function(result) { + function (result) { if (result && result.length < 1) { return this.createTable(this.internals.seedTable, options); } else { @@ -329,7 +329,7 @@ var PgDriver = Base.extend({ .nodeify(callback); }, - createColumnConstraint: function(spec, options, tableName, columnName) { + createColumnConstraint: function (spec, options, tableName, columnName) { var constraint = []; var callbacks = []; var cb; @@ -374,7 +374,7 @@ var PgDriver = Base.extend({ if (spec.comment) { // TODO: create a new function addComment is not callable from here callbacks.push( - function(tableName, columnName, comment, callback) { + function (tableName, columnName, comment, callback) { var sql = util.format( "COMMENT on COLUMN %s.%s IS '%s'", tableName, @@ -393,7 +393,7 @@ var PgDriver = Base.extend({ }; }, - renameTable: function(tableName, newTableName, callback) { + renameTable: function (tableName, newTableName, callback) { let options = {}; if (typeof callback === 'object') { options = callback; @@ -408,7 +408,7 @@ var PgDriver = Base.extend({ return this.runSql(sql).nodeify(callback); }, - removeColumn: function(tableName, columnName, callback) { + removeColumn: function (tableName, columnName, callback) { let options = {}; if (typeof callback === 'object') { options = callback; @@ -432,7 +432,7 @@ var PgDriver = Base.extend({ return this.runSql(sql).nodeify(callback); }, - renameColumn: function(tableName, oldColumnName, newColumnName, callback) { + renameColumn: function (tableName, oldColumnName, newColumnName, callback) { let options = {}; if (typeof callback === 'object') { options = callback; @@ -448,7 +448,7 @@ var PgDriver = Base.extend({ return this.runSql(sql).nodeify(callback); }, - changeColumn: function(tableName, columnName, columnSpec, callback) { + changeColumn: function (tableName, columnName, columnSpec, callback) { let options = {}; if (typeof callback === 'object') { options = callback; @@ -457,7 +457,7 @@ var PgDriver = Base.extend({ return setNotNull.call(this).nodeify(callback); - function setNotNull() { + function setNotNull () { var setOrDrop = columnSpec.notNull === true ? 'SET' : 'DROP'; var sql = util.format( 'ALTER TABLE "%s" ALTER COLUMN "%s" %s NOT NULL', @@ -469,7 +469,7 @@ var PgDriver = Base.extend({ return this.runSql(sql).then(setUnique.bind(this)); } - function setUnique() { + function setUnique () { var sql; var constraintName = tableName + '_' + columnName + '_key'; @@ -493,7 +493,7 @@ var PgDriver = Base.extend({ } } - function setDefaultValue() { + function setDefaultValue () { var sql; if (columnSpec.defaultValue !== undefined) { @@ -519,16 +519,16 @@ var PgDriver = Base.extend({ return this.runSql(sql).then(setType.bind(this)); } - function setType() { + function setType () { if (columnSpec.type !== undefined) { var using = columnSpec.using !== undefined ? columnSpec.using : util.format( - 'USING "%s"::%s', - columnName, - this.mapDataType(columnSpec.type) - ); + 'USING "%s"::%s', + columnName, + this.mapDataType(columnSpec.type) + ); var len = columnSpec.length ? util.format('(%s)', columnSpec.length) : ''; @@ -545,7 +545,7 @@ var PgDriver = Base.extend({ } }, - addForeignKey: function( + addForeignKey: function ( tableName, referencedTableName, keyName, @@ -558,7 +558,7 @@ var PgDriver = Base.extend({ rules = {}; } var columns = Object.keys(fieldMapping); - var referencedColumns = columns.map(function(key) { + var referencedColumns = columns.map(function (key) { return '"' + fieldMapping[key] + '"'; }); var sql = util.format( @@ -574,37 +574,36 @@ var PgDriver = Base.extend({ return this.runSql(sql).nodeify(callback); }, - removeForeignKey: function(tableName, keyName, callback) { + removeForeignKey: function (tableName, keyName, callback) { let options = {}; if (typeof callback === 'object') { options = callback; callback = null; } - if (type) - var sql = util.format( - 'ALTER TABLE "%s" DROP CONSTRAINT "%s"', - tableName, - keyName - ); + 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; if (arguments.length > 3) { index = 2; } - arguments[index] = arguments[index].map(function(value) { + arguments[index] = arguments[index].map(function (value) { return typeof value === 'string' ? value : JSON.stringify(value); }); return this._super.apply(this, arguments); }, - runSql: function() { + runSql: function () { var callback; var minLength = 1; var params; @@ -633,8 +632,8 @@ var PgDriver = Base.extend({ } return new Promise( - function(resolve, reject) { - var prCB = function(err, data) { + function (resolve, reject) { + var prCB = function (err, data) { return err ? reject(err) : resolve(data); }; @@ -646,7 +645,7 @@ var PgDriver = Base.extend({ ).nodeify(callback); }, - _getKV: function(table, key) { + _getKV: function (table, key) { var sql = 'SELECT * FROM ' + this._escapeDDL + @@ -656,7 +655,7 @@ var PgDriver = Base.extend({ return this.allAsync(sql, [key]).then(([row]) => row); }, - all: function(...params) { + all: function (...params) { let cb; if (typeof params[params.length - 1] === 'function') { cb = params.pop(); @@ -665,11 +664,11 @@ var PgDriver = Base.extend({ this.log.sql.apply(null, params); return new Promise( - function(resolve, reject) { - var prCB = function(err, data) { + function (resolve, reject) { + var prCB = function (err, data) { return err ? reject(err) : resolve(data); }; - params.push(function(err, result) { + params.push(function (err, result) { prCB(err, result ? result.rows : result); }); @@ -678,7 +677,7 @@ var PgDriver = Base.extend({ ).nodeify(cb); }, - close: function(callback) { + close: function (callback) { this.connection.end(); if (typeof callback === 'function') { return Promise.resolve().nodeify(callback); @@ -698,7 +697,7 @@ var PgDriver = Base.extend({ Promise.promisifyAll(PgDriver); -exports.connect = function(config, intern, callback) { +exports.connect = function (config, intern, callback) { if (config.native) { pg = pg.native; } @@ -706,7 +705,7 @@ exports.connect = function(config, intern, callback) { config.database = 'postgres'; } var db = config.db || new pg.Client(config); - db.connect(function(err) { + db.connect(function (err) { if (err) { callback(err); }