kulizh/migratool

简单的PHP类,用于执行数据库迁移

1.0.1 2023-12-04 13:10 UTC

This package is auto-updated.

Last update: 2024-09-04 14:50:38 UTC


README

通过Composer安装

composer require kulizh/migratool

工作原理

库的逻辑基于获取迁移目录(.sql 文件)的内容。

接收到列表后,脚本尝试执行文件中指定的SQL命令。结果将放置在库目录中的JSON文件 result.json 中。如果执行成功,SQL脚本将不再执行。

注意:对于每个文件,Migrator都会启动自己的事务。如果您需要执行敏感命令,请将它们放在一个文件中。

用法:创建PHP运行器

示例代码在上文。您可以在手动启动或部署开始时启动此类运行器。

require_once './vendor/autoload.php';

use Migratool\Migrator;

// Define migrations dir
// default is <./migration>
$migrations_dir = '/var/www/migrations/';

try {
    // Create instance of Migratool
    $migrator = new Migrator($pdo, $migrations_dir);

    // Start script
    $migrator->run();

    // Echo result, you can also save it to file
    // execution result is available via method ->result().
    // If you pass Migratool\Result::RETURN_ERR_ONLY to the method
    // it returns only failed transactions

    echo "Result:\n";
    foreach($migrator->result() as $file => $result)
    {
        echo "\t$file: $result\n";
    }
} catch(\Exception $exception)
{
    die($exception->getMessage() . "\n");
}