Skip to content

Instantly share code, notes, and snippets.

@RichAyotte
Created April 11, 2014 17:35
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RichAyotte/10486685 to your computer and use it in GitHub Desktop.
Save RichAyotte/10486685 to your computer and use it in GitHub Desktop.
Initial Sequelize Migration with existing database.
// Inspired by http://bulkan-evcimen.com/using_sequelize_migrations_with_an_existing_database
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));
module.exports = {
up: function(migration, DataTypes, done) {
var db = migration.migrator.sequelize;
fs.readFileAsync(__dirname + '/initial.sql', {encoding: 'utf8'})
.then(function(initialSchema) {
var tables = initialSchema.split(';');
return db.query('SET FOREIGN_KEY_CHECKS = 0')
.then(function() {
// Run create table statements squentially
// otherwise MySQL randomly complains.
return Promise.reduce(tables, function(_, sql) {
if (sql) {
return db.query(sql);
}
}, null);
}).then(function(){
return db.query('SET FOREIGN_KEY_CHECKS = 1');
});
}).then(done)
.catch(function(err){
console.log(err);
});
}
, down: function(migration, DataTypes, done) {
migration.dropAllTables('SequelizeMeta')
.then(done)
.catch(function(err){
console.log(err);
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment