Last active
June 27, 2016 06:01
-
-
Save crrobinson14/24450c563e0989cf733e07959bbdb5a9 to your computer and use it in GitHub Desktop.
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
function getMigrator(orm) { | |
var Umzug = require('umzug'), | |
DataTypes = require('sequelize/lib/data-types'); | |
var umzug = new Umzug({ | |
storage: 'sequelize', | |
storageOptions: { | |
sequelize: sequelize, | |
model: orm.models.migration | |
}, | |
upName: 'up', | |
downName: 'down', | |
migrations: { | |
params: [orm, DataTypes, sequelize.getQueryInterface()], | |
path: path.join(__dirname, 'migrations'), | |
pattern: /^\d+[\w-]+\.js$/ | |
} | |
}); | |
umzug.on('migrated', function(name, migration) { | |
var file = require(migration.path); | |
orm.logger.info('Migrated ' + name + ': ' + (file.description || 'No Description')); | |
}); | |
umzug.on('reverted', function(name, migration) { | |
var file = require(migration.path); | |
orm.logger.info('Reverted ' + name + ': ' + (file.description || 'No Description')); | |
}); | |
return umzug; | |
} | |
/** | |
* Perform a migration of all pending updates. | |
* @returns {Promise} | |
*/ | |
ORM.prototype.migrate = function() { | |
if (!this.initialized) { | |
throw new Error('ORM must be initialized before migrating.'); | |
} | |
return getMigrator(this).up(); | |
}; | |
/** | |
* Revert the most recently-performed migration. | |
* @returns {Promise} | |
*/ | |
ORM.prototype.revert = function() { | |
if (!this.initialized) { | |
throw new Error('ORM must be initialized before reverting.'); | |
} | |
return getMigrator(this).down(); | |
}; |
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
var Store = require('store'), | |
config = require('../local-config/mysql'), | |
logger = require('winston'), | |
env = process.env.NODE_ENV, | |
configBlock = config[env] || config.default; | |
var store = new Store(configBlock.mysql(), logger); | |
store.init().then(function() { | |
return store.migrate(); | |
}).catch(function(e) { | |
console.log(e.stack); | |
console.error(e.message); | |
console.error(e.sql); | |
}).finally(function() { | |
process.exit(0); | |
}); |
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
var Store = require('store'), | |
config = require('../local-config/mysql'), | |
logger = require('winston'), | |
env = process.env.NODE_ENV, | |
configBlock = config[env] || config.default; | |
var store = new Store(configBlock.mysql(), logger); | |
store.init().then(function() { | |
return store.revert(); | |
}).catch(function(e) { | |
console.log(e.stack); | |
console.error(e.message); | |
console.error(e.sql); | |
}).finally(function() { | |
process.exit(0); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment