From 6a9165f1e52480c955311d7652239c606ca10a15 Mon Sep 17 00:00:00 2001 From: Alexander Maslov Date: Thu, 9 Apr 2020 16:51:43 +0300 Subject: [PATCH] fix(createMigrationsTable): create schema Create schema for migrations table before creation of migrations table if the schema not exists Signed-off-by: Alexander Maslov --- .travis.yml | 3 ++ index.js | 78 ++++++++++++++++++++++++++++++++++++----------- package-lock.json | 72 +++++++++++++++++++++++-------------------- package.json | 6 ++-- 4 files changed, 106 insertions(+), 53 deletions(-) diff --git a/.travis.yml b/.travis.yml index 12b7c14..0b87830 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,6 +30,9 @@ cache: directories: - node_modules +services: + - postgresql + addons: apt: sources: diff --git a/index.js b/index.js index 7238c57..0266044 100644 --- a/index.js +++ b/index.js @@ -238,6 +238,12 @@ var PgDriver = Base.extend({ return this.all('SET search_path TO ' + searchPath); }.bind(this) ) + .then( + function () { + // create schema if not exists + return this.all('SET search_path TO ' + this.schema); + }.bind(this) + ) .then( function () { return this.all( @@ -394,6 +400,12 @@ var PgDriver = Base.extend({ }, renameTable: function (tableName, newTableName, callback) { + let options = {}; + if (typeof callback === 'object') { + options = callback; + callback = null; + } + var sql = util.format( 'ALTER TABLE "%s" RENAME TO "%s"', tableName, @@ -403,6 +415,20 @@ var PgDriver = Base.extend({ }, removeColumn: function (tableName, columnName, callback) { + let options = {}; + if (typeof callback === 'object') { + options = callback; + callback = null; + } + + if (options.columnStrategy === 'delay') { + return this.renameColumn( + tableName, + columnName, + options.passthrough.column + ); + } + var sql = util.format( 'ALTER TABLE "%s" DROP COLUMN "%s"', tableName, @@ -413,6 +439,12 @@ var PgDriver = Base.extend({ }, renameColumn: function (tableName, oldColumnName, newColumnName, callback) { + let options = {}; + if (typeof callback === 'object') { + options = callback; + callback = null; + } + var sql = util.format( 'ALTER TABLE "%s" RENAME COLUMN "%s" TO "%s"', tableName, @@ -423,7 +455,13 @@ var PgDriver = Base.extend({ }, changeColumn: function (tableName, columnName, columnSpec, callback) { - return setNotNull.call(this); + let options = {}; + if (typeof callback === 'object') { + options = callback; + callback = null; + } + + return setNotNull.call(this).nodeify(callback); function setNotNull () { var setOrDrop = columnSpec.notNull === true ? 'SET' : 'DROP'; @@ -434,14 +472,10 @@ var PgDriver = Base.extend({ setOrDrop ); - return this.runSql(sql).nodeify(setUnique.bind(this)); + return this.runSql(sql).then(setUnique.bind(this)); } - function setUnique (err) { - if (err) { - return Promise.reject(err); - } - + function setUnique () { var sql; var constraintName = tableName + '_' + columnName + '_key'; @@ -452,24 +486,20 @@ var PgDriver = Base.extend({ constraintName, columnName ); - return this.runSql(sql).nodeify(setDefaultValue.bind(this)); + return this.runSql(sql).then(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)); + return this.runSql(sql).then(setDefaultValue.bind(this)); } else { return setDefaultValue.call(this); } } - function setDefaultValue (err) { - if (err) { - return Promise.reject(err).nodeify(callback); - } - + function setDefaultValue () { var sql; if (columnSpec.defaultValue !== undefined) { @@ -492,9 +522,7 @@ var PgDriver = Base.extend({ columnName ); } - return this.runSql(sql) - .then(setType.bind(this)) - .nodeify(callback); + return this.runSql(sql).then(setType.bind(this)); } function setType () { @@ -553,6 +581,12 @@ var PgDriver = Base.extend({ }, removeForeignKey: function (tableName, keyName, callback) { + let options = {}; + if (typeof callback === 'object') { + options = callback; + callback = null; + } + var sql = util.format( 'ALTER TABLE "%s" DROP CONSTRAINT "%s"', tableName, @@ -654,6 +688,16 @@ var PgDriver = Base.extend({ if (typeof callback === 'function') { return Promise.resolve().nodeify(callback); } else return Promise.resolve(); + }, + + _meta: { + supports: { + // all legacy callbacks can be option objects + optionParam: true, + // support for column strategies for example on dropping not + // null columns + columnStrategies: true + } } }); diff --git a/package-lock.json b/package-lock.json index 073bad0..33d51d4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "db-migrate-pg", - "version": "1.0.0", + "version": "1.2.2", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -21,9 +21,9 @@ "dev": true }, "db-migrate-base": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/db-migrate-base/-/db-migrate-base-2.0.0.tgz", - "integrity": "sha512-GRZviOejHiiluJCAG59RxVTaDMzTTrPe2HWx44p48aigoBrr0N9NwMVs9CYEPclBuvZZLOLphQR0V6cQLHACqA==", + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/db-migrate-base/-/db-migrate-base-2.3.0.tgz", + "integrity": "sha512-mxaCkSe7JC2uksvI/rKs+wOQGBSZ6B87xa4b3i+QhB+XRBpGdpMzldKE6INf+EnM6kwhbIPKjyJZgyxui9xBfQ==", "requires": { "bluebird": "^3.1.1" } @@ -105,20 +105,21 @@ } }, "packet-reader": { - "version": "0.3.1", - "resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-0.3.1.tgz", - "integrity": "sha1-zWLmCvjX/qinBexP+ZCHHEaHHyc=" + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-1.0.0.tgz", + "integrity": "sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ==" }, "pg": { - "version": "7.8.0", - "resolved": "https://registry.npmjs.org/pg/-/pg-7.8.0.tgz", - "integrity": "sha512-yS3C9YD+ft0H7G47uU0eKajgTieggCXdA+Fxhm5G+wionY6kPBa8BEVDwPLMxQvkRkv3/LXiFEqjZm9gfxdW+g==", + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/pg/-/pg-8.1.0.tgz", + "integrity": "sha512-Jp+XSNTGYDztc2FgIbmBXeeYMR7kKjfgnl3R+ioO6rkcxDmaea+YPp/gaxe13PBnJAFYyEGl0ixpwPm2gb6eUw==", "requires": { "buffer-writer": "2.0.0", - "packet-reader": "0.3.1", - "pg-connection-string": "0.1.3", - "pg-pool": "^2.0.4", - "pg-types": "~2.0.0", + "packet-reader": "1.0.0", + "pg-connection-string": "^2.2.2", + "pg-pool": "^3.2.0", + "pg-protocol": "^1.2.2", + "pg-types": "^2.1.0", "pgpass": "1.x", "semver": "4.3.2" }, @@ -131,9 +132,9 @@ } }, "pg-connection-string": { - "version": "0.1.3", - "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-0.1.3.tgz", - "integrity": "sha1-2hhHsglA5C7hSSvq9l1J2RskXfc=" + "version": "2.2.2", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.2.2.tgz", + "integrity": "sha512-+hel4DGuSZCjCZwglAuyi+XlodHnKmrbyTw0hVWlmGN2o4AfJDkDo5obAFzblS5M5PFBMx0uDt5Y1QjlNC+tqg==" }, "pg-int8": { "version": "1.0.1", @@ -141,19 +142,24 @@ "integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==" }, "pg-pool": { - "version": "2.0.6", - "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-2.0.6.tgz", - "integrity": "sha512-hod2zYQxM8Gt482q+qONGTYcg/qVcV32VHVPtktbBJs0us3Dj7xibISw0BAAXVMCzt8A/jhfJvpZaxUlqtqs0g==" + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.2.0.tgz", + "integrity": "sha512-7BLwDNDEfPFjE9vmZLcJPLFwuDAVGZ5lIZo2MeQfwYG7EPGfdNVis/dz6obI/yKqvQIx2sf6QBKXMLB+y/ftgA==" + }, + "pg-protocol": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.2.2.tgz", + "integrity": "sha512-r8hGxHOk3ccMjjmhFJ/QOSVW5A+PP84TeRlEwB/cQ9Zu+bvtZg8Z59Cx3AMfVQc9S0Z+EG+HKhicF1W1GN5Eqg==" }, "pg-types": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.0.0.tgz", - "integrity": "sha512-THUD7gQll5tys+5eQ8Rvs7DjHiIC3bLqixk3gMN9Hu8UrCBAOjf35FoI39rTGGc3lM2HU/R+Knpxvd11mCwOMA==", + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz", + "integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==", "requires": { "pg-int8": "1.0.1", "postgres-array": "~2.0.0", "postgres-bytea": "~1.0.0", - "postgres-date": "~1.0.0", + "postgres-date": "~1.0.4", "postgres-interval": "^1.1.0" } }, @@ -176,14 +182,14 @@ "integrity": "sha1-AntTPAqokOJtFy1Hz5zOzFIazTU=" }, "postgres-date": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.3.tgz", - "integrity": "sha1-4tiXAu/bJY/52c7g/pG9BpdSV6g=" + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.5.tgz", + "integrity": "sha512-pdau6GRPERdAYUQwkBnGKxEfPyhVZXG/JiS44iZWiNdSOWE09N2lUgN6yshuq6fVSon4Pm0VMXd1srUUkLe9iA==" }, "postgres-interval": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.1.2.tgz", - "integrity": "sha512-fC3xNHeTskCxL1dC8KOtxXt7YeFmlbTYtn7ul8MkVERuTmf7pI4DrkAxcw3kh1fQ9uz4wQmd03a1mRiXUZChfQ==", + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz", + "integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==", "requires": { "xtend": "^4.0.0" } @@ -230,9 +236,9 @@ "dev": true }, "xtend": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz", - "integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68=" + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==" } } } diff --git a/package.json b/package.json index ad2ce56..0929fbc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "db-migrate-pg", - "version": "1.0.0", + "version": "1.2.2", "description": "A postgresql driver for db-migrate", "main": "index.js", "scripts": { @@ -31,8 +31,8 @@ "homepage": "https://github.com/db-migrate/pg", "dependencies": { "bluebird": "^3.1.1", - "db-migrate-base": "^2.0.0", - "pg": "^7.8.0", + "db-migrate-base": "^2.3.0", + "pg": "^8.0.3", "semver": "^5.0.3" }, "devDependencies": {