Skip to content

Commit

Permalink
fix(createMigrationsTable): create schema
Browse files Browse the repository at this point in the history
Create schema for migrations table before creation of migrations table
if the schema not exists

Signed-off-by: Alexander Maslov <[email protected]>
  • Loading branch information
drakmail committed May 16, 2020
1 parent 42e289b commit 6a9165f
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 53 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ cache:
directories:
- node_modules

services:
- postgresql

addons:
apt:
sources:
Expand Down
78 changes: 61 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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';
Expand All @@ -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';

Expand All @@ -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) {
Expand All @@ -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 () {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
}
});

Expand Down
72 changes: 39 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down

0 comments on commit 6a9165f

Please sign in to comment.