> For the complete documentation index, see [llms.txt](https://quantumphp.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://quantumphp.gitbook.io/docs/advanced-features/migration.md).

# Migration

## Overview

Database migrations provide a way to safely and consistently manage schema changes. Quantum's migration system supports creating, applying, reverting, and tracking migrations via CLI and programmatic APIs.

## Migration Concepts

* Migrations are PHP classes that contain `up()` and `down()` methods which apply and revert schema changes.
* Migrations are stored in migration files located in the `/migrations` directory.
* A migrations table tracks which migrations have been applied to the database.

## Migration Actions

Supported actions include:

* create
* alter
* rename
* drop

Each action corresponds to templates used in generated migration files.

### Example Create Migration

```php
use Quantum\Database\Factories\TableFactory;
use Quantum\Migration\QtMigration;

class CreateTableUsers extends QtMigration
{
    public function up(?TableFactory $tableFactory): void
    {
        $table = $tableFactory->create('users');
        $table->addColumn('id', 'integer')->primary()->autoIncrement();
        $table->addColumn('uuid', 'char', 36);
        $table->addColumn('firstname', 'varchar', 255);
        $table->addColumn('lastname', 'varchar', 255);
        $table->addColumn('role', 'varchar', 255);
        $table->addColumn('email', 'varchar', 255);
        $table->addColumn('password', 'varchar', 255);
        $table->addColumn('activation_token', 'varchar', 255)->nullable();
        $table->addColumn('remember_token', 'varchar', 255)->nullable();
        $table->addColumn('access_token', 'varchar', 255)->nullable();
        $table->addColumn('refresh_token', 'varchar', 255)->nullable();
        $table->addColumn('reset_token', 'varchar', 255)->nullable();
        $table->addColumn('otp', 'integer')->nullable();
        $table->addColumn('otp_token', 'varchar', 255)->nullable();
        $table->addColumn('otp_expires', 'timestamp')->nullable();
    }

    public function down(?TableFactory $tableFactory): void
    {
        $tableFactory->drop('users');
    }
}
```

## Generating a Migration

You can generate a migration file using the CLI:

```bash
php qt migration:generate <action> <table_name>
```

Example:

```bash
php qt migration:generate create users
```

This creates a migration file with an up/down template for the specified action.

## Applying Migrations

Apply pending migrations with:

```bash
php qt migration:migrate
```

To downgrade migrations:

```bash
php qt migration:migrate down --step=1
```

You can increase `--step` to revert more migrations.

## MigrationManager Responsibilities

* Loads migration files and determines which migrations need applying or rolling back.
* Checks for the presence of migration tracking table, and creates it if missing.
* Executes migration classes' `up()` or `down()` methods to apply or revert changes.
* Updates the migrations table with applied and removed migration entries.
* Supports migration ordering and stepping through rollback.

## Best Practices

* Always use migrations for schema changes to keep environments synced.
* Review generated migration templates before applying.
* Use migration rollback during development cautiously.
* Keep migration files under version control.

## What to read next

After migration basics, continue with:

* [Database](/docs/advanced-features/database.md)
* [Models](/docs/core-concepts/models.md)
* [Services](/docs/core-concepts/services.md)
* [CLI Console Commands](/docs/cli/console-commands.md#migrations)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://quantumphp.gitbook.io/docs/advanced-features/migration.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
