jackeratarina / laravel-uuid-database-queue
这使Laravel可以使用UUID作为作业主键,并实现适当的交互。
Requires
- php: >=7.3
- doctrine/dbal: ^3.1
- laravel/framework: ^8.0
This package is auto-updated.
Last update: 2024-09-06 03:31:36 UTC
README
使用UUID作为主键的Laravel数据库队列
问题:在使用MySQL时,如果
id
的数据类型为CHAR(36)
并且插入记录时没有设置值,则添加作业时会抛出错误。
此包提供服务提供者和必要的类,允许您使用UUID
作为主键插入记录。UUID是有序的,并使用Laravel的Str::orderedUuid()
。
安装包
要安装此包,请使用以下命令
composer require jackeratarina/laravel-uuid-database-queue
Fresh Install of Laravel
如果您有一个全新的Laravel项目,您需要使用以下Laravel命令创建jobs
表的迁移。
php artisan queue:table
接下来,打开名为create_jobs_table
的文件,并做出以下更改
Schema::create('jobs', function (Blueprint $table) {
// $table->bigIncrements('id'); // remove this line
$table->uuid('id')->primary(); // add this line
$table->string('displayName'); // add this line
$table->string('queue')->index();
$table->longText('payload');
$table->unsignedTinyInteger('attempts');
$table->unsignedInteger('reserved_at')->nullable();
$table->unsignedInteger('available_at');
$table->unsignedInteger('created_at');
});
从运行迁移
部分继续
已迁移作业表的现有项目
在添加服务提供者之前,请使用
php artisan migrate:status
检查是否可以看到迁移2021_10_24_161222_change_id_column_in_jobs_table
。如果您看到了它,请跳转到运行迁移
。
如果您已经运行了迁移并且表已经存在于您的数据库中,您需要首先注册包的服务提供者。打开您的项目中的config/app.php
文件,并将以下行添加到providers
数组中。
jackeratarina\LaravelUuidDatabaseQueue\LaravelUuidDatabaseQueueProvider::class
之后,运行迁移。
运行迁移
现在所有迁移都已设置好,请使用以下命令
php artisan migrate
设置服务提供者
最后一步是将Illuminate\Queue\QueueServiceProvider::class
替换为jackeratarina\LaravelUuidDatabaseQueue\QueueServiceProvider::class
,打开config/app.php
文件。删除或注释掉providers
数组中的Illuminate\Queue\QueueServiceProvider::class
行,并添加jackeratarina\LaravelUuidDatabaseQueue\QueueServiceProvider::class
您已经设置完毕...