PHPackages                             eniams/safe-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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. eniams/safe-migrations

ActiveProject[Validation &amp; Sanitization](/categories/validation)

eniams/safe-migrations
======================

Warn you when a migration is unsafe

1.2.0(2y ago)56237.5k↓25%1[1 issues](https://github.com/ismail1432/safe-migrations-bundle/issues)proprietaryPHPPHP ^8.1.0

Since May 6Pushed 2y ago3 watchersCompare

[ Source](https://github.com/ismail1432/safe-migrations-bundle)[ Packagist](https://packagist.org/packages/eniams/safe-migrations)[ RSS](/packages/eniams-safe-migrations/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (10)Versions (8)Used By (0)

Safe Migrations Bundle
======================

[](#safe-migrations-bundle)

---

### ⚠️ Warn you when your **auto generated** doctrine migrations contains unsafe SQL statements.

[](#️-warn-you-when-your-auto-generated-doctrine-migrations-contains-unsafe-sql-statements)

An unsafe migration is:

- An operation that have to be done carefully if you are doing zero downtime deployments.
- An operation on a critical table defined by yourself.
- An operation that can lock table such like `NOT NULL CONSTRAINT` or loss data such like remove or truncate.
- An operation that can be dangerous such like `DROP` or `RENAME`.
- An operation defined by yourself.

When an unsafe migration is detected, a warning is displayed in the command `doctrine:migrations:diff` and a comment is added into the migration file.

[![image](https://user-images.githubusercontent.com/13260307/237054338-1b1412e3-6f24-4e05-b929-15c30ab0a736.png)](https://user-images.githubusercontent.com/13260307/237054338-1b1412e3-6f24-4e05-b929-15c30ab0a736.png)

[![image](https://user-images.githubusercontent.com/13260307/237054375-44fe19b1-9915-4841-b366-9ac83d76e360.png)](https://user-images.githubusercontent.com/13260307/237054375-44fe19b1-9915-4841-b366-9ac83d76e360.png)

### Unsafe statements list

[](#unsafe-statements-list)

- CREATE INDEX
- DROP
- MODIFY
- NOT NULL
- RENAME
- TRUNCATE

Any of these statement present in your last migration will trigger a warning, feel free to submit a PR to add more statements.

### Features

[](#features)

- [You can exclude a statement](#exclude-a-statement)
- [You can add your own statements](#create-your-own-statement)
- [You can flag a table as critical to be warned when a migration contains changes on these tables](#configure-critical-tables)
- [You decorate a statement to personalize the warning message](#decorate-a-statement)

Getting started
---------------

[](#getting-started)

### Installation

[](#installation)

You can easily install Safe Migrations Bundle by composer

```
$ composer require eniams/safe-migrations --dev

```

Then, bundle should be registered. Just verify that `config\bundles.php` is containing :

```
Eniams\SafeMigrationsBundle\SafeMigrationsBundle::class => ['dev' => true],
```

### Configuration

[](#configuration)

Then, you should register it in the configuration (`config/packages/dev/safe_migrations.yaml`) :

```
# config/packages/safe-migrations.yaml
    safe_migrations:
      # required
      migrations_path: '%kernel.project_dir%/migrations'
      # optional
      critical_tables: # List of critical tables
        - 'user'
        - 'product'
        - # ...
      # optional
      excluded_statements: # List of operations that not need a warning
        - 'TRUNCATE'
        - # ...
```

##### Exclude a statement

[](#exclude-a-statement)

If you want to exclude a statement, you can do it by adding it in the configuration file.

```
# config/packages/safe-migrations.yaml
    safe_migrations:
      excluded_statements: # List of operations that not need a warning
        - 'TRUNCATE' # The statement TRUNCATE will not be flagged as unsafe
        - # ...
```

##### Create your own statement

[](#create-your-own-statement)

If you want to create a custom statement, you can do it by adding a new class that implements `Eniams\SafeMigrationsBundle\Statement\StatementInterface`.

###### Here is an example

[](#here-is-an-example)

```
# config/services.yaml
services:
    _defaults:
        autoconfigure: true
```

```
