定制 eniams/safe-migrations 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

eniams/safe-migrations

Composer 安装命令:

composer create-project eniams/safe-migrations

包简介

Warn you when a migration is unsafe

README 文档

README

⚠️ 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

image

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

Getting started

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

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

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

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
# config/services.yaml
services:
    _defaults:
        autoconfigure: true
<?php
namespace App\Statement\MyStatement;
use Eniams\SafeMigrationsBundle\Statement\StatementInterface;

class MyStatement implements StatementInterface
{
    protected string $migrationWarning;

    public function migrationWarning(): string
    {
        // The message that will be added in the migration file
        return $this->migrationWarning;
    }
    
    public function supports(string $migrationUpContent): bool
    {
        // The logic to determine if the statement is present in the `up` method of migration file.
        // The following code can be enough
        return str_contains(strtoupper($statement), $this->getStatement());
    }

    public function getStatement(): string;
    {
        return 'MY_STATEMENT';
    }
}
Configure critical tables

If you want to flag a table as critical and be warned when a migration contains changes on it, just flag the tables like this:

# config/packages/safe-migrations.yaml
    safe_migrations:
      critical_tables: # List of critical tables
        - 'user'
        - 'product'
        - # ...
Decorate a statement

If you want to wrap a statement to personalize the warning message or the logic to catch the statement you can use the decorator design pattern.

See the example bellow, you can also check how to decorate a service with Symfony.

<?php
namespace App\Statement;

use Eniams\SafeMigrationsBundle\Statement\AbstractStatement;

class CustomNotNullStatement extends AbstractStatement
{
    public function getStatement(): string
    {
        return 'NOT NULL';
    }

    public function migrationWarning(): string
    {
        return 'Your custom message';
    }
}
# config/services.yaml
    App\Warning\CustomNotNullStatement:
      decorates: 'eniams_safe_migrations.not_null.statement'
Event Listener

When an unsafe migration is created, an event Eniams\SafeMigrationsBundle\Event\UnsafeMigrationEvent is dispatched, you can listen it and retrieve a UnsafeMigration with the migration name and the content of the migration file.

Example:

<?php

namespace App\Listener;

use Eniams\SafeMigrationsBundle\Event\UnsafeMigrationEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class MigrationRiskySubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            UnsafeMigrationEvent::class => 'onUnsafeMigration',
        ];
    }

    public function onUnsafeMigration(UnsafeMigrationEvent $event)
    {
        $unsafeMigration = $event->getUnsafeMigration();

        // Version20231030215756
        $unsafeMigration->getMigrationName();

        // Migration file
        $unsafeMigration->getMigrationFileContent();

        // Migration file with the warning.
        $unsafeMigration->getMigrationFileContentWithWarning();
    }
}
Debug the configuration

You can debug the configuration you set with the following command: $ bin/console eniams:debug-configuration

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.

# Install dev dependencies
$ composer install

# Running tests and quality tools locally
$ make all

Authors

  • Smaïne Milianni - ismail1432 - <smaine(dot)milianni@gmail(dot)com>
  • Quentin Dequippe - qdequippe - <quentin@dequippe(dot)tech>

eniams/safe-migrations 适用场景与选型建议

eniams/safe-migrations 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 238.07k 次下载、GitHub Stars 达 56, 最近一次更新时间为 2023 年 05 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 eniams/safe-migrations 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 eniams/safe-migrations 我们能提供哪些服务?
定制开发 / 二次开发

基于 eniams/safe-migrations 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 238.07k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 56
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 56
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: proprietary
  • 更新时间: 2023-05-06