cubadevops/dbmigrator

简单的PHP数据库版本控制(迁移系统)

1.0.1 2022-10-19 09:40 UTC

This package is auto-updated.

Last update: 2024-09-19 14:08:19 UTC


README

数据库简单控制版本系统

如何在项目或项目内的单个模块/插件目录中集成

首先需要require autoload.php

require_once './vendor/autoload.php';

然后配置数据库连接

$connection = new Connection('localhost', '3306', 'root', 'root', 'test', 'mysql');

配置迁移路径和迁移版本表名

$configurator = new Configurator($connection, __DIR__.'/migrations_dir', 'migrations_version_table');

最后创建迁移管理器

$migrator = new Migrator($configurator);

如何使用

首先你必须创建一个迁移类,该类继承自Migration或者如果你在“up”和“down”方法中使用简单的平面查询,则只需实现Migration接口

use CubaDevOps\DbMigrator\domain\Migration;
use Illuminate\Database\Capsule\Manager;
use Illuminate\Database\Schema\Blueprint;

class MigrationTest extends Migration
{

    public function up(): void
    {
        Manager::schema()->create('migration_example_table',function (Blueprint $table){
            $table->increments('id');
            $table->string('content')->default('test');
            // others columns
        });
    }

    public function down(): void
    {
        Manager::schema()->dropIfExists('migration_example_table');
    }
}

如何运行挂起的迁移

$migrator->migrate();

如何回滚迁移

$migrator->rollback();

如何运行整个迁移的全新版本

$migrator->fresh();

这等同于执行

$migrator->rollback();然后$migrator->migrate();

欢迎你通过创建新的功能或修复错误来为这个项目做出贡献,只需在新的分支上创建一个“pull request”。