Created
March 19, 2012 15:11
-
-
Save doug-martin/2115744 to your computer and use it in GitHub Desktop.
Drop Foreign Key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var patio = require("../index"), | |
sql = patio.sql, | |
comb = require("comb"), | |
format = comb.string.format; | |
patio.camelize = true; | |
//comb.logging.Logger.getRootLogger().level = comb.logging.Level.ERROR; | |
//disconnect and error callback helpers | |
patio.configureLogging(); | |
var disconnect = comb.hitch(patio, "disconnect"); | |
var disconnectError = function (err) { | |
patio.logError(err); | |
patio.disconnect(); | |
}; | |
var connectAndCreateSchema = function () { | |
//This assumes new tables each time you could just connect to the database | |
return patio.connectAndExecute("mysql://test:testpass@localhost:3306/sandbox", | |
function (db, patio) { | |
//drop and recreate the user | |
db.forceCreateTable("user", function () { | |
this.primaryKey("id"); | |
this.firstName(String); | |
this.lastName(String); | |
}); | |
db.forceCreateTable("userOtherTable", function () { | |
this.primaryKey("id"); | |
this.firstName(String); | |
this.lastName(String); | |
this.foreignKey("userId", "user", "id"); | |
}) | |
}); | |
}; | |
var createData = function (db) { | |
var pArr = [], user = db.from("user"), otherUser = db.from("userOtherTable"); | |
for (var i = 0; i < 1000; i++) { | |
(function (f, l) { | |
pArr.push(function () { | |
var ret = new comb.Promise(); | |
user.insert({firstName:f, lastName:l}).then(function (user) { | |
otherUser.insert({firstName:f, lastName:l, userId:user}).then(comb.hitch(ret, "callback"), comb.hitch(ret, "errback")); | |
}, comb.hitch(ret, 'errback')); | |
return ret; | |
}); | |
})("first" + i, "last" + i); | |
} | |
return comb.serial(pArr); | |
} | |
var dropForeignKey = function (db) { | |
return db.alterTable("userOtherTable", function () { | |
this.dropColumn("userId"); | |
}); | |
}; | |
connectAndCreateSchema().then(function (db) { | |
createData(db).then(function () { | |
dropForeignKey(db).then(disconnect, disconnectError) | |
}, disconnectError) | |
}, disconnectError); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment