> 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/packages/migration/usage.md).

# Usage

Most of the package is built around writing migration classes and then letting `MigrationManager` run them.

## Writing a migration

Create a class under your project's `migrations` directory and extend `Quantum\Migration\Migration`.

```php
use Quantum\Database\Enums\Type;
use Quantum\Database\Factories\TableFactory;
use Quantum\Migration\Migration;

class CreateTableUsers1710000000 extends Migration
{
    public function up(TableFactory $tableFactory): void
    {
        $table = $tableFactory->create('users');

        $table->addColumn('id', Type::INT, 11)->autoIncrement();
        $table->addColumn('email', Type::VARCHAR, 255)->unique();
        $table->addColumn('created_at', Type::TIMESTAMP)
            ->default('CURRENT_TIMESTAMP', false);
    }

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

Use `create()` when the table does not exist yet, and `get()` when you want to alter an existing table.

## Generating a scaffold

```php
use Quantum\Migration\MigrationManager;

$manager = new MigrationManager();
$name = $manager->generateMigration('users', 'create');
```

This writes a file into `base_dir()/migrations` and returns the generated migration name.

Review the generated file before running it. The scaffold is a starting point: update the method signatures to match `Migration`, then complete the table operations for your schema change.

## Applying pending migrations

```php
use Quantum\Migration\MigrationManager;

$manager = new MigrationManager();
$applied = $manager->applyMigrations(MigrationManager::UPGRADE);
```

Upgrade mode applies every pending migration file that is not already recorded in the `migrations` table.

Create a fresh `MigrationManager` for each apply cycle. The manager keeps its discovered migration list on the instance, so a new instance gives each run a clean view of the current migration state.

## Rolling migrations back

```php
use Quantum\Migration\MigrationManager;

$manager = new MigrationManager();
$rolledBack = $manager->applyMigrations(MigrationManager::DOWNGRADE, 1);
```

Passing `1` reverts the latest applied migration. Omitting the step argument makes the manager attempt to revert all recorded migrations, newest first.

## Practical guidance

* Keep one schema change per migration when possible.
* Finish generated rename and drop templates before running them.
* Make `down()` real whenever you expect to roll a migration back.
* Be careful with long sequences: the package does not wrap the full migration batch in its own transaction.
* Plan rollback strategy per migration (especially for destructive schema changes), because partial failures can leave schema and tracking records temporarily out of sync.
* Remember that this package relies on the relational `TableFactory` API, so use it with SQL drivers only.


---

# 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/packages/migration/usage.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.
