Last active
September 2, 2017 12:05
-
-
Save amitsaxena/95897c982cee39a3c20a074ced3e2e24 to your computer and use it in GitHub Desktop.
Quickstart guide: Migrations with node.js
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
// 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