Merge branch 'master' of github.com:tgriesser/knex

* 'master' of github.com:tgriesser/knex:
  added tests for table.charset and table.collate
  added charset() and collate() for mysql schema builder
This commit is contained in:
Tim Griesser 2013-10-14 07:22:45 -04:00
commit 2ec64f6dd6
4 changed files with 44 additions and 3 deletions

View File

@ -33,8 +33,8 @@ exports.schemaGrammar = _.defaults({
var sql = baseSchemaGrammar.compileCreateTable.call(this, builder, command);
var conn = builder.client.connectionSettings;
if (conn.charset) sql += ' default character set ' + conn.charset;
if (conn.collation) sql += ' collate ' + conn.collation;
if (builder.flags.charset || conn.charset) sql += ' default character set ' + (builder.flags.charset || conn.charset);
if (builder.flags.collation || conn.collation) sql += ' collate ' + (builder.flags.collation || conn.collation);
if (builder.flags.engine) {
sql += ' engine = ' + builder.flags.engine;
}

View File

@ -56,6 +56,20 @@ define(function(require, exports) {
return this;
},
// Sets the character set for the table in MySql
charset: function(charset) {
if (!this.creating()) throw new Error('The `engine` modifier may only be used while creating a table.');
this.flags.charset = charset;
return this;
},
// Sets the collation for the table in MySql
collate: function(collation) {
if (!this.creating()) throw new Error('The `engine` modifier may only be used while creating a table.');
this.flags.collation = collation;
return this;
},
// Adds a comment to the current table being created.
comment: function(comment) {
return this._addCommand('comment', {comment: comment});

View File

@ -13,7 +13,8 @@ module.exports = function(knex) {
knex.schema.dropTableIfExists('datatype_test'),
knex.schema.dropTableIfExists('accounts'),
knex.schema.dropTableIfExists('test_default_table'),
knex.schema.dropTableIfExists('composite_key_test')
knex.schema.dropTableIfExists('composite_key_test'),
knex.schema.dropTableIfExists('charset_collate_test')
]);
});
@ -76,6 +77,18 @@ module.exports = function(knex) {
}).logMe('sql');
});
it('is possible to set the table collation with table.charset and table.collate', function() {
return knex.schema.createTable('charset_collate_test', function(table) {
table.charset('latin1');
table.collate('latin1_general_ci');
table.engine('InnoDB');
table.increments();
table.integer('account_id');
table.text('details');
table.tinyint('status');
}).logMe('sql');
});
});
describe('table', function() {

View File

@ -96,5 +96,19 @@ module.exports = {
bindings: [],
sql: ['create table "composite_key_test" ("column_a" integer, "column_b" integer)','create unique index composite_key_test_column_a_column_b_unique on "composite_key_test" ("column_a", "column_b")']
}
},
'is possible to set the table collation with table.charset and table.collate': {
mysql: {
bindings: [],
sql: ['create table `charset_collate_test` (`id` int(11) unsigned not null auto_increment primary key, `account_id` int(11), `details` text, `status` tinyint) default character set latin1 collate latin1_general_ci engine = InnoDB']
},
postgresql: {
bindings: [],
sql: ['create table "charset_collate_test" ("id" serial primary key not null, "account_id" integer, "details" text, "status" smallint)']
},
sqlite3: {
bindings: [],
sql: ['create table "charset_collate_test" ("id" integer primary key autoincrement not null, "account_id" integer, "details" text, "status" tinyint)']
}
}
};