Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more column specs #17

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ postgres driver for db-migrate
## Installation

```
npm install db-migrate-pg
```
npm install --save db-migrate-pg
```
25 changes: 24 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ var PgDriver = Base.extend({
},

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.autoIncrement ? '' : this.mapDataType(spec.type);
var len = spec.length ? util.format('(%s)', spec.length) : '';
var constraint = this.createColumnConstraint(spec, options, tableName, name);
Expand All @@ -47,11 +53,15 @@ var PgDriver = Base.extend({
}

return { foreignKey: constraint.foreignKey,
callbacks: constraint.callbacks,
constraints: [name, type, len, constraint.constraints].join(' ') };
},

mapDataType: function(str) {
switch(str) {
case 'json':
case 'jsonb':
return str.toUpperCase()
case type.STRING:
return 'VARCHAR';
case type.DATE_TIME:
Expand Down Expand Up @@ -267,6 +277,7 @@ var PgDriver = Base.extend({

createColumnConstraint: function(spec, options, tableName, columnName) {
var constraint = [],
callbacks = [],
cb;

if (spec.primaryKey && options.emitPrimaryKey) {
Expand All @@ -284,6 +295,10 @@ var PgDriver = Base.extend({
constraint.push('UNIQUE');
}

if (spec.timezone) {
constraint.push('WITH TIME ZONE')
}

if (spec.defaultValue !== undefined) {
constraint.push('DEFAULT');
if (typeof spec.defaultValue == 'string'){
Expand All @@ -293,12 +308,20 @@ var PgDriver = Base.extend({
}
}

// 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))
}

return { foreignKey: cb, constraints: constraint.join(' ') };
return { foreignKey: cb, callbacks: callbacks, constraints: constraint.join(' ') };
},

renameTable: function(tableName, newTableName, callback) {
Expand Down
11 changes: 9 additions & 2 deletions test/pg_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ vows.describe('pg').addBatch({
smalint: dataType.SMALLINT,
dt: dataType.DATE,
dti: dataType.DATE_TIME,
dti_tz: { type: dataType.DATE_TIME, timezone: true },
bl: dataType.BOOLEAN
}, this.callback.bind(this));
},
Expand Down Expand Up @@ -77,9 +78,9 @@ vows.describe('pg').addBatch({
}.bind(this));
},

'with 10 columns': function(err, columns) {
'with 11 columns': function(err, columns) {
assert.isNotNull(columns);
assert.equal(columns.length, 10);
assert.equal(columns.length, 11);
},

'that has integer id column that is primary key, non-nullable, and auto increments': function(err, columns) {
Expand Down Expand Up @@ -127,6 +128,12 @@ vows.describe('pg').addBatch({
assert.equal(column.isNullable(), true);
},

'that has timestamp with time zone column': function(err, columns) {
var column = findByName(columns, 'dti_tz');
assert.equal(column.getDataType(), 'TIMESTAMP WITH TIME ZONE');
assert.equal(column.isNullable(), true);
},

'that has boolean bl column': function(err, columns) {
var column = findByName(columns, 'bl');
assert.equal(column.getDataType(), 'BOOLEAN');
Expand Down