Migrations

Migrations are like version control for your database. They allow your team to define and share the structure of the application database. If you've ever had to ask a colleague to manually add a column to your local database after updating your version control changes, you've encountered the problem that database migrations solve.

Generating Migrations

You can use the make:migration command to generate a database migration. The new migration will be placed in the src/database/migrations directory of your project. Each migration filename contains a timestamp that allows Lithe to determine the order of the migrations.

php line make:migration MigrationName --template=TemplateName

Where:

  • MigrationName: Name of the migration to be created.
  • --template=TemplateName: Optional. Specifies the template to be used for generating the migration. If not specified, the template defined in the DB_CONNECTION_METHOD environment variable will be used.

Migration Templates

Migration templates determine how migration files are generated, adapted for different ORMs or database approaches. Lithe supports the following templates:

  • eloquent: Template for migrations using the Eloquent ORM.
  • mysqli: Template for migrations using MySQLi.
  • default: Default template for custom or generic migrations.

To implement migrations in Lithe using different templates like Eloquent or MySQLi, it's important to understand how each of them works and how to create the corresponding migration files.

Database Migration

Lithe supports various forms of migration to manage databases efficiently and simplistically. Here is an example of how to create a migration for the users table, regardless of which ORM or connection method is being used:

1

Creating the Migration

Run the following command to create a migration:

php line make:migration CreateUsersTable

This will create a migration file inside the src/database/migrations/ directory. For example, YYYY_MM_DD_HHMMSS_CreateUsersTable.php.

2

Example Migration File


Running Migrations

After creating the migration files as shown in the examples above, you need to run them to apply the changes to the database.

To run all your pending migrations, use the command:

php line migrate

Rolling Back Migrations

To roll back the last migration operation, you can use the rollback command from Line. This command rolls back the last 'batch' of migrations, which may include multiple migration files.

php line migrate:rollback

You can roll back a specific "batch" of migrations by providing the batch option to the rollback command, where the batch option corresponds to a "batch" value in your application's migration table. For example, the following command will roll back all migrations in "batch" three:

php line migrate:rollback --batch=3

This will roll back all migrations that were grouped in the third "batch."

Using the migrate:reset command, all migrations in your application will be rolled back:

php line migrate:reset
Rollback and Migrate Using a Single Command

The migrate:refresh command will roll back all your migrations and then run the migrate command. This effectively recreates your entire database:

php line migrate:refresh

Managing database migrations is essential for maintaining the integrity and evolution of data structures throughout the lifecycle of an application. By using migrations in Lithe, you gain full control over these changes, facilitating the development and maintenance of your project.