Skip to content

Commit c9533e2

Browse files
committed
feat(defaultValue): add support for timestamps and add internal handling
Signed-off-by: Tobias Gurtzick <magic@wizardtales.com>
1 parent 15adb78 commit c9533e2

3 files changed

Lines changed: 50 additions & 21 deletions

File tree

index.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,23 @@ var CockroachDriver = Base.extend({
3535
return this.runSql(sql).nodeify(callback);
3636
},
3737

38-
_handleMultiPrimaryKeys: function(primaryKeyColumns) {
38+
_translateSpecialDefaultValues: function (
39+
spec,
40+
options,
41+
tableName,
42+
columnName
43+
) {
44+
switch (spec.defaultValue.special) {
45+
case 'CURRENT_TIMESTAMP':
46+
spec.defaultValue.prep = 'CURRENT_TIMESTAMP()';
47+
break;
48+
default:
49+
this.super(spec, options, tableName, columnName);
50+
break;
51+
}
52+
},
53+
54+
_handleMultiPrimaryKeys: function (primaryKeyColumns) {
3955
return util.format(
4056
', PRIMARY KEY (%s)',
4157
this.quoteDDLArr(
@@ -253,7 +269,7 @@ var CockroachDriver = Base.extend({
253269
if (spec.autoIncrement) {
254270
if (this.mapDataType(spec.type) === 'uuid') {
255271
constraint.push('UUID');
256-
spec.defaultValue = { raw: 'gen_random_uuid()' };
272+
spec.defaultValue = { prep: 'gen_random_uuid()' };
257273
} else {
258274
constraint.push('SERIAL');
259275
}
@@ -278,10 +294,12 @@ var CockroachDriver = Base.extend({
278294

279295
if (spec.defaultValue !== undefined) {
280296
constraint.push('DEFAULT');
281-
if (typeof spec.defaultValue === 'string' && !spec.defaultValue.raw) {
297+
if (typeof spec.defaultValue === 'string') {
282298
constraint.push("'" + spec.defaultValue + "'");
283299
} else if (spec.defaultValue.raw) {
284300
constraint.push(spec.defaultValue.raw);
301+
} else if (spec.defaultValue.prep) {
302+
constraint.push(spec.defaultValue.prep);
285303
} else {
286304
constraint.push(spec.defaultValue);
287305
}

package-lock.json

Lines changed: 28 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"homepage": "https://github.com/db-migrate/cockroachdb#readme",
3232
"dependencies": {
3333
"bluebird": "^3.5.0",
34+
"db-migrate-base": "^1.6.3",
3435
"db-migrate-pg": "^0.3.1",
3536
"pg": "^7.4.1"
3637
}

0 commit comments

Comments
 (0)