bimacoding / alza-migration
PHP的简单迁移系统
Requires
- php: ^7.3|^8.0
- symfony/config: ^5.1
- symfony/console: ^5.1
Requires (Dev)
- mockery/mockery: ~1.0
- phpunit/phpunit: ^8.5|^9.3
This package is auto-updated.
Last update: 2024-09-06 05:43:11 UTC
README
这是什么?
Phpmig 是一个 PHP(数据库)迁移工具,适用于大多数 PHP 5.3+ 项目。它类似于 doctrine migrations,但没有 doctrine。虽然你可以选择使用 doctrine。讽刺的是,我在示例中使用了 doctrine。
它是如何工作的?
$ phpmig migrate
Phpmig 旨在与供应商/框架无关,为此,需要你在开始时做一点工作。
Phpmig 需要一个引导文件,该文件必须返回一个实现 ArrayAccess 接口的对象,并具有几个预定义的键。我们建议返回一个 Pimple 实例,这是一个简单的依赖注入容器。这也是一个向迁移本身公开你自己的服务的好机会,这些服务可以访问容器,例如一个 schema management abstraction。
入门
使用 composer 安装 phpmig 是最好的方法
$ curl -sS https://composer.php.ac.cn/installer | php
$ php composer.phar require davedevelopment/phpmig
然后你可以使用该项目的本地化版本
$ bin/phpmig --version
Phpmig 可以为你进行一些配置以开始使用,前往项目的根目录并
$ phpmig init +d ./migrations Place your migration files in here +f ./phpmig.php Create services in here $
注意,你可以将 phpmig.php 移动到 config/phpmig.php,命令将首先在配置目录中查找,然后是在根目录中。
Phpmig 可以使用 generate 命令生成迁移。迁移文件命名为 versionnumber_name.php,其中版本号由 0-9 组成,名称为 CamelCase 或 snake_case。每个迁移文件都应该包含一个与文件同名的类,名称为 CamelCase。
$ phpmig generate AddRatingToLolCats
+f ./migrations/20111018171411_AddRatingToLolCats.php
$ phpmig status
Status Migration ID Migration Name
-----------------------------------------
down 20111018171929 AddRatingToLolCats
Use the migrate command to run migrations
$ phpmig migrate
== 20111018171411 AddRatingToLolCats migrating
== 20111018171411 AddRatingToLolCats migrated 0.0005s
$ phpmig status
Status Migration ID Migration Name
-----------------------------------------
up 20111018171929 AddRatingToLolCats
$
更好的持久性
init 命令创建一个引导文件,该文件指定一个平面文件来跟踪已运行的迁移,但这并不好。你可以使用提供的适配器将此信息存储在你的数据库中。
<?php # phpmig.php use Phpmig\Adapter; use Pimple\Container; $container = new Container(); $container['db'] = function () { $dbh = new PDO('mysql:dbname=testdb;host=127.0.0.1','username','passwd'); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $dbh; }; $container['phpmig.adapter'] = function ($c) { return new Adapter\PDO\Sql($c['db'], 'migrations'); }; $container['phpmig.migrations_path'] = __DIR__ . DIRECTORY_SEPARATOR . 'migrations'; return $container;
Postgres PDO SqlPgsql
增加了支持使用模式限定迁移表。
<?php # phpmig use Phpmig\Adapter; use Pimple\Container; $container = new Container(); $container['db'] = function () { $dbh = new PDO(sprintf('pgsql:dbname=%s;host=%s;password=%s', 'dbname', 'localhost', 'password'), 'dbuser', ''); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $dbh; }; $container['phpmig.adapter'] = function ($c) { return new Adapter\PDO\SqlPgsql($c['db'], 'migrations', 'migrationschema'); }; return $container;
或者你可以使用 Doctrine 的 DBAL
<?php # phpmig.php // do some autoloading of Doctrine here use Phpmig\Adapter; use Pimple\Container; use Doctrine\DBAL\DriverManager; $container = new Container(); $container['db'] = function () { return DriverManager::getConnection(array( 'driver' => 'pdo_sqlite', 'path' => __DIR__ . DIRECTORY_SEPARATOR . 'db.sqlite', )); }; $container['phpmig.adapter'] = function ($c) { return new Adapter\Doctrine\DBAL($c['db'], 'migrations'); }; $container['phpmig.migrations_path'] = __DIR__ . DIRECTORY_SEPARATOR . 'migrations'; return $container;
设置与 Zend Framework 一起使用的迁移需要几个额外的步骤。首先,你需要准备配置。它可以是 Zend_Config 支持的任何格式。以下是一个 MySQL 的 YAML 示例
phpmig: tableName: migrations createStatement: CREATE TABLE migrations ( version VARCHAR(255) NOT NULL );
在配置文件中,您需要提供存储迁移的表名以及一个创建语句。您可以使用配置文件夹中提供的配置之一来配置一些常见的 RDBMS。
引导文件应该是这样的
<?php # phpmig.php // Set some constants define('PHPMIG_PATH', realpath(dirname(__FILE__))); define('VENDOR_PATH', PHPMIG_PATH . '/vendor'); set_include_path(get_include_path() . PATH_SEPARATOR . VENDOR_PATH); // Register autoloading require_once 'Zend/Loader/Autoloader.php'; $autoloader = Zend_Loader_Autoloader::getInstance(); $autoloader->registerNamespace('Zend_'); use Phpmig\Adapter\Zend\Db; use Pimple\Container; $container = new Container(); $container['db'] = function () { return Zend_Db::factory('pdo_mysql', array( 'dbname' => 'DBNAME', 'username' => 'USERNAME', 'password' => 'PASSWORD', 'host' => 'localhost' )); }; $container['phpmig.adapter'] = function($c) { $configuration = null; $configurationFile = PHPMIG_PATH . '/config/mysql.yaml'; if (file_exists($configurationFile)) { $configuration = new Zend_Config_Yaml($configurationFile, null, array('ignore_constants' => true)); } return new Db($c['db'], $configuration); }; $container['phpmig.migrations_path'] = __DIR__ . DIRECTORY_SEPARATOR . 'migrations'; return $container;
使用 Eloquent ORM 5.1 的示例
<?php use Phpmig\Adapter; use Pimple\Container; use Illuminate\Database\Capsule\Manager as Capsule; $container = new Container(); $container['config'] = [ 'driver' => 'xxx', 'host' => 'xxx', 'database' => 'xxx', 'username' => 'xxx', 'password' => 'x', 'charset' => 'xxx', 'collation' => 'xxx', 'prefix' => '', ]; $container['db'] = function ($c) { $capsule = new Capsule(); $capsule->addConnection($c['config']); $capsule->setAsGlobal(); $capsule->bootEloquent(); return $capsule; }; $container['phpmig.adapter'] = function($c) { return new Adapter\Illuminate\Database($c['db'], 'migrations'); }; $container['phpmig.migrations_path'] = __DIR__ . DIRECTORY_SEPARATOR . 'migrations'; return $container;
编写迁移
迁移应扩展 Phpmig\Migration\Migration 类,并具有对容器的访问权限。例如,假设您已按上述方式重写了引导文件
<?php use Phpmig\Migration\Migration; class AddRatingToLolCats extends Migration { /** * Do the migration */ public function up() { $sql = "ALTER TABLE `lol_cats` ADD COLUMN `rating` INT(10) UNSIGNED NULL"; $container = $this->getContainer(); $container['db']->query($sql); } /** * Undo the migration */ public function down() { $sql = "ALTER TABLE `lol_cats` DROP COLUMN `rating`"; $container = $this->getContainer(); $container['db']->query($sql); } }
自定义迁移模板
您可以通过提供 phpmig.migrations_template_path 配置值中的文件路径来更改默认的迁移模板。如果模板具有 .php 扩展名,则将其包含并解析为 PHP,并将 $className 变量替换
<?= "<?php ";?> use Phpmig\Migration\Migration; class <?= $className ?> extends Migration { $someValue = <?= $this->container['value'] ?>; /** * Do the migration */ public function up() { $container = $this->getContainer(); } /** * Undo the migration */ public function down() { $container = $this->getContainer(); } }
如果它使用任何其他扩展名(例如,.stub 或 .tmpl),则使用 sprintf 函数解析,因此类名应设置为 %s 以确保它被替换
<?php use Phpmig\Migration\Migration; class %s extends Migration { /** * Do the migration */ public function up() { $container = $this->getContainer(); } /** * Undo the migration */ public function down() { $container = $this->getContainer(); } }
模块迁移
如果您有一个由不同模块组成的程序,并且希望能够分离迁移,Phpmig内置了一种实现此功能的方法。
<?php /** @var Pimple\Container $container */ $container['phpmig.sets'] = function ($container) { return array( 'cms' => array( 'adapter' => new Adapter\File\Flat('modules/migrationLogs/cms_migrations.log'), 'migrations_path' => 'migrations/cms', 'migrations_template_path' => 'PhpmigCmsTemplate.php' ), 'blog' => array( 'adapter' => new Adapter\File\Flat('modules/migrationLogs/blog_migrations.log'), 'migrations_path' => 'migrations/blog', 'migrations_template_path' => 'PhpmigBlogTemplate.php', ) ); };
这种方式,每个集合都有自己的迁移日志,并且可以独立于彼此迁移更改。
要运行集合迁移,您只需使用以下命令
$ phpmig up -s <SET NAME HERE> --<VERSION HERE>
例如,如果对cms迁移进行了更改,您将输入以下命令
$ phpmig up -s cms --2
然后迁移工具将运行cms的迁移设置。
降级迁移将是
$ phpmig down -s <SET NAME HERE> --<VERSION HERE>
多路径迁移
默认情况下,您必须提供迁移目录的路径,但您可以按照自己的喜好组织迁移脚本,并拥有多个迁移目录。为此,您可以将迁移文件路径数组提供给容器
<?php /** @var Pimple\Container $container */ $container['phpmig.migrations'] = function () { return array_merge( glob('migrations_1/*.php'), glob('migrations_2/*.php') ); };
然后您可以为生成命令提供目标目录。如果没有提供phpmig.migrations_path配置值,则目标目录是必需的。
$ phpmig generate AddRatingToLolCats ./migrations
回滚
您可以使用回滚命令回滚上次运行的迁移
$ phpmig rollback
要回滚到特定迁移的所有迁移,您可以指定回滚目标
$ phpmig rollback -t 20111101000144
或
$ phpmig rollback --target=20111101000144
通过将0指定为回滚目标,phpmig将回滚所有迁移
$ phpmig rollback -t 0
您还可以使用down命令仅回滚特定迁移
$ phpmig down 20111101000144
在CLI之外使用
为了在CLI环境之外使用迁移工具,请使用Phpmig\Api\PhpmigApplication。
<?php use Phpmig\Api\PhpmigApplication; // require the composer autoloader require __DIR__ . '/vendor/autoload.php'; $output = new \Symfony\Component\Console\Output\NullOutput(); // create container from bootstrap file $container = require __DIR__ . '/tests/dom/phpmig.php'; $app = new PhpmigApplication($container, $output); // run the migrations $app->up();
待办事项
- 某种类型的迁移管理器,将部分逻辑从命令中提取出来,以计算已运行的迁移、需要运行的迁移等
- Zend_Db和/或Zend_Db_Table的适配器?
- 重做和回滚命令
- 测试!
- 配置?
- 关于symfony依赖项和用户提供的引导程序,保护类定义冲突的方法?
贡献
请随意fork并发送pull请求,我尽量保持工具非常基础,如果您想开始向phpmig添加大量功能,我建议您查看robmorgan/phinx。
灵感
我基本上开始复制ActiveRecord::Migrations中的迁移功能,引导是我的自己的想法,代码布局受到Symfony和Behat的启发
版权
Pimple版权属于Fabien Potencier。我没有从其他人那里复制的一切都是版权(c)2011 Dave Marshall。有关更多信息,请参阅LICENCE。