Dung (Donny) Nguyen

Senior Software Engineer

Overview of Adonis.js Migrations

Adonis.js migrations are a system for managing and version-controlling your database schema directly from your application code, instead of manually writing SQL queries. Migrations allow you to create, alter, and drop tables and other database structures using programmatic scripts, ensuring that your database schema evolves in sync with your application code.

How Migrations Work

Migration Lifecycle

Best Practices

Migration Commands (Ace CLI)

Command Description
make:migration Create a new migration file
migration:run Run all pending migrations
migration:rollback Roll back the last batch of migrations
migration:status Show the status of all migrations
migration:reset Roll back all migrations
migration:fresh Drops all tables (without running the down methods), then runs all the up methods

Example Migration File Structure

A migration file typically contains two methods:

export default class UsersTable {
  public async up () {
    // Code to create or alter tables
  }

  public async down () {
    // Code to revert the changes made in up()
  }
}

Migration in AdonisJS v5 and v6

Summary

Adonis.js migrations provide a robust, programmatic way to manage your database schema, supporting collaborative development and safe schema evolution. They are essential for maintaining consistency between your codebase and database, especially as your project grows and changes over time.