PHPackages                             roslov/migration-checker - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. roslov/migration-checker

ActiveLibrary[Database &amp; ORM](/categories/database)

roslov/migration-checker
========================

Up/down database migration checker

1.0.1(2mo ago)015.3k↑37.5%[4 issues](https://github.com/roslov/migration-checker/issues)[1 PRs](https://github.com/roslov/migration-checker/pulls)2MITPHPPHP ^8.1CI passing

Since Jan 2Pushed 2mo agoCompare

[ Source](https://github.com/roslov/migration-checker)[ Packagist](https://packagist.org/packages/roslov/migration-checker)[ RSS](/packages/roslov-migration-checker/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (9)Versions (22)Used By (2)

Database Migration Checker
==========================

[](#database-migration-checker)

[![Latest Stable Version](https://camo.githubusercontent.com/01e157ec8a473d631951ecf589495c926a9d3d917f91b10782323852050986e2/68747470733a2f2f706f7365722e707567782e6f72672f726f736c6f762f6d6967726174696f6e2d636865636b65722f76)](https://packagist.org/packages/roslov/migration-checker)[![Total Downloads](https://camo.githubusercontent.com/3cc85ac559a5ccb0bd211d355032a8af2a2150803fa7b1bcafc111cd012647e4/68747470733a2f2f706f7365722e707567782e6f72672f726f736c6f762f6d6967726174696f6e2d636865636b65722f646f776e6c6f616473)](https://packagist.org/packages/roslov/migration-checker)[![License](https://camo.githubusercontent.com/4ca424631a5a9175fa32aad827dfdd6bc40073da72919dd3d86531616563008c/68747470733a2f2f706f7365722e707567782e6f72672f726f736c6f762f6d6967726174696f6e2d636865636b65722f6c6963656e7365)](https://packagist.org/packages/roslov/migration-checker)[![PHP Version Require](https://camo.githubusercontent.com/8d695dfa3ca1516117fb05ce3ee35a49d63db39c5792d4088bba6cdd057df8d1/68747470733a2f2f706f7365722e707567782e6f72672f726f736c6f762f6d6967726174696f6e2d636865636b65722f726571756972652f706870)](https://packagist.org/packages/roslov/migration-checker)

Database Migration Checker validates your database migrations end-to-end. It runs each migration up and down and verifies that the schema after rolling back is identical to the schema before the migration was applied. This helps you catch migrations that leave behind tables, columns, indexes, or other schema artifacts.

The package focuses on the core algorithm and exposes framework-agnostic contracts. You implement a few small interfaces to connect it to your ORM or framework, and it will take care of executing migrations, capturing schema state, comparing states, and printing readable diffs.

Requirements
------------

[](#requirements)

- PHP 8.1 or higher.

Supported database types and versions
-------------------------------------

[](#supported-database-types-and-versions)

- MySQL 5.5 to 9.5+
- MariaDB 10.0 to 12.1+
- PostgreSQL 11.0 to 18.1+
- SQLite 3.x

Installation
------------

[](#installation)

The package could be installed with composer:

```
composer require --dev roslov/migration-checker
```

How it works
------------

[](#how-it-works)

The checker performs the following loop for each pending migration:

1. Capture the current schema state.
2. Run the next migration up.
3. Run the same migration down.
4. Capture the schema state again.
5. Compare the two states and fail if any differences are found.
6. Re-apply the migration up so the next migration can build on it.

If a down migration does not fully revert the changes, the checker prints a unified diff to help you locate the problem.

Core concepts
-------------

[](#core-concepts)

You connect the checker to your app by implementing these contracts from namespace `\Roslov\MigrationChecker\Contract`:

- `EnvironmentInterface`prepares and cleans up the environment (ensure metadata tables exist, reset caches, etc.).
- `MigrationInterface`applies the next migration up, apply the last migration down, and indicate if more migrations exist.
- `QueryInterface` executes queries (used by schema dumpers).
- `PrinterInterface` renders schema diffs when changes are detected.
- `DatabaseDetectorInterface` *(optional)* detects database type and version.

The checker ships with SQL helpers from namespace `\Roslov\MigrationChecker\Db`:

- `SchemaStateComparer` compares two schema dumps.
- `Dumper` automatically detects the database type and dumps its schema. It uses `DatabaseDetector` to determine which schema dumper to use:
    - `MySqlDumper` dumps the schema for MySQL or MariaDB,
    - `PostgreSqlDumper` dumps the schema for PostgreSQL,
    - `SqLiteDumper` dumps the schema for SQLite.
- `SqlQuery` fetches data from SQL database via PDO connection.

General usage
-------------

[](#general-usage)

Below is an example of usage with the Symfony framework and Doctrine Migrations. The key idea is to wire the checker into your framework and run it in a safe environment (typically the test database).

Install [sebastian/diff](https://github.com/sebastianbergmann/diff) if not installed:

```
composer require --dev sebastian/diff
```

Add the changes below to your project.

```
# config/services.yaml

services:
    App\Migration\Migration:
        arguments:
            $dependencyFactory: '@doctrine.migrations.dependency_factory'
    App\Migration\Environment:
        arguments:
            $dependencyFactory: '@doctrine.migrations.dependency_factory'
```

```
# src/Command/CheckMigrationsCommand.php
