Merge pull request #406 from hsz/chainable-first

chainable-first method for MySQL
This commit is contained in:
Tim Griesser 2014-08-14 15:15:51 -04:00
commit 980818872d
5 changed files with 25 additions and 3 deletions

View File

@ -783,7 +783,7 @@ inherits(ColumnBuilder_MySQL, Schema.ColumnBuilder);
function ColumnCompiler_MySQL() {
this.Formatter = client.Formatter;
this.modifiers = ['unsigned', 'nullable', 'defaultTo', 'after', 'comment'];
this.modifiers = ['unsigned', 'nullable', 'defaultTo', 'first', 'after', 'comment'];
Schema.ColumnCompiler.apply(this, arguments);
}
inherits(ColumnCompiler_MySQL, Schema.ColumnCompiler);
@ -848,6 +848,9 @@ ColumnCompiler_MySQL.prototype.defaultTo = function(value) {
ColumnCompiler_MySQL.prototype.unsigned = function() {
return 'unsigned';
};
ColumnCompiler_MySQL.prototype.first = function() {
return 'first';
};
ColumnCompiler_MySQL.prototype.after = function(column) {
return 'after ' + this.formatter.wrap(column);
};

View File

@ -183,6 +183,7 @@
<li> <a href="#Chainable-unsigned">unsigned</a></li>
<li> <a href="#Chainable-notNullable">notNullable</a></li>
<li> <a href="#Chainable-nullable">nullable</a></li>
<li> <a href="#Chainable-first">first</a></li>
<li> <a href="#Chainable-after">after</a></li>
<li> <a href="#Chainable-comment">comment</a></li>
</ul>
@ -1756,6 +1757,12 @@ knex.schema.raw("SET sql_mode='TRADITIONAL'")
Default on column creation, this explicitly sets a field to be nullable.
</p>
<p id="Chainable-first">
<b class="header">first</b><code>column.first()</code>
<br />
Sets the column to be inserted on the first position, only used in MySQL alter tables.
</p>
<p id="Chainable-after">
<b class="header">after</b><code>column.after(field)</code>
<br />

View File

@ -19,7 +19,7 @@ inherits(ColumnBuilder_MySQL, Schema.ColumnBuilder);
function ColumnCompiler_MySQL() {
this.Formatter = client.Formatter;
this.modifiers = ['unsigned', 'nullable', 'defaultTo', 'after', 'comment'];
this.modifiers = ['unsigned', 'nullable', 'defaultTo', 'first', 'after', 'comment'];
Schema.ColumnCompiler.apply(this, arguments);
}
inherits(ColumnCompiler_MySQL, Schema.ColumnCompiler);
@ -84,6 +84,9 @@ ColumnCompiler_MySQL.prototype.defaultTo = function(value) {
ColumnCompiler_MySQL.prototype.unsigned = function() {
return 'unsigned';
};
ColumnCompiler_MySQL.prototype.first = function() {
return 'first';
};
ColumnCompiler_MySQL.prototype.after = function(column) {
return 'after ' + this.formatter.wrap(column);
};

View File

@ -49,7 +49,7 @@ AlterMethods.alterType = function(type) {
var modifiers = [
'defaultsTo', 'defaultTo', 'unsigned',
'nullable', 'notNull', 'notNullable',
'after', 'comment'
'first', 'after', 'comment'
];
// Aliases for convenience.

View File

@ -228,6 +228,15 @@ module.exports = function(client) {
expect(tableSql[0].sql).to.equal('alter table `users` add `name` varchar(255) after `foo`');
});
it('test adding column on the first place', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.string('first_name').first();
}).toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table `users` add `first_name` varchar(255) first');
});
it('test adding string', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.string('foo');