Created
April 11, 2014 17:35
-
-
Save RichAyotte/10486685 to your computer and use it in GitHub Desktop.
Initial Sequelize Migration with existing database.
This file contains hidden or 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
// 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