Migrations
Introduction
Migrations are used to create or alter the table schema in a database. We use the powerful set of migration APIs provided by KnexJS
.
We have written few commands to ease the migration process for you though, so that you focus on doing wonderful things only.
Usage
Following are the list of operations that you can perform using the commands that we provide.
This boilerplate internally uses the package @squareboat/nest-console
package, so please make sure that you have run the command npm run build
Create a new migration
To create a new migration file, run the command mentioned below:
node cli make:migration create_users_table
This will create a new migration file in database/migrations/20200829220336_create_users_table.js
, the random number before the create_users_table is the micro timestamp.
All migrations are stored in database/migrations
folder.
exports.up = async function (knex) {
const migration = await knex.schema.createTable("users", function (table) {
table.bigIncrements("id");
table.string("email").index();
});
return migration;
};
exports.down = async function (knex) {
return knex.schema.dropTableIfExists("users");
};
To learn more about the APIs provided by KnexJS, click here.
Check Migration Status
This command will return list of completed and pending migrations.
node cli migrate:status
Run migration(s)
Use this command to run all migrations that have not yet been run.
node cli migrate
Rollback migration(s)
Rolls back the last migration batch. Only the last batch will be rolled back.
node cli migrate:rollback
Reset Database
Rollbacks all migrations, resulting in reseting the entire database.
Please use this command with caution!
node cli migrate:reset