rockero-cz/laravel-db-updates

This is my package laravel-db-updates
3,523 8
Install
composer require rockero-cz/laravel-db-updates
Latest Version:1.0.2
PHP:^8.1
Laravel:10
Older releases:9
License:MIT
Last Updated:Oct 9, 2023
Links: GitHub  ·  Packagist
Maintainer: ganyicz
Banner

Laravel DB Updates

Rockero Latest Version on Packagist Total Downloads Software License

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.

Related Packages

rockero-cz/laravel-linter

This is my package laravel-linter

1,163 0
rockero-cz/ci

This is my package ci

1,100 0
rockero-cz/laravel-starter-kit

Speed up the kickoff of your Laravel projects.

9,031 32
doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

648,266,899 9,701
laravel/framework

The Laravel Framework.

604,216,237 34,982

Version History

Version Released PHP Laravel License
1.0.2 ^8.1 ^10.0 MIT
1.0.1 ^8.1 ^9.0 MIT
1.0.0 ^8.1 ^9.0 MIT