rockero-cz / laravel-db-updates
This is my package laravel-db-updates
Installs: 2 885
Dependents: 0
Suggesters: 0
Security: 0
Stars: 8
Watchers: 2
Forks: 0
Open Issues: 2
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-10-09 03:04:38 UTC
README
Laravel DB Updates
In short, database updates package is used to update or fix your data in database. It works on the same principle as Laravel migrations.
Each DB update
can be run only once across environments.
Installation
Install the package via composer:
composer require rockero-cz/laravel-db-updates
Publish and run migrations with:
php artisan vendor:publish --tag="db-updates-migrations"
php artisan migrate
Generating Updates
php artisan make:update update_name
return new class { /** * Run the updates. */ public function __invoke(): void { // } };
Running Updates
php artisan db:update
Usage Examples
Below you can find some practical examples.
Until now, a post could only have one image, now it can have multiple ones though, so you need to transfer your post image to the separate images
table:
/** * Run the updates. */ public function __invoke(): void { Post::all()->each(function (Post $post) { $post->images()->create([ 'url' => $post->image, 'title' => $post->title ]); }); }
Your production database had some testing data and you finally decided to delete them, so you need to delete all records older than 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(); }
You have made some refactoring and you also renamed a state name, so you need to rename it in the database:
/** * Run the updates. */ public function __invoke(): void { Order::query() ->where('state', 'new') ->update(['state' => OrderState::CREATED]); }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
License
The MIT License (MIT). Please see License File for more information.