定制 antonkomarev/php-db-migration-validator 二次开发

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

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

antonkomarev/php-db-migration-validator

Composer 安装命令:

composer require antonkomarev/php-db-migration-validator

包简介

Validate PHP database migration files

README 文档

README

php-db-migration-validator

Discord Releases License

Introduction

In modern PHP frameworks such as Symfony and Laravel, migrations usually have up and down methods. In up method of migration definition you had to write code which is called only on running migration forward and in down — the code which is called only on rolling migration back. It is standard practice to make database migrations irreversible. Migrations should be backward compatible and only go forward.

In Laravel, a missing or empty down method does not prevent rollback migration on execution of php artisan migrate:rollback CLI command. The state of the database will not change, but the migration will be removed from the registry of applied migrations, and the next execution of php artisan migrate will call the up method again. To prevent this behavior, all migrations should have down method that will throw an Exception, nothing more.

PHP DB Migration Validator checks whether all migration files meet this requirement. You can add it to the server's git hooks to prevent migration rollback, or add validation step to CI.

Installation

Pull in the package through Composer.

php composer require antonkomarev/php-db-migration-validator

Usage

Validate migrations are irreversible

Validate file

php vendor/bin/php-db-migration-validator --rule=irreversible migrations/file.php

Validate many files

php vendor/bin/php-db-migration-validator --rule=irreversible migrations/file.php migrations/file2.php

Validate many files by wildcard

php vendor/bin/php-db-migration-validator --rule=irreversible migrations/2022_*

Validate files in directory

php vendor/bin/php-db-migration-validator --rule=irreversible migrations/

Validate files in many directories

php vendor/bin/php-db-migration-validator --rule=irreversible app/migrations/ vendor/migrations/

CI automation

Automating the validation of all contributions to the repository as part of the Continuous Integration is one of the possible ways to use this tool.

GitHub Action

Create file .github/workflows/db-migration-validation.yaml in the application repository.

name: DB Migration Validation

on:
    push:

jobs:
    db-migration-validation:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v2
            - uses: shivammathur/setup-php@v2
              with:
                  php-version: 8.1
                  extensions: tokenizer
                  coverage: none
            - name: Install PHP DB Migration Validator dependency
              run: composer global require antonkomarev/php-db-migration-validator --no-interaction
            - name: Ensure all database migrations are irreversible
              run: php-db-migration-validator --rule=irreversible ./database/migrations

Command usage instructions

$ php vendor/bin/php-db-migration-validator --help
PHP DB Migration Validator
--------------------------
by Anton Komarev <anton@komarev.com>

Usage: php-db-migration-validator --rule=<rule> <path>

  The following commands are available:

    help  Shows this usage instructions.

  Options:

    --rules=<rule>   Validates the database migration(s) in the specified <path>.
                     Exits with code 1 on validation errors, 2 on other errors and 0 on success.
                     Available rules (at least one should be specified):
                     - irreversible — ensure if migration file has `down` method and this method throws an Exception.

License

Support the project

If you'd like to support the development of PHP DB Migration Validator, then please consider sponsoring me. Thanks so much!

About CyberCog

CyberCog is a Social Unity of enthusiasts. Research the best solutions in product & software development is our passion.

CyberCog

统计信息

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

GitHub 信息

  • Stars: 19
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-01-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固