orottier / laravel-migration-squasher
将增量Laravel迁移文件聚合为每个表的单一迁移。这消除了所有修改列,使得通过sqlite进行测试成为可能。
Requires
- laravel/framework: ^5.1
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2024-09-18 19:00:45 UTC
README
将增量Laravel迁移文件聚合为每个表的单一迁移。在需要将大量迁移集压缩为每个表的单一迁移时,这可能是有益的。
此包还消除了所有修改列,因为所有迁移都聚合到一个文件中,这使得通过sqlite进行测试成为可能。
要安装,只需要求这个分叉的包
"orottier/laravel-migration-squasher": "~0.1"
然后,将服务提供者添加到您的 config/app.php 中以启用 artisan 功能
Cytracom\Squasher\SquasherServiceProvider::class
注意:如果您不想使用命令行界面,则这不是必需的。如果您只想使用squasher进行测试,则可以忽略此服务提供者,并直接调用squasher。这样,squasher可以放在require-dev中,而不是作为生产栈的一部分。
命令行使用
php artisan migrate:squash [-p|--path[="..."]] [-o|--output[="..."]] [-mv|--move-to[="..."]]
Options:
--path (-p) The path to the migrations folder (default: "database/migrations")
--output (-o) The path to the output folder of squashes (default: "tests/migrations")
--move-to (-mv) The path where old migrations will be moved. (default: "database/migrations")
在php中的使用
$squasher = new \Cytracom\Squasher\MigrationSquasher($pathToMigrations, $outputForSquashedMigrations [, $moveOldToThisPath = null]); $squasher->squash();
squasher目前不支持复合键或索引。如果您发现我遗漏了其他任何内容,请提出问题!或者,更好的是,尝试将其集成!
迁移squasher将取几个迁移并创建一个单一、最终的迁移,该迁移反映了所有迁移运行后数据库模式应该是什么。
请注意,squasher是为测试制作的,而不是用于增量数据库更改。使用squasher将丢弃代码中与迁移无关的所有功能。目标是删除所有修改列,以启用sqlite测试。
表squasher可以处理简单的迁移语句,以正常方式编写。如下所示
Schema::create('my_table', function (Blueprint $table) { $table->integer("my_int",10)->unsigned()->unique(); $table->increments("id"); $table->string("test",255); $table->myEnum("oldArrayInit", array("val1","val2")); $table->myEnum("newArrayInit", ["val1","val2"]); DB::update('ALTER TABLE `my_table` MODIFY COLUMN `test` blob(500);'); //etc; });
这也适用于删除和修改模式。有关它可以处理的内容的更详细视图,请查看src/tests/data/MigrationTestData.php中的示例测试数据。
表squasher将不会处理如下内容
$myStringColumns = ["col1","col2","col3"]; foreach($myStringColumns as $column){ $table->string($column); }
并且永远不会。迁移不应这样编写,用php编写php解析器也不是一件小任务。
以下是如何使用此工具进行测试
在设置测试用例时,我们运行
recursiveDelete(base_path('tests/migrations')); $squash = new \Cytracom\Squasher\MigrationSquasher("database/migrations", "tests/migrations"); $squash->squash(); \Artisan::call('migrate', ['--path' => 'tests/migrations']); /** * Delete a file or recursively delete a directory * * @param string $str Path to file or directory * @return bool */ function recursiveDelete($str){ if(is_file($str)){ return @unlink($str); } elseif(is_dir($str)){ $scan = glob(rtrim($str,'/').'/*'); foreach($scan as $index=>$path){ recursiveDelete($path); } return @rmdir($str); } }
我们在压缩之前删除所有迁移,以消除可能存在的旧压缩迁移。
再次提醒,如果您发现任何问题,请提出问题,并且请随意提交拉取请求以供审查!我们的目标是使使用sqlite进行测试变得更有可能,以实现快速测试。社区的帮助始终受到欢迎。