create if not exists

This commit is contained in:
Calvin Metcalf 2014-07-17 11:27:02 -04:00
parent 030bdf1e5a
commit 957cad6a43
5 changed files with 14 additions and 5 deletions

View File

@ -25,7 +25,7 @@ function TableCompiler_MySQL() {
inherits(TableCompiler_MySQL, Schema.TableCompiler);
TableCompiler_MySQL.prototype.createQuery = function(columns) {
var conn = {}, sql = 'create table ' + this.tableName() + ' (' + columns.sql.join(', ') + ')';
var conn = {}, sql = (this._createIfNot ? 'create table if not exists ' : 'create table ') + this.tableName() + ' (' + columns.sql.join(', ') + ')';
// Check if the connection settings are set.
if (client.connectionSettings) {

View File

@ -40,7 +40,7 @@ TableCompiler_PG.prototype.compileAdd = function(builder) {
// Adds the "create" query to the query sequence.
TableCompiler_PG.prototype.createQuery = function(columns) {
this.pushQuery({
sql: 'create table ' + this.tableName() + ' (' + columns.sql.join(', ') + ')',
sql: (this._createIfNot ? 'create table if not exists ' : 'create table ') + this.tableName() + ' (' + columns.sql.join(', ') + ')',
bindings: columns.bindings
});
var hasComment = _.has(this.single, 'comment');

View File

@ -29,7 +29,7 @@ inherits(TableCompiler_SQLite3, Schema.TableCompiler);
// Create a new table.
TableCompiler_SQLite3.prototype.createQuery = function(columns) {
var sql = 'create table ' + this.tableName() + ' (' + columns.sql.join(', ');
var sql = (this._createIfNot ? 'create table if not exists ' : 'create table ') + this.tableName() + ' (' + columns.sql.join(', ');
// SQLite forces primary keys to be added when the table is initially created
// so we will need to check for a primary key commands and add the columns

View File

@ -15,7 +15,7 @@ inherits(SchemaBuilder, EventEmitter);
// Each of the schema builder methods just add to the
// "_sequence" array for consistency.
_.each([
'createTable', 'table', 'alterTable', 'hasTable', 'hasColumn',
'createTable', 'createTableIfNotExists', 'table', 'alterTable', 'hasTable', 'hasColumn',
'dropTable', 'renameTable', 'dropTableIfExists', 'raw', 'debug'
], function(method) {
SchemaBuilder.prototype[method] = function() {

View File

@ -4,12 +4,17 @@
function SchemaCompiler(builder) {
this.builder = builder;
this.initCompiler();
this._createIfNot = false;
}
function buildTable(type) {
return function(tableName, fn) {
var TableBuilder = this.client.TableBuilder;
var sql = new TableBuilder(type, tableName, fn).toSQL();
var builder = new TableBuilder(type, tableName, fn);
if (this._createIfNot){
builder._createIfNot = true;
}
var sql = builder.toSQL();
for (var i = 0, l = sql.length; i < l; i++) {
this.sequence.push(sql[i]);
}
@ -17,6 +22,10 @@ function buildTable(type) {
}
SchemaCompiler.prototype.createTable = buildTable('create');
SchemaCompiler.prototype.createTableIfNotExists = function () {
this._createIfNot = true;
this.createTable.apply(this, arguments);
};
SchemaCompiler.prototype.alterTable = buildTable('alter');
SchemaCompiler.prototype.dropTable = function(tableName) {
this.pushQuery('drop table ' + this.formatter.wrap(tableName));