majpage / simple-database-migrations
简单的PHP数据库迁移
0.3.1
2024-04-30 14:36 UTC
Requires
- php: ^7.4 || ^8.0
- majpage/simple-database: ^0.4.2
- majpage/simple-structure: ^0.3.1
Requires (Dev)
- phpunit/phpunit: ^9
This package is not auto-updated.
Last update: 2024-09-17 16:16:20 UTC
README
简单的PHP数据库迁移
测试
要运行单元测试,请输入 composer run test
。
实现
要将迁移添加到现有项目中,请向依赖注入容器中添加两个类,假设 db
依赖项是数据库连接 (SimpleDatabase\Client\SqlConnectionInterface
)
use SimpleDatabaseMigrations\Command\MigrationsCommand; use SimpleDatabaseMigrations\Manager\MigrationsManager; $container = $this->getContainer(); $container ->setObject('migrationsManager', MigrationsManager::class, ['db'], [ $container->get('baseDir') . '/src/Migration', 'MyProject\\Migration', ]) ->setObject('migrationsCommand', MigrationsCommand::class, ['migrationsManager']) ;
然后创建一个包含以下内容的 bin/migrations
文件
#!/usr/bin/env php <?php use MyProject\Bootstrap; use SimpleDatabaseMigrations\Command\MigrationsCommand; $baseDir = realpath(dirname(__FILE__) . '/..'); require_once($baseDir . '/vendor/autoload.php'); $bootstrap = new Bootstrap($baseDir); /** @var MigrationsCommand $command */ $command = $bootstrap ->getContainer() ->get('migrationsCommand') ; $command->execute($argv);
命令
所有迁移命令都在应用程序运行的服务器上执行,因此在本地环境中,您可以通过Docker容器的bash访问它(例如,在Docker容器中)。以下是一个可用的命令列表
bin/migrations status
- 返回迁移状态;支持以下标志--full
- 返回迁移状态的完整信息,
bin/migrations create
- 创建一个空迁移文件以填充查询,bin/migrations migrate
- 实现迁移;支持以下标志--remove-unknown
- 删除未知迁移(数据库中有记录,但没有相应的文件),
bin/migrations migrate 1234567890
- 实现或回滚到指定的版本(例如1234567890
),bin/migrations migrate empty
- 回滚所有迁移。