Download the PHP package shipmonk/doctrine-two-phase-migrations without Composer
On this page you can find all versions of the php package shipmonk/doctrine-two-phase-migrations. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download shipmonk/doctrine-two-phase-migrations
More information about shipmonk/doctrine-two-phase-migrations
Files in shipmonk/doctrine-two-phase-migrations
Package doctrine-two-phase-migrations
Short Description Two phase migrations for Doctrine ORM: before and after deploying new codebase version
License MIT
Informations about the package doctrine-two-phase-migrations
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.
Installation:
Configuration in symfony application:
If your Doctrine\ORM\EntityManagerInterface
is autowired, just register few services in your DIC and tag the commands:
Commands:
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.
Generating new migration:
You can generate migration from database <=> 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.
The generated file then looks like this:
You can adjust it by providing custom $templateFilePath
to MigrationConfig
, but it needs to implement Migration
interface.
Status verification:
You can check awaiting migrations and entity sync status:
Skipping all migrations:
You can also mark all migrations as already executed, e.g. when you just created fresh schema from entities. This will mark all not executed migrations in all stages as migrated.
Executing migration:
Execution is performed without any interaction and does not fail nor warn when no migration is present for execution.
When executing all the migrations (e.g. in test environment) you probably want to achieve one-by-one execution. You can do that by:
Advanced usage
Run custom code for each executed query:
You can hook into migration execution by implementing MigrationExecutor
interface and registering your implementations as a service.
Implement executeQuery()
to run checks or other code before/after each query.
Interface of this method mimics interface of Doctrine\DBAL\Connection::executeQuery()
.
Run all queries within transaction:
You can change your template (or a single migration) to extend; TransactionalMigration
.
That causes each phases to be executed within migration.
Be aware that many databases (like MySQL) does not support transaction over DDL operations (ALTER and such).
Checking execution duration:
Migration table has started_at
and finished_at
columns with datetime data with microseconds.
But those columns are declared as VARCHARs by default, because there is no microsecond support in doctrine/dbal yet.
That may complicate datetime manipulations (like duration calculation).
You can adjust the structure to your needs (e.g. use DATETIME(6)
for MySQL) manually in some migration.
Differences from doctrine/migrations
The underlying diff checking and generation is equal to what happens in doctrine/migrations as it uses doctrine/dbal features. Main difference is that we do not provide any downgrade phases.
This library is aiming to provide only core functionality needed for safe migrations within rolling-update deployments.
Basically all the logic is inside MigrationService
, which has only ~300 lines.
We try to keep it as lightweight as possible, we do not plan to copy features from doctrine/migrations.
All versions of doctrine-two-phase-migrations with dependencies
doctrine/dbal Version ^3.6.0 || ^4.0.0
doctrine/orm Version ^3.0.0
symfony/console Version ^5.4.0 || ^6.0.0 || ^7.0.0