diff --git a/clients/server/mysql/schemagrammar.js b/clients/server/mysql/schemagrammar.js index 35960ca2..cf09bf40 100644 --- a/clients/server/mysql/schemagrammar.js +++ b/clients/server/mysql/schemagrammar.js @@ -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; } diff --git a/lib/schemabuilder.js b/lib/schemabuilder.js index 035d055a..545fb915 100644 --- a/lib/schemabuilder.js +++ b/lib/schemabuilder.js @@ -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}); diff --git a/test/integration/builder/schema.js b/test/integration/builder/schema.js index 26a8f628..db5d760a 100644 --- a/test/integration/builder/schema.js +++ b/test/integration/builder/schema.js @@ -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() { diff --git a/test/integration/output/Schema.js b/test/integration/output/Schema.js index e0f8847f..a59ff610 100644 --- a/test/integration/output/Schema.js +++ b/test/integration/output/Schema.js @@ -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)'] + } } }; \ No newline at end of file