Skip to content

Commit ce2d382

Browse files
STRMLsuperkhau
authored andcommitted
Fix #123: Set default value during autoupdate. (#167)
This is a mergeable update to #153 that also respects dbDefault.
1 parent 36cfe1d commit ce2d382

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

lib/migration.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ function mixinMigration(PostgreSQL) {
236236
var prop = this.getModelDefinition(model).properties[propName];
237237
var sqlCommand = self.columnEscaped(model, propName)
238238
+ ' ' + self.columnDataType(model, propName) +
239-
(self.isNullable(prop) ? '' : ' NOT NULL');
239+
(self.isNullable(prop) ? '' : ' NOT NULL') +
240+
self.columnDbDefault(model, propName);
240241
return sqlCommand;
241242
};
242243

@@ -496,7 +497,8 @@ function mixinMigration(PostgreSQL) {
496497
};
497498

498499
/*!
499-
* Get the database-default value for column from given model property
500+
* Get the database-default value for column from given model property.
501+
* Falls back to LDL's prop.default.
500502
*
501503
* @param {String} model The model name
502504
* @param {String} property The property name
@@ -505,8 +507,14 @@ function mixinMigration(PostgreSQL) {
505507
PostgreSQL.prototype.columnDbDefault = function(model, property) {
506508
var columnMetadata = this.columnMetadata(model, property);
507509
var colDefault = columnMetadata && columnMetadata.dbDefault;
510+
if (!colDefault) {
511+
var prop = this.getModelDefinition(model).properties[property];
512+
if (prop.hasOwnProperty('default')) {
513+
colDefault = String(this.escapeValue(prop.default));
514+
}
515+
}
508516

509-
return colDefault ? (' DEFAULT ' + columnMetadata.dbDefault) : '';
517+
return colDefault ? (' DEFAULT ' + colDefault) : '';
510518
};
511519

512520
/*!

lib/postgresql.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ PostgreSQL.prototype.escapeValue = function(value) {
345345
if (typeof value === 'number' || typeof value === 'boolean') {
346346
return value;
347347
}
348+
// Can't send functions, objects, arrays
349+
if (typeof value === 'object' || typeof value === 'function') {
350+
return null;
351+
}
348352
return value;
349353
};
350354

test/postgresql.migration.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ describe('migrations', function() {
1414
before(setup);
1515

1616
it('should run migration', function(done) {
17-
db.automigrate('UserDataWithIndexes', function() {
18-
done();
19-
});
17+
db.automigrate('UserDataWithIndexes', done);
2018
});
2119

2220
it('UserDataWithIndexes should have correct indexes', function(done) {
@@ -89,6 +87,7 @@ function setup(done) {
8987
birthDate: Date,
9088
pendingPeriod: Number,
9189
createdByAdmin: Boolean,
90+
deleted: {type: Boolean, required: true, default: false},
9291
}, {
9392
indexes: {
9493
udwi_index1: {

0 commit comments

Comments
 (0)