8ctopus/nano-migrations 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

8ctopus/nano-migrations

最新稳定版本:2.0.2

Composer 安装命令:

composer require 8ctopus/nano-migrations

包简介

Automate database migrations

README 文档

README

packagist downloads min php version license tests code coverage badge lines of code

A tiny database migrations package

Migrations are used to manage database schema changes over time. The rationale behind migrations is to make it easier to maintain and evolve the database schema as the application evolves. This package makes it easy to deal with migrations in small projects not using a framework.

demo

  • git clone the repository

  • run composer install

  • create the migrations file touch demo/migrations.txt

  • start Docker Desktop and docker-compose up &

  • to migrate

    php demo/index.php migrate [<count:int>]

  • to rollback

    php demo/index.php rollback <count:int>

install

composer require 8ctopus/nano-migrations

You will need to extend AbstractPDOMigration class if you use php PDO. Extending the class requires implementing the up and down migration methods and the potential safety check. Refer to the demo directory example.

use Oct8pus\Migration\AbstractPDOMigration;

final class Migration extends AbstractPDOMigration
{
    protected function up1() : string
    {
        return <<<'SQL'
        CREATE TABLE user (
            email TEXT NOT NULL,
            password TEXT NOT NULL,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        )
        SQL;
    }

    protected function down1() : string
    {
        return <<<'SQL'
        DROP TABLE IF EXISTS user
        SQL;
    }

    protected function up2() : string
    {
        ...
    }

    protected function down2() : string
    {
        ...
    }

    /**
     * Safety check
     *
     * @param array $methods
     *
     * @return self
     *
     * @throws MigrationException
     */
    protected function safetyCheck(array $methods) : self
    {
        $stdin = fopen('php://stdin', 'r', false);

        if ($stdin === false) {
            throw new MigrationException('fopen');
        }

        $this->logger?->info('migrations to run:');

        foreach ($methods as $method) {
            $this->logger?->info("- {$method}");
        }

        $this->logger?->warning('Confirm action (y/n): ');
        $input = trim(fgets($stdin));

        fclose($stdin);

        if ($input === 'y') {
            return $this;
        }

        throw new MigrationException('safety check abort');
    }
}

For other database engines, extend the AbstractMigration class and implement:

  • up and down methods
  • database connection
  • database query

clean code

composer fix(-risky)

phpstan

composer phpstan

phpmd

composer phpmd

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-05-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固