Skip to content

Commit 7024f1e

Browse files
committed
feat(uuid): enable autoIncrement alias for uuid
Signed-off-by: Tobias Gurtzick <magic@wizardtales.com>
1 parent a3d7fdf commit 7024f1e

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

index.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,83 @@ var CockroachDriver = Base.extend({
235235
// no changes are possible afterwards in cockroachdb currently
236236
return Promise.resolve();
237237
}
238+
},
239+
240+
mapDataType: function(str) {
241+
switch (str) {
242+
case "uuid":
243+
return str.toUpperCase();
244+
}
245+
return this._super(str);
246+
},
247+
248+
createColumnConstraint: function(spec, options, tableName, columnName) {
249+
var constraint = [];
250+
var callbacks = [];
251+
var cb;
252+
253+
if (spec.primaryKey) {
254+
if (spec.autoIncrement) {
255+
if (this.mapDataType(spec.type) === "UUID") {
256+
constraint.push("UUID");
257+
spec.defaultValue = { raw: "gen_random_uuid()" };
258+
} else {
259+
constraint.push("SERIAL");
260+
}
261+
}
262+
263+
if (options.emitPrimaryKey) {
264+
constraint.push("PRIMARY KEY");
265+
}
266+
}
267+
268+
if (spec.timezone) {
269+
constraint.push("WITH TIME ZONE");
270+
}
271+
272+
if (spec.notNull === true) {
273+
constraint.push("NOT NULL");
274+
}
275+
276+
if (spec.unique) {
277+
constraint.push("UNIQUE");
278+
}
279+
280+
if (spec.defaultValue !== undefined) {
281+
constraint.push("DEFAULT");
282+
if (typeof spec.defaultValue === "string" && !spec.defaultValue.raw) {
283+
constraint.push("'" + spec.defaultValue + "'");
284+
} else if (spec.defaultValue.raw) {
285+
constraint.push(spec.defaultValue.raw);
286+
} else {
287+
constraint.push(spec.defaultValue);
288+
}
289+
}
290+
291+
// keep foreignKey for backward compatiable, push to callbacks in the future
292+
if (spec.foreignKey) {
293+
cb = this.bindForeignKey(tableName, columnName, spec.foreignKey);
294+
}
295+
if (spec.comment) {
296+
// TODO: create a new function addComment is not callable from here
297+
callbacks.push(
298+
function(tableName, columnName, comment, callback) {
299+
var sql = util.format(
300+
"COMMENT on COLUMN %s.%s IS '%s'",
301+
tableName,
302+
columnName,
303+
comment
304+
);
305+
return this.runSql(sql).nodeify(callback);
306+
}.bind(this, tableName, columnName, spec.comment)
307+
);
308+
}
309+
310+
return {
311+
foreignKey: cb,
312+
callbacks: callbacks,
313+
constraints: constraint.join(" ")
314+
};
238315
}
239316
});
240317

0 commit comments

Comments
 (0)