dweik/laravel-database-patching 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

dweik/laravel-database-patching

最新稳定版本:1.0.0

Composer 安装命令:

composer require dweik/laravel-database-patching

包简介

Laravel database patching

README 文档

README

Managing DML (Data Manipulation Language) statements during code deployment can be challenging, especially if included in database migrations designed for DDL (Data Definition Language) operations. This package provides a solution to handle DML operations separately, ensuring smooth and error-free deployments.

Why Use Laravel SQL-Patch?

Incorporating DML statements into migrations can lead to:

  • Re-run Errors: DML statements might fail if the data already exists or conditions are not met when re-running migrations in a new setup.
  • Separation of Concerns: Keeping schema changes (DDL) and data changes (DML) separate enhances clarity and maintainability of your codebase.

Features

  • Seamless DML Management: Easily create and manage DML patches without interfering with migration files.
  • Idempotent Patches: Ensure DML operations can be safely executed multiple times.
  • Structured Deployment: Maintain a clear separation between schema changes and data changes.

Installation

Install the package using Composer:

composer require dweik/laravel-database-patching

Creating a New Patch File

To create a new patch file, use the following Artisan command. The patch file will be located in database/patches/...:

php artisan sql-patch:make SomeClassName

Add your DML code under the `handler` method in the generated patch file.

Running Patch Files

To run the new patch files, use the following command:

php artisan sql-patch

Example

Here's an example of how to create and use a patch file:

  1. Create a Patch File:
php artisan sql-patch:make UpdateUserDefaults
  1. Edit the Patch File:
// database/patches/2024_08_04_104717_UpdateUserDefaults.php
use LaravelDatabasePatching\Interfaces\SQLPatchInterface;
use \Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

class UpdateUserDefaults implements SQLPatchInterface
{
    public function handler()
    {
        // Ensure the column exists before updating
        if (Schema::hasColumn('users', 'default_column')) {
            DB::table('users')->whereNull('default_column')->update(['default_column' => 'default_value']);
        }
    }
}
  1. Run the Patch Files:
php artisan sql-patch

Best Practices

  • Idempotency: Ensure all DML operations are idempotent to avoid issues when scripts are executed multiple times.
  • Testing: Test your patches in a staging environment before deploying to production.
  • Version Control: Keep your patch files under version control to track changes and collaborate effectively.

Conclusion

The Laravel SQL-Patch package simplifies the management of DML operations during production deployments, ensuring they are handled separately from migrations. This approach minimizes errors and maintains a clean and organized codebase.

Feel free to adjust further as needed for your specific package details and requirements.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-04