psylogic / laravel-sqlite-handler
处理测试中的迁移限制
dev-master
2020-05-19 15:18 UTC
Requires
- php: >=7.1.3
Requires (Dev)
- doctrine/dbal: ^2.6
- orchestra/testbench: 5.x-dev
This package is auto-updated.
Last update: 2024-09-20 01:13:55 UTC
README
处理 Laravel SQLite 限制
适用于 Laravel 5.6, 5.7, 5.8, 6.x, 7.x
composer require psylogic/laravel-sqlite-handler
基于 PR laravel/framework#32737
有时我们处理现有的项目并决定添加单元测试,问题是当我们有数百个迁移时,其中一些迁移会删除列并为现有表添加新列,甚至删除外键,由于我们总是喜欢在内存或本地文件上使用 SQLite,因此维护变得困难。提供的解决方案是在迁移文件中添加更多测试,例如
if (\DB::getDriverName() != 'sqlite') { $table->string('column'); }else{ $table->string('column')->nullable(); }
if (\DB::getDriverName() != 'sqlite') { $table->dropColumn('column1'); $table->dropColumn('column2'); }else{ $table->dropColumn(['column1', 'column2']); }
或者如果没有条件,你可以更新你的代码为
$table->dropColumn(['column1', 'column2']);
默认情况下,在 Laravel 中对所有驱动程序都是可接受的
if (\DB::getDriverName() != 'sqlite') { $table->dropForeign('name'); }
或者创建 单独的迁移 或修改所有受影响的迁移,通过重构代码来适应 SQLite 的限制。
现在你不需要担心在迁移文件中添加条件或创建单独的迁移
覆盖内容
- 在一个迁移文件中删除列(修复:SQLite 不支持多次调用 dropColumn)
- 为现有表上的新列添加 Nullable(修复:不能添加 NOT NULL 列,默认值为 NULL)
- 删除外键(修复:SQLite 不支持删除外键)