PHPackages                             shipmonk/doctrine-two-phase-migrations - 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. shipmonk/doctrine-two-phase-migrations

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

shipmonk/doctrine-two-phase-migrations
======================================

Two phase migrations for Doctrine ORM: before and after deploying new codebase version

0.6.1(5mo ago)2438.5k—9.9%1[3 PRs](https://github.com/shipmonk-rnd/doctrine-two-phase-migrations/pulls)MITPHPPHP ^8.2CI passing

Since Feb 17Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/shipmonk-rnd/doctrine-two-phase-migrations)[ Packagist](https://packagist.org/packages/shipmonk/doctrine-two-phase-migrations)[ RSS](/packages/shipmonk-doctrine-two-phase-migrations/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (9)Dependencies (16)Versions (16)Used By (0)

Two-phase migrations for Doctrine
---------------------------------

[](#two-phase-migrations-for-doctrine)

This lightweight library allows you to perform safer Doctrine migrations during deployment in cluster-based environments like kubernetes where rolling-update takes place. Each migration has two *up* phases, no *down* phase.

- **before**
    - to be called before any traffic hits the new application version
    - typically contains ADD COLUMN etc.
- **after**
    - to be called after the deployment is done and no traffic is hitting the old application version
    - typically contains DROP COLUMN etc.

You can see [Czech talk about this library on YouTube](https://youtu.be/7OVO8itXUt0?t=3380).

### Installation:

[](#installation)

```
composer require shipmonk/doctrine-two-phase-migrations
```

### Configuration in symfony application:

[](#configuration-in-symfony-application)

If your `Doctrine\ORM\EntityManagerInterface` is autowired, just register few services in your DIC and tag the commands:

```
_instanceof:
    Symfony\Component\Console\Command\Command:
        tags:
            - console.command

services:
    ShipMonk\Doctrine\Migration\Command\MigrationInitCommand:
    ShipMonk\Doctrine\Migration\Command\MigrationRunCommand:
    ShipMonk\Doctrine\Migration\Command\MigrationSkipCommand:
    ShipMonk\Doctrine\Migration\Command\MigrationCheckCommand:
    ShipMonk\Doctrine\Migration\Command\MigrationGenerateCommand:
    ShipMonk\Doctrine\Migration\MigrationService:
    ShipMonk\Doctrine\Migration\MigrationConfig:
        $migrationsDir: "%kernel.project_dir%/migrations"

    # more optional parameters:
        $migrationClassNamespace: 'YourCompany\Migrations'
        $migrationTableName: 'doctrine_migration'
        $migrationClassPrefix: 'Migration' # will be appended with date('YmDHis') by default
        $excludedTables: ['my_tmp_table'] # migration table ($migrationTableName) is always added to excluded tables automatically
        $templateFilePath: "%kernel.project_dir%/migrations/my-template.txt" # customizable according to your coding style
        $templateIndent: "\t\t" # defaults to spaces
```

### Commands:

[](#commands)

#### Initialization:

[](#initialization)

After installation, you need to create `migration` table in your database. It is safe to run it even when the table was already initialized.

```
$ bin/console migration:init

# example output:
Creating migration table... done.
```

#### Generating new migration:

[](#generating-new-migration)

You can generate migration from database &lt;=&gt; entity diff automatically. This puts all the queries generated by Doctrine to before stage, which will NOT be correct for any destructive actions. Be sure to verify the migration and move the queries to proper stage or adjust them. When no diff is detected, empty migration class is generated.

```
$ bin/console migration:generate

# example output:
Migration version 20230217063818 was generated
```

The generated file then looks like this:

```
