Skip to content

Instantly share code, notes, and snippets.

@crrobinson14
Last active June 27, 2016 06:01
Show Gist options
  • Save crrobinson14/24450c563e0989cf733e07959bbdb5a9 to your computer and use it in GitHub Desktop.
Save crrobinson14/24450c563e0989cf733e07959bbdb5a9 to your computer and use it in GitHub Desktop.
// NOTE: Gist doesn't allow subdir names. This would be in migrations/
module.exports = {
description: 'Migration 00047: Sample migration file',
// Make the appropriate updates
up: function(orm, DataTypes, queryInterface) {
return orm.Promise.all([
queryInterface.addColumn('mytable', 'myField', { type: DataTypes.DATE }),
queryInterface.addColumn('mytable', 'myField2', { type: DataTypes.INTEGER, defaultValue: 0 })
]);
},
// Revert the updates
down: function(orm, DataTypes, queryInterface) {
return orm.Promise.all([
queryInterface.removeColumn('mytable', 'myField'),
queryInterface.removeColumn('mytable', 'myField2')
]);
}
};
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();
};
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);
});
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