Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@amitsaxena
Last active September 2, 2017 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitsaxena/95897c982cee39a3c20a074ced3e2e24 to your computer and use it in GitHub Desktop.
Save amitsaxena/95897c982cee39a3c20a074ced3e2e24 to your computer and use it in GitHub Desktop.
Quickstart guide: Migrations with node.js
// Install required modules as below:
npm install -g db-migrate
npm install -g db-migrate-mysql
// Create a database.json file:
{
"development": {
"driver": "mysql",
"host": "localhost",
"user": "root",
"password": "root",
"database": "notification_dev"
},
"production": {
"driver": "mysql",
"host": "localhost",
"user": "root",
"password": "root",
"database": "notification_prod"
}
}
// Create a migration
db-migrate create --config config/database.json add_notifications_table
// The generated migration has `up` and `down` methods, which can be modified as below:
exports.up = function(db) {
return db.createTable('notifications', {
id: { type: 'int', primaryKey: true, autoIncrement: true },
name: 'string'
});
};
exports.down = function(db) {
return db.dropTable('notifications');
};
// To run all the pending migrations use:
db-migrate up --config config/database.json
db-migrate up --config config/database.json -e production
// You can find detailed documentation here: https://db-migrate.readthedocs.io/en/latest/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment