Download the PHP package yousign/safe-migrations without Composer
On this page you can find all versions of the php package yousign/safe-migrations. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package safe-migrations
🦺 safe-migrations
Make your migrations safe
🪄 Features
- PG 13+
- PHP 8.1
- Doctrine Migration
🤷 Why?
Because SQL migrations can execute heavy queries on database which can slow down your application.
⚙️ Config
*For a Symfony > 6.x
Install in your project
Declare the Middleware in your services.yaml
Create a migration template migration.php.tpl
Set this template as default for your migrations in doctrine_migrations.yaml
Enable the retry on lock through the env var in you .env<.environment>
That's it ☕
▶️ Usage
Migration class
When you generate a new migration, this one extend the Migration
class of the library which expose the following safe methods.
Each of these methods will generate the right set of SQL requests to make the requested query safe.
Create table
Add foreign key
Rename constraint
Comment on column
Add index
> Note: Adding an index on a table will execute an "analyze" on all columns of the table to update statisticsDrop index
Rename index
Add column
Drop column
Set default on column
Drop default on column
Set column nullable
Set column not nullable
Migration execution
If there is a lock while executing a Doctrine migration, the migration will throw a DriverException
(Doctrine\DBAL\Driver\Exception).
If the SQLSTATE value is 55P03 (_lock_notavailable), the query will be retried up to 3 times with a 10s interval before throwing the thrown exception if it does not succeed.
You will get the following output:
Failed after the 3 retry
Succeed after 1 retry
📋 FAQ
Does it work with Migration bundle ?
Yes, of course. There is no incompatibility between this library and the [doctrine/doctrine-migrations-bundle](https://symfony.com/bundles/DoctrineMigrationsBundle/current/index.html).🔗 References
- PostgreSQL doc: Multiversion Concurrency Control
-
PostgreSQL doc: lock behavior: must read, it will help you understand a lot of things about PG locks.
For example, some DDL queries require an exclusive lock on a table. When the table is concurrently accessed and modified by other processes, acquiring the lock may take a while. The lock request is waiting in a queue, and it may also block other queries on this table once it has been enqueued.
-
Examples of other safe migration frameworks:
-
Great article to introduce to ZDD
Les Patterns des Géants du Web – Zero Downtime Deployment - OCTO Talks !
-
Very good resources to know how to schema changes while keeping backward compatibility
-
GitLab explains to contributors how they manage ZDD with their database
https://docs.gitlab.com/ee/development/what_requires_downtime.html
-
Article that explain best practices for zero downtime schema changes and many tips
PostgreSQL at Scale: Database Schema Changes Without Downtime
-
Very good video that explains a lot of things about ZDD with Database schema changes:
-
Another good video on the subject but in English this time:
PHP UK Conference 2018 - Michiel Rook - Database Schema Migrations with Zero Downtime
- Doctolib explains how they manage their migration without downtime, we basically implemented the same thing.
🤝 Contributing
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
After writing your fix/feature, you can run following commands to make sure that everything is still ok.
Authors
All versions of safe-migrations with dependencies
doctrine/dbal Version ^3.7
doctrine/migrations Version ^3.3
symfony/string Version ^6.4|^7.0