whirlwind-framework / mongo-migrations
Whirlwind 框架的 MongoDB 迁移工具实现
v0.0.2
2023-06-26 11:54 UTC
Requires
- php: >=8.0
- league/container: ~4.0
- whirlwind-framework/migration-core: *
- whirlwind-framework/persistence-mongodb: dev-main
Requires (Dev)
- dg/bypass-finals: dev-master
- phpunit/phpunit: ~10.2
- symfony/var-dumper: ~6.0
This package is not auto-updated.
Last update: 2024-09-16 17:24:58 UTC
README
MongoDB 迁移工具
实现说明
- 绑定 MigrationPaths 依赖(所有迁移文件夹的路径)。
$container->add( \Whirlwind\MigrationCore\Config\MigrationPaths::class, fn (): \Whirlwind\MigrationCore\Config\MigrationPaths => new \Whirlwind\MigrationCore\Config\MigrationPaths([ new \Whirlwind\MigrationCore\Config\MigrationPath( '/path/to/migrations/folder', 'Your\\Migration\\Namespace' ) ]) )
- 如果使用自己的迁移模板文件,必须绑定
Config依赖。
$container->add( Whirlwind\MigrationCore\Config\Config::class, fn () \Whirlwind\MigrationCore\Config\Config::class => new \Whirlwind\MigrationCore\Config\Config( $container->get(\Whirlwind\MigrationCore\Config\MigrationPaths::class), '/path/to/your/migration/template' ) )
- 为控制台应用程序注册
MongoMigrationServiceProvider。
$container->addServiceProvider(new \WhirlwindFramework\MongoMigrations\MongoMigrationServiceProvider())
命令
注册 MongoMigrationServiceProvider 后,您可以通过控制台命令管理数据库迁移。
创建迁移
运行此命令以创建新的迁移文件。
php /path/to/console/executable migrate:create MyMigration
该命令需要将迁移文件名作为第一个参数。如果您想指定迁移路径,可以使用选项 --path=/my/migration/path。请注意,此路径必须在 MigrationPaths 依赖中注册。
安装迁移
该命令可以帮助您安装迁移。
php /path/to/console/executable migrate:install
您可以通过第一个参数的限制来控制系统中将要安装的迁移数量。该值必须是大于等于 0 的整数。默认情况下,将安装所有迁移。
回滚迁移
回滚命令可以帮助您取消迁移所做的更改
php /path/to/console/executable migrate:rollback
如果没有参数运行该命令,则将回滚最后一个迁移。您可以通过指定限制来指定要回滚的迁移数量。选项 --all 将帮助您回滚所有迁移。
状态
如果您想查看到目前为止已运行的迁移,可以使用 migrate:status 命令
php /path/to/console/executable migrate:status
输出将显示最近应用的 10 个迁移。您可以通过第一个参数或选项 --all 指定限制。
迁移 API
Migration 具有一系列有用的方法来管理迁移过程。
class MyMigration111 extends \Whirlwind\MigrationCore\Migration { public function up(): void { $this->createIfNotExists('orders', static function (\WhirlwindFramework\MongoMigrations\Blueprint $b) { $b->setCapped(true); $b->setMax(100); $b->setSize(500); $b->setCollation([ 'locale' => 'en', 'strength' => 1, ]); $b->setValidator([ '$jsonSchema' => [ 'bsonType' => 'object' 'required' => ['name', 'year'], 'properties' => [ 'name' => [ 'bsonType' => 'string', ], 'year' => [ 'bsonType' => 'int', 'minimum' => 1990, 'maximum' => 3017, ] ] ] ]); $b->setValidationLevel(\WhirlwindFramework\MongoMigrations\Blueprint::VALIDATION_LEVEL_STRICT); $b->setValidationLevel(\WhirlwindFramework\MongoMigrations\Blueprint::VALIDATION_ACTION_ERROR); }); $this->modify('orders', static function (\WhirlwindFramework\MongoMigrations\Blueprint $b) { $b->createIndex(['year']); $b->setOption('unique', true); $b->createIndex(['name']); }); } public function down(): void { $this->modify('orders', function (\WhirlwindFramework\MongoMigrations\Blueprint $b) { $b->dropAllIndexes(); }); $this->dropIfExists('orders'); } }
create($collection, $callback)- 用于创建新集合。在callback中,您可以指定在创建过程中使用的附加选项。createIfNotExists($collection, $callback)- 与create一样,但如果集合存在,则跳过创建modify($collection, $callback)- 用于修改集合。在callback中,您可以指定附加选项并执行额外操作,例如重命名集合或创建新索引。请注意,如果您需要为不同命令使用不同的选项,请尽可能使用modify方法。drop($collection)- 删除整个集合dropIfExists($collection)- 与delete相同,但会检查集合是否存在。
蓝图方法
create(callable)- 用于配置创建过程。setCapped(bool)- 设置capped选项。setSize(int)- 为 capped 集合设置size选项。setValidator(array)- 为集合设置validator选项。setValidationLevel(string)- 为集合设置validatorLevel选项。可能的值是Blueprint::VALIDATION_LEVEL_OFFBlueprint::VALIDATION_LEVEL_STRICT、Blueprint::VALIDATION_LEVEL_MODERATE。setValidationAction(string)- 为集合设置validationAction选项。可能的值是Blueprint::VALIDATION_ACTION_WARN、Blueprint::VALIDATION_ACTION_ERROR。setCollation(array)- 为集合设置collation选项。有关更多信息,请参阅 MongoDB 官方文档。setOption(string, mixed)- 通过其名称设置任何选项。drop()- 删除整个集合。dropIfExists()- 如果存在,则删除整个集合。createIfNotExists(callable)- 如果集合不存在,则配置创建过程。createIndex(array, ?string)- 使用name创建针对keys的新索引。dropIndex(string)- 通过名称删除索引。dropAllIndexes()- 删除所有集合索引。renameCollection(string)- 重命名集合。insert(array)- 插入新的文档。batchInsert(array)- 插入多个文档。update(array, array)- 根据条件更新文档,并使用新的数据。delete(array)- 根据条件删除文档。