update massive unit

main
Johnathan Douglas 2023-07-07 12:33:19 -03:00
parent bca2ecce12
commit 71923923c3
9 changed files with 227 additions and 1 deletions

View File

@ -0,0 +1,31 @@
<?php
namespace App\Console\Commands;
use App\Jobs\UpdateMassiveJob;
use Illuminate\Console\Command;
class UpdateMassiveCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:update-massive-command';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
UpdateMassiveJob::dispatch();
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace App\Jobs;
use App\Tasks\UpdateMassiveTask;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class UpdateMassiveJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*/
public function __construct()
{
//
}
/**
* Execute the job.
*/
public function handle(): void
{
(new UpdateMassiveTask())->handle();
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Transaction extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'value',
'date',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'date' => 'datetime',
];
}

View File

@ -0,0 +1,29 @@
<?php
namespace App\Tasks;
use App\Models\Transaction;
use Carbon\Carbon;
class UpdateMassiveTask
{
public function handle()
{
echo 'update massive unit' . PHP_EOL;
Transaction::query()
->where('id', '>', 0)
->chunkById(10, function ($transactions) {
foreach ($transactions as $transaction) {
Transaction::query()
->where('id', '=', $transaction->id)
->update([
'date' => Carbon::now(),
'value' => 1,
]);
}
});
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
*/
class TransactionFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'date' => fake()->dateTimeBetween('-30 days'),
'value' => fake()->numerify('####.##')
];
}
}

View File

@ -13,7 +13,7 @@ return new class extends Migration {
Schema::create('transactions', function (Blueprint $table) {
$table->id();
$table->decimal('value', 12, 2);
$table->timestamps('date');
$table->timestamp('date');
$table->timestamps();
});
}

View File

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('jobs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('queue')->index();
$table->longText('payload');
$table->unsignedTinyInteger('attempts');
$table->unsignedInteger('reserved_at')->nullable();
$table->unsignedInteger('available_at');
$table->unsignedInteger('created_at');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('jobs');
}
};

View File

@ -12,6 +12,7 @@ class DatabaseSeeder extends Seeder
*/
public function run(): void
{
$this->call(TransactionSeeder::class);
// \App\Models\User::factory(10)->create();
// \App\Models\User::factory()->create([

View File

@ -0,0 +1,40 @@
<?php
namespace Database\Seeders;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use App\Models\Transaction;
use Illuminate\Database\Seeder;
class TransactionSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
$count = 100;
$type = 2;
if ($type === 1) {
/**
* insert unit
* 1000 - Database\Seeders\TransactionSeeder ...... 1,443.86 ms DONE
* 10000 - Database\Seeders\TransactionSeeder ..... 14,256.24 ms DONE
*/
$transactions = Transaction::factory($count)->create();
}
if ($type === 2) {
/**
* insert multiple
* 1000 - Database\Seeders\TransactionSeeder ......... 96.11 ms DONE
* 10000 - Database\Seeders\TransactionSeeder ........ 887.48 ms DONE
*/
$transactions = Transaction::factory($count)->make();
Transaction::query()->insert($transactions->toArray());
}
}
}