whirlwind-framework / migration-core
Whirlwind框架迁移工具界面
v0.0.3
2023-06-30 07:51 UTC
Requires
- php: >=8.0
- whirlwind-framework/framework: *
Requires (Dev)
- dg/bypass-finals: dev-master
- phpunit/phpunit: 10.2.x-dev
- squizlabs/php_codesniffer: 4.0.x-dev
- symfony/var-dumper: 6.3.x-dev
This package is not auto-updated.
Last update: 2024-09-20 13:38:57 UTC
README
实现Whirlwind框架数据库迁移的通用接口。
实现说明
- 创建
MigrationTableGatewayInterface的实现。方法queryOrCreateCollection必须在没有存在的情况下创建迁移集合。
class DummyMigrationTableGateway extends MongoTableGateway implements MigrationTableGatewayInterface { protected string $collectionName = 'migrations'; public function queryOrCreateCollection(array $conditions = [], int $limit = 0, array $order = []): array { $collection = $this->connection->listCollections(['name' => $this->collectionName]); if (!$collection) { $this->createCollection($this->collectionName); } return $this->queryAll($conditions, $order, $limit) } }
- 为
BlueprintInterface和BlueprintFactoryInterface创建你的数据库实现。所有如创建、修改、删除等操作都将在此处理。
class DummyBlueprint implements \Whirlwind\MigrationCore\BlueprintInterface { protected string $collectionName; protected Query $current; // your database query builder implementation protected array $queries = []; public function __construct(string $collectionName) { $this->collectionName = $collectionName; } public function build(ConnectionInterface $connection): void { foreach ($queries as $query) { $query->execute($connection); } } public function create(callable $callback): void { $this->current = new Query(); $callback($this); $this->queries[] = $this->current; } public function drop(): void { $this->queries[] = (new Query())->dropCollection($this->collectionName); } public function dropIfExists(): void { $this->queries[] = (new Query())->dropCollectionIfExists($this->collectionName); } public function createIfNotExists(callable $callback) { $this->create($callback); } }
- 为你的迁移工具实现创建ServiceProvider。将你的实现与接口绑定。你还需要通过添加
Config依赖来配置你的模块。例如
class MyDatabaseServiceProvider extends \League\Container\ServiceProvider\AbstractServiceProvider { public function register(): void { $container->add( \Whirlwind\MigrationCore\Config\MigrationPaths::class, fn() => new \Whirlwind\MigrationCore\Config\MigrationPaths([ new \Whirlwind\MigrationCore\Config\MigrationPath( 'path/to/your/migrations', 'Your\\Migration\\Namespace' ), new \Whirlwind\MigrationCore\Config\MigrationPath( 'path/to/your/another/migrations', 'Your\\Another\\Migration\\Namespace' ), ]) ); $container->add( \Whirlwind\MigrationCore\Config\Config::class, fn () => new \Whirlwind\MigrationCore\Config\Config( $container->get(\Whirlwind\MigrationCore\Config\MigrationPaths::class), '/path/to/your/template' // by default using template from core package ) ); } }
- 添加控制台命令路由。
/** * @var \Whirlwind\App\Console\Application $app */ $app->addCommand('migrate:create', \Whirlwind\MigrationCore\Command\Migration\CreateCommand::class); $app->addCommand('migrate:install', \Whirlwind\MigrationCore\Command\Migration\InstallCommand::class); $app->addCommand('migrate:rollback', \Whirlwind\MigrationCore\Command\Migration\RollbackCommand::class); $app->addCommand('migrate:status', \Whirlwind\MigrationCore\Command\Migration\StatusCommand::class);