capsulescodes / laravel-population
轻松简化数据库迁移,并确保与数据库表的兼容性。
v1.2.0
2024-08-27 08:42 UTC
Requires
- php: ^8.2.0
- nikic/php-parser: ^5.1.0
Requires (Dev)
- orchestra/testbench: ^9.4.0
- pestphp/pest: ^2.35.1
README
轻松简化数据库迁移,并确保与数据库表的兼容性。
Laravel Population 包提供了一套命令,用于解析您的迁移并检测它们与数据库表之间的任何差异。如果发现差异,将启动向导以帮助您迁移并使用转换后的记录填充新表。
通常,您的 users
表可能有一个 fullname
列,但您需要两个独立的列:firstname
和 lastname
。然而,您的数据库已经充满了记录。
这篇文章 深入探讨了该包。
警告
我们建议在生产环境中使用此包时谨慎行事。
安装
composer require --dev capsulescodes/laravel-population
用法
假设,您的当前 users
表有一个 fullname
列,但您需要两个独立的列:firstname
和 lastname
。首先,修改您的迁移
... Schema::create( 'users', function( Blueprint $table ) { $table->id(); - $table->string( 'fullname' ); + $table->string( 'firstname' ); + $table->string( 'lastname' ); } ); ...
现在释放魔法
php artisan populate
populate 命令将显示迁移文件中的更改,并要求确认。
INFO Migration changes : create_users_table .......................................................................................................................... DONE INFO Table 'users' has changes. ⇂ delete column : 'fullname' => type : varchar ⇂ create column : 'firstname' => type : varchar ⇂ create column : 'lastname' => type : varchar ┌ Do you want to proceed on populating the 'users' table? ─────┐ │ Yes │ └──────────────────────────────────────────────────────────────┘ ┌ How would you like to convert the records for the column 'varchar' of type 'string'? 'fn( $attribute, $model ) => $attribute' ┐ │ fn( $a, $b ) => explode( ' ', $b->fullname )[ 0 ] │ └──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ ┌ How would you like to convert the records for the column 'lastname' of type 'varchar'? 'fn( $attribute, $model ) => $attribute' ┐ │ fn( $a, $b ) => explode( ' ', $b->fullname )[ 1 ] │ └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ INFO Population succeeded.
您的 users
表已更新并使用转换后的记录填充。很简单。
App\Models\User { id: 1, - fullname: "Louie Wolff", + firstname: "Louie", + lastname: "Wolff", }, App\Models\User { id: 2, - fullname: "Holly Waters", + firstname: "Holly", + lastname: "Waters", }, App\Models\User { id: 3, - fullname: "Colton Mueller", + firstname: "Colton", + lastname: "Mueller", }, ...
# The populator will ask you the formula to convert existing records 'fn( $attribute, $model ) => $attribute' # The inital representation of the parameters $attribute = 'fullname' $model = '$user' # But you can decide to use any Laravel helpers instead 'fn() => fake()->firstName()'
如果您想回滚最新的填充
php artisan populate:rollback
WARN The rollback command will only set back the latest copy of your database(s). You'll have to modify your migrations and models manually. INFO Database dump successfully reloaded.
选项
php artisan populate --path={path-to-migrations-to-populate} --realpath={true|false} --database={database-name} --daptabase={database-name}
- Laravel Population 支持 SQLite、MySQL、MariaDB 和 PostgreSQL。
- Laravel Population 可以与多个数据库一起工作。
- Laravel Population 支持匿名和命名迁移类。
- Laravel Population 支持在迁移文件中创建多个表。
贡献
欢迎提交拉取请求。对于重大更改,请首先提交一个问题以讨论您想要更改的内容。
请确保适当更新测试。为了运行 MySQL 测试,必须在相应的 TestCases 中配置凭据。