Validate PHP database migration files for compliance with best practices. Ensure db migrations are irreversible.

Overview

PHP DB Migration Validator

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 <[email protected]>

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

You might also like...
Easily add all the 58 Algerian Wilayas and its Dairas to your cool Laravel project (Migrations, Seeders and Models).
Easily add all the 58 Algerian Wilayas and its Dairas to your cool Laravel project (Migrations, Seeders and Models).

Laravel-Algereography Laravel-Algereography allows you to add Migrations, Seeders and Models of Algerian Wilayas and Dairas to your existing or new co

Laravel MiGator - Migrations Generator
Laravel MiGator - Migrations Generator

Laravel Migator A package that will allow developers to interactively generate schema migrations for a laravel application. It takes into account the

This package extends Illuminate to provide partitioned table creation in migrations.
This package extends Illuminate to provide partitioned table creation in migrations.

Laravel Partitions for Migrations This package extends Illuminate to provide partitioned table creation in migrations for PostgreSQL. Support for othe

Boilerplate code for protecting a form with proof of work. Uses javascript in the browser to generate the hashcash and PHP on the server to generate the puzzle and validate the proof of work.

Boilerplate code for protecting a form with proof of work. Uses javascript in the browser to generate the hashcash and PHP on the server to generate the puzzle and validate the proof of work.

Laravel package to generate and to validate a UUID according to the RFC 4122 standard. Only support for version 1, 3, 4 and 5 UUID are built-in.

Laravel Uuid Laravel package to generate and to validate a universally unique identifier (UUID) according to the RFC 4122 standard. Support for versio

A Laravel wrapper for spatie/dns. Allows to query and validate DNS records.

A Laravel wrapper for spatie/dns. Allows to query and validate DNS records.

A package to validate email domains in a user registration form
A package to validate email domains in a user registration form

This package allows to define a subset of allowed email domains and validate any user registration form with a custom rule.

Easily validate data attributes through a remote request
Easily validate data attributes through a remote request

Laravel Remote Rule Easily validate data attributes through a remote request. This package allows you to define a subset of custom rules to validate a

A simple laravel package to validate console commands arguments and options.

Command Validator A simple laravel package to validate console commands arguments and options. Installation Require/Install the package using composer

Comments
  • Specify multiple paths

    Specify multiple paths

    php vendor/bin/php-database-migration-validator --irreversible migrations/1.php migrations/2.php
    php vendor/bin/php-database-migration-validator --irreversible migrations/2022_*
    
    enhancement 
    opened by antonkomarev 1
  • Colorize output

    Colorize output

    private function printErrorLine(
        string $message
    ): void {
        echo $this->colorizeMessage($message, '0;31') . PHP_EOL;
    }
    
    private function colorizeMessage(
        string $message,
        string $color
    ): string {
        return sprintf("\033[%sm%s\033[0m", $color, $message);
    }
    
    enhancement 
    opened by antonkomarev 0
Releases(1.0.2)
  • 1.0.2(Jan 16, 2022)

    Fixed

    • Fixed missing trailing slash on end of directory path

    Full Changelog: https://github.com/antonkomarev/php-db-migration-validator/compare/1.0.1...1.0.2

    Source code(tar.gz)
    Source code(zip)
  • 1.0.1(Jan 16, 2022)

    Fixed

    • Fixed no migration files found error message

    Full Changelog: https://github.com/antonkomarev/php-db-migration-validator/compare/1.0.0...1.0.1

    Source code(tar.gz)
    Source code(zip)
  • 1.0.0(Jan 15, 2022)

Owner
Anton Komarev
Creator of @yhype 🚀 git analytics | Co-founder of @cybercog social unity
Anton Komarev
Migrator is a GUI migration manager for Laravel which you can create, manage and delete your migration.

Migrator Migrator is a GUI migration manager for Laravel which you can create, manage and delete your migration. Installation: To install Migrator you

Reza Amini 457 Jan 8, 2023
Laravel best practices

Translations: Nederlands (by Protoqol) Indonesia (by P0rguy, Doni Ahmad) 한국어 (by cherrypick) 日本語 (by 2bo) 简体中文 (by xiaoyi) 繁體中文 (by woeichern) ภาษาไทย

null 9k Jan 2, 2023
Ensure your Laravel applications keep a normal pulse

Ensure your Laravel applications keep a normal rhythm Laravel Defibrillator helps you ensure that aspects of your application that should be running a

Michael Dyrynda 148 Dec 20, 2022
Declare database migrations and factory definitions inside Laravel models.

Lucid This package allows you to declare database migrations and factory definitions inside of your Laravel models. Running the lucid:migrate command

null 23 Jul 9, 2022
PHP 7 Migration Assistant Report (MAR)

Introduction What is PHP 7 Migration Assistant Report(MAR)? PHP 7 MAR, or just "php7mar", is a command line utility to generate reports on existing PH

Alexia E. Smith 790 Dec 8, 2022
Run patches migration style in your Laravel applications.

This package generates patch files in the same fashion Laravel generates migrations. Each file is timestamped with an up and a down method and is asso

Anthony Rappa 44 Sep 9, 2022
Magento Data Migration Tool

Overview We're pleased you're considering moving from the world's #1 eCommerce platform—Magento 1.x—to the eCommerce platform for the future, Magento

Magento 327 Jan 4, 2023
NYC Checkbook - Drupal 9 migration

Checkbook NYC Checkbook NYC is an open source financial transparency web application. Checkbook provides transparent access to a city's or other juris

Office of the Comptroller, City of New York 1 Jan 14, 2022
Make Laravel Pivot Tables using the new Laravel 9 closure migration format

This will allow you to create pivot table migration files using the new Laravel 9 closure migration format by simply passing two models.

Jose Jimenez 16 Aug 23, 2022
Automatic Laravel model migrations.

Laravel Automatic Migrations Automatic Laravel model migrations. Instead of having to create and manage migration files, this package allows you to sp

null 38 Nov 11, 2022