Migrations are the best way of creating and maintaining database stuff. Through Knex we can easily create migrations and then finally run them against the database. After installing the "knex" package, we can use the following command:
knex migrate:make setup
The above command helps to create directory (migrations) and place a migration file in that folder.
Now edit the contents of the file to include the following stuff:
exports.up = function(knex, Promise) {
return Promise.all([
knex.schema.createTable('employees', function(table){
table.string('empname');
table.string('job');
table.timestamps();
})
])
};
exports.down = function(knex, Promise) {
return Promise.all([
knex.schema.dropTable('employees');
])
};
The above code has got two important concepts:
a. UP
b. DOWN
UP is a function used when the migration is run against the Database and DOWN is the function when we would use rollback. The code creates a table named "Employees" with 4 columns.
Happy Coding !!!.
knex migrate:make setup
The above command helps to create directory (migrations) and place a migration file in that folder.
Now edit the contents of the file to include the following stuff:
exports.up = function(knex, Promise) {
return Promise.all([
knex.schema.createTable('employees', function(table){
table.string('empname');
table.string('job');
table.timestamps();
})
])
};
exports.down = function(knex, Promise) {
return Promise.all([
knex.schema.dropTable('employees');
])
};
The above code has got two important concepts:
a. UP
b. DOWN
UP is a function used when the migration is run against the Database and DOWN is the function when we would use rollback. The code creates a table named "Employees" with 4 columns.
Happy Coding !!!.
Comments