Skip to content

Commit

Permalink
Maybe fix #1725?
Browse files Browse the repository at this point in the history
  • Loading branch information
tgriesser committed Oct 9, 2016
1 parent dc431f8 commit b0d723b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
14 changes: 7 additions & 7 deletions src/schema/columnbuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ export default function ColumnBuilder(client, tableBuilder, type, args) {
// All of the modifier methods that can be used to modify the current query.
const modifiers = [
'default', 'defaultsTo', 'defaultTo', 'unsigned',
'nullable', 'notNull', 'notNullable',
'first', 'after', 'comment', 'collate'
'nullable', 'notNull', 'first', 'after', 'comment', 'collate'
];

// If we call any of the modifiers (index or otherwise) on the chainable, we pretend
// as though we're calling `table.method(column)` directly.
each(modifiers, function(method) {
const key = aliasMethod[method] || method
ColumnBuilder.prototype[method] = function() {
if (aliasMethod[method]) {
method = aliasMethod[method];
}
if (method === 'notNullable') return this.nullable(false);
this._modifiers[method] = toArray(arguments);
this._modifiers[key] = toArray(arguments);
return this;
};
});

ColumnBuilder.prototype.notNullable = function notNullable() {
return this.nullable(false)
}

each(['index', 'primary', 'unique'], function(method) {
ColumnBuilder.prototype[method] = function() {
if (this._type.toLowerCase().indexOf('increments') === -1) {
Expand Down
30 changes: 13 additions & 17 deletions src/schema/tablebuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ const columnTypes = [
'biginteger',
'bigInteger',
'string',
'timestamps',
'json',
'jsonb',
'uuid',
Expand All @@ -156,31 +155,28 @@ const columnTypes = [
each(columnTypes, function(type) {
TableBuilder.prototype[type] = function() {
const args = toArray(arguments);

// The "timestamps" call is really a compound call to set the
// `created_at` and `updated_at` columns.
if (type === 'timestamps') {
const col = (args[0] === true) ? 'timestamp' : 'datetime';
const createdAt = this[col]('created_at');
const updatedAt = this[col]('updated_at');
if (args[1] === true) {
const now = this.client.raw('CURRENT_TIMESTAMP');
createdAt.notNullable().defaultTo(now);
updatedAt.notNullable().defaultTo(now);
}
return;
}
const builder = this.client.columnBuilder(this, type, args);

this._statements.push({
grouping: 'columns',
builder
});
return builder;
};

});

// The "timestamps" call is really just sets the `created_at` and `updated_at` columns.
TableBuilder.prototype.timestamps = function timestamps() {
const method = (arguments[0] === true) ? 'timestamp' : 'datetime';
const createdAt = this[method]('created_at');
const updatedAt = this[method]('updated_at');
if (arguments[1] === true) {
const now = this.client.raw('CURRENT_TIMESTAMP');
createdAt.notNullable().defaultTo(now);
updatedAt.notNullable().defaultTo(now);
}
return;
}

// Set the comment value for a table, they're only allowed to be called
// once per table.
TableBuilder.prototype.comment = function(value) {
Expand Down

0 comments on commit b0d723b

Please sign in to comment.