rockero-cz / laravel-db-updates
这是我创建的包 laravel-db-updates
1.0.2
2023-06-12 18:52 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^10.0
- spatie/laravel-package-tools: ^1.13.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.1
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.0
- pestphp/pest: ^1.23
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.6
This package is auto-updated.
Last update: 2024-09-09 02:54:23 UTC
README
Laravel DB Updates
简而言之,数据库更新包用于更新或修复数据库中的数据。它与Laravel迁移的原理相同。
每个 DB 更新
只能在环境中运行一次。
安装
通过composer安装此包
composer require rockero-cz/laravel-db-updates
使用以下命令发布并运行迁移
php artisan vendor:publish --tag="db-updates-migrations"
php artisan migrate
生成更新
php artisan make:update update_name
return new class { /** * Run the updates. */ public function __invoke(): void { // } };
运行更新
php artisan db:update
使用示例
以下是一些实际示例。
到目前为止,一篇文章只能有一张图片,现在它可以有多个,因此您需要将文章图片转移到单独的 images
表中
/** * Run the updates. */ public function __invoke(): void { Post::all()->each(function (Post $post) { $post->images()->create([ 'url' => $post->image, 'title' => $post->title ]); }); }
您的生产数据库中有些测试数据,您最终决定删除它们,因此您需要删除所有早于 2023-01-01
的记录
/** * Run the updates. */ public function __invoke(): void { Post::query() ->where('created_at', '<', '2023-01-01') ->delete(); User::query() ->where('created_at', '<', '2023-01-01') ->delete(); }
您进行了一些重构,并且还重命名了一个状态名称,因此您需要在数据库中重命名它
/** * Run the updates. */ public function __invoke(): void { Order::query() ->where('state', 'new') ->update(['state' => OrderState::CREATED]); }
测试
composer test
变更日志
请参阅 变更日志 以获取有关最近更改的更多信息。
安全漏洞
请参阅 我们的安全策略 了解如何报告安全漏洞。
许可证
MIT许可证(MIT)。请参阅 许可证文件 以获取更多信息。