定制 capsulescodes/laravel-population 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

capsulescodes/laravel-population

最新稳定版本:v3.0.5

Composer 安装命令:

composer require capsulescodes/laravel-population

包简介

Simplify database migrations and ensure consistency with your database tables effortlessly.

README 文档

README

Laravel Population

Simplify database migrations and ensure consistency with your database tables effortlessly.

Laravel Population package provides a set of commands that parses your migrations and detects any disparities between them and your database tables. If differences are found, a wizard is triggered to help you migrate and seed the new tables with converted records.


Typically, your users table might have a name column, but you need two separate columns : first_name and last_name. However, your database is already full of records.


This article provides an in-depth exploration of the package.


Warning

We recommend exercising caution when using this package on production.


Installation

composer require --dev capsulescodes/laravel-population

Usage


Let's say, your current users table have a name column, but you need two separate columns : first_name and last_name. First, modify your migration :


...
Schema::create( 'users', function( Blueprint $table )
{
    $table->id();
-    $table->string( 'name' );
+    $table->string( 'first_name' );
+    $table->string( 'last_name' );
} );
...

Now unleash the magic :

php artisan populate

The populate command will display the changes made in the migration files and ask for confirmation.

   INFO  Migration changes :

  create_users_table .......................................................................................................................... DONE

   INFO  Table 'users' has changes.

  ⇂ delete column : 'name' => type : varchar
  ⇂ create column : 'first_name' => type : varchar
  ⇂ create column : 'last_name' => type : varchar

 ┌ Do you want to proceed on populating the 'users' table? ─────┐
 │ Yes                                                          │
 └──────────────────────────────────────────────────────────────┘

 ┌ How would you like to convert the records for the column 'first_name' of type 'varchar'?  'fn( $attribute, $model ) => $attribute' ┐
 │ fn( $a, $b ) => explode( ' ', $b->name )[ 0 ]                                                                                      │
 └────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

 ┌ How would you like to convert the records for the column 'last_name' of type 'varchar'?  'fn( $attribute, $model ) => $attribute' ┐
 │ fn( $a, $b ) => explode( ' ', $b->name )[ 1 ]                                                                                     │
 └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

   INFO  Population succeeded.

Your users table has been updated and seeded with converted records. Simple.


App\Models\User
{
    id: 1,
-    name: "Louie Wolff",
+    first_name: "Louie",
+    last_name: "Wolff",
},
App\Models\User
{
    id: 2,
-    name: "Holly Waters",
+    first_name: "Holly",
+    last_name: "Waters",
},
App\Models\User
{
    id: 3,
-    name: "Colton Mueller",
+    first_name: "Colton",
+    last_name: "Mueller",
},
...


# The populator will ask you the formula to convert existing records
'fn( $attribute, $model ) => $attribute'

# The inital representation of the parameters
$attribute = 'name'
$model = '$user'

# But you can decide to use any Laravel helpers instead
'fn() => fake()->firstName()'


If you want to rollback the latest population :

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.


Options

php artisan populate --path={path-to-migrations-to-populate} --realpath={true|false} --database={database-name}

  • Laravel Population supports SQLite, MySQL, MariaDB and PostgreSQL.
  • Laravel Population can work with multiple databases.
  • Laravel Population supports both anonymous and named migrations classes.
  • Laravel Population supports multiple table creation in migration files.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate. In order to run MySQL tests, credentials have to be configured in the intended TestCases.

Credits

Capsules Codes

License

MIT

统计信息

  • 总下载量: 454
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 37
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 37
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-10-20