adding ability to specify engine, collation, charset on createTable

This commit is contained in:
Tim Griesser 2013-05-15 19:05:55 -04:00
parent 567ef6b6d7
commit 0f38f04de2
4 changed files with 42 additions and 21 deletions

View File

@ -24,11 +24,11 @@ _.extend(MysqlClient.prototype, base.protoProps, {
// If we have a debug flag set, console.log the query.
if (debug) base.debug(builder, conn);
// Call the querystring and then release the client
conn.query(builder.sql, builder.bindings, function (err, resp) {
if (err) { return dfd.reject(err); }
if (builder._source === 'Raw') return dfd.resolve(resp);
if (builder._source === 'SchemaBuilder') {
@ -84,7 +84,22 @@ MysqlClient.schemaGrammar = _.extend({}, base.schemaGrammar, MysqlClient.grammar
// The possible column modifiers.
modifiers: ['Unsigned', 'Nullable', 'Default', 'Increment', 'After'],
// Compile a create table command.
compileCreateTable: function(blueprint, command) {
var sql = base.schemaGrammar.compileCreateTable.call(this, blueprint, command);
var conn = blueprint.client.connectionSettings;
if (conn.charset) sql += ' default character set ' + conn.charset;
if (conn.collation) sql += ' collate ' + conn.collation;
if (blueprint.useEngine) {
sql += ' engine = ' + blueprint.useEngine;
}
return sql;
},
// Compile the query to determine if a table exists.
compileTableExists: function(blueprint) {
blueprint.bindings.unshift(blueprint.client.connectionSettings.database);
@ -102,7 +117,7 @@ MysqlClient.schemaGrammar = _.extend({}, base.schemaGrammar, MysqlClient.grammar
command.name = null;
return this.compileKey(blueprint, command, 'primary key');
},
// Compile a unique key command.
compileUnique: function(blueprint, command) {
return this.compileKey(blueprint, command, 'unique');
@ -119,13 +134,13 @@ MysqlClient.schemaGrammar = _.extend({}, base.schemaGrammar, MysqlClient.grammar
var table = this.wrapTable(blueprint);
return 'alter table ' + table + " add " + type + " " + command.index + "(" + columns + ")";
},
// Compile a drop column command.
compileDropColumn: function(blueprint, command) {
var columns = this.prefixArray('drop', this.wrapArray(command.columns));
return 'alter table ' + this.wrapTable(blueprint) + ' ' + columns.join(', ');
},
// Compile a drop primary key command.
compileDropPrimary: function(blueprint, command) {
return 'alter table ' + this.wrapTable(blueprint) + ' drop primary key';
@ -140,7 +155,7 @@ MysqlClient.schemaGrammar = _.extend({}, base.schemaGrammar, MysqlClient.grammar
compileDropIndex: function(blueprint, command) {
return 'alter table ' + this.wrapTable(blueprint) + ' drop index ' + command.index;
},
// Compile a drop foreign key command.
compileDropForeign: function(blueprint, command) {
return 'alter table ' + this.wrapTable(blueprint) + " drop foreign key " + command.index;
@ -150,7 +165,7 @@ MysqlClient.schemaGrammar = _.extend({}, base.schemaGrammar, MysqlClient.grammar
compileRenameTable: function(blueprint, command) {
return 'rename table ' + this.wrapTable(blueprint) + ' to ' + this.wrapTable(command.to);
},
// Create the column definition for a string type.
typeString: function(column) {
return "varchar(" + column.length + ")";
@ -179,7 +194,7 @@ MysqlClient.schemaGrammar = _.extend({}, base.schemaGrammar, MysqlClient.grammar
typeFloat: function(column) {
return 'float(' + column.total + ',' + column.places + ')';
},
// Create the column definition for a decimal type.
typeDecimal: function(column) {
return 'decimal(' + column.precision + ', ' + column.scale + ')';

View File

@ -1345,6 +1345,12 @@
after: function(name) {
this.isAfter = name;
return this;
},
// Sets the engine to use when creating the table in MySql
engine: function(name) {
this.useEngine = name;
return this;
}
};

View File

@ -4,7 +4,7 @@ module.exports = {
mysql: {
database: 'knex_test',
user: 'root',
encoding: 'utf8'
charset: 'utf8'
},
postgres: {

View File

@ -16,7 +16,7 @@ module.exports = {
},
'schema.2': {
mysql: {
sql: ['create table `test_table_one` (`id` int(11) not null auto_increment primary key, `first_name` varchar(255) not null, `last_name` varchar(255) not null, `email` varchar(255) null, `logins` int(11) not null default \'1\', `about` text not null, `created_at` timestamp default 0 not null, `updated_at` timestamp default 0 not null)','alter table `test_table_one` add unique test_table_one_email_unique(`email`)','alter table `test_table_one` add index test_table_one_logins_index(`logins`)'],
sql: ['create table `test_table_one` (`id` int(11) not null auto_increment primary key, `first_name` varchar(255) not null, `last_name` varchar(255) not null, `email` varchar(255) null, `logins` int(11) not null default \'1\', `about` text not null, `created_at` timestamp default 0 not null, `updated_at` timestamp default 0 not null) default character set utf8','alter table `test_table_one` add unique test_table_one_email_unique(`email`)','alter table `test_table_one` add index test_table_one_logins_index(`logins`)'],
bindings: []
},
postgres: {
@ -30,7 +30,7 @@ module.exports = {
},
'schema.3': {
mysql: {
sql: ['create table `test_table_two` (`id` int(11) not null auto_increment primary key, `account_id` int(11) not null, `details` text not null, `status` tinyint not null)'],
sql: ['create table `test_table_two` (`id` int(11) not null auto_increment primary key, `account_id` int(11) not null, `details` text not null, `status` tinyint not null) default character set utf8'],
bindings: []
},
postgres: {
@ -44,7 +44,7 @@ module.exports = {
},
'schema.4': {
mysql: {
sql: ['create table `test_table_three` (`main` int(11) not null, `paragraph` text not null)','alter table `test_table_three` add primary key test_table_three_main_primary(`main`)'],
sql: ['create table `test_table_three` (`main` int(11) not null, `paragraph` text not null) default character set utf8','alter table `test_table_three` add primary key test_table_three_main_primary(`main`)'],
bindings: []
},
postgres: {
@ -1971,7 +1971,7 @@ module.exports = {
},{
email: 'test6@example.com',
logins: 2
}, {
},{
email: 'test100@example.com',
logins: 1
}],
@ -2017,34 +2017,34 @@ module.exports = {
'aggregate.3': {
mysql: [{
aggregate: 2
}, {
},{
aggregate: 4
}],
postgres: [{
aggregate: 2
}, {
},{
aggregate: 4
}],
sqlite3: [{
aggregate: 2
}, {
},{
aggregate: 4
}]
},
'aggregate.4': {
mysql: [{
aggregate: 5
}, {
},{
aggregate: 1
}],
postgres: [{
aggregate: 1
}, {
},{
aggregate: 5
}],
sqlite3: [{
aggregate: 5
}, {
},{
aggregate: 1
}]
},
@ -2543,4 +2543,4 @@ module.exports = {
}]
}
}
}
}