tflori / breyta
数据库迁移库
v1.1.1
2023-09-24 19:27 UTC
Requires
- php: ^7.1 || ^8.0
- ext-json: *
- ext-pdo: *
- ext-tokenizer: *
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-03 22:11:26 UTC
README
Breyta 是一个数据库迁移的 库。数据库迁移有很多应用场景,但没有一个实际库(不包含任何用户界面)。
趣闻
我在尝试了 ruckusing-migrations 之后创建了此库。这是唯一一个不需要额外库提供用户界面,并且被提及在 awesome-php 的库。不幸的是,它似乎没有被积极开发和维护。我还缺少一些功能,它非常旧,没有使用 PSR-2/4 和命名空间。
此库的名称再次来源于冰岛语,意为“改变”。
概念
- 迁移是一组数据库语句
- 迁移必须在事务中运行
- 日志记录非常重要,并记录到迁移表中
- 此库不会生成代码(纯 SQL)
- 没有用户界面(仅 API)
- 生成花哨的输出并支持进度条
安装
我们只支持并建议使用 composer - 其他一切风险自负。
$ composer require tflori/breyta
用法
这可能在接下来的几天内发生变化,直到版本 1。
迁移脚本
<?php namespace Breyta\Migrations; use Breyta\AbstractMigration; class CreateAnimalsTable extends AbstractMigration { public function up(): void { $this->exec('DROP TABLE IF EXISTS animals'); $this->exec('CREATE TABLE animals ( id MEDIUMINT NOT NULL AUTO_INCREMENT, name CHAR(30) NOT NULL, PRIMARY KEY (id) )'); } public function down(): void { $this->exec('DROP TABLE IF EXISTS animals'); } }
控制结构
<?php namespace App\Cli\Commands; class MigrateCommand extends AbstractCommand // implements \Breyta\ProgressInterface { /** * Your database connection * @var PDO */ protected $db; public function handle() { $breyta = new \Breyta\Migrations($this->db, '/path/to/migrations', function($class, ...$args) { // return app()->make($class, $args); // the closure is optional. default: if ($class === \Breyta\AdapterInterface::class) { return new \Breyta\BasicAdapter(...$args); // first arg = closure $executor } return new $class(...$args); // first arg = AdapterInterface $adapter }); // register handler (optional) /** @var \Breyta\CallbackProgress $callbackProgress */ $callbackProgress = $breyta->getProgress(); $callbackProgress->onStart([$this, 'start']) ->onBeforeMigration([$this, 'beginMigration']) ->onBeforeExecution([$this, 'beforeExecution']) ->onAfterExecution([$this, 'afterExecution']) ->onAfterMigration([$this, 'finishMigration']) ->onFinish([$this, 'finish']); // alternative: implement \Breyta\ProgressInterface and register // $breyta->setProgress($this); $breyta->migrate(); } }
请参阅 参考 以获得 API 的更好概述。