Skip to content

Commit

Permalink
Revert "Fix undocumented upgrade fuckery for L11"
Browse files Browse the repository at this point in the history
This reverts commit 238d59b.
  • Loading branch information
romanzipp committed Mar 15, 2024
1 parent f36937e commit e516653
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 8 deletions.
10 changes: 4 additions & 6 deletions migrations/2018_02_05_000000_create_queue_monitor_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,29 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use romanzipp\QueueMonitor\Enums\MonitorStatus;

class CreateQueueMonitorTable extends Migration
{
public function up()
{
Schema::create(config('queue-monitor.table'), function (Blueprint $table) {
$table->increments('id');
$table->uuid('job_uuid')->nullable();

$table->string('job_id')->index();
$table->string('name')->nullable();
$table->string('queue')->nullable();

$table->unsignedInteger('status')->default(MonitorStatus::RUNNING);

$table->dateTime('queued_at')->nullable();
$table->timestamp('started_at')->nullable()->index();
$table->string('started_at_exact')->nullable();

$table->timestamp('finished_at')->nullable();
$table->string('finished_at_exact')->nullable();

$table->float('time_elapsed', 12, 6)->nullable()->index();

$table->boolean('failed')->default(false)->index();

$table->integer('attempt')->default(0);
$table->boolean('retried')->default(false);
$table->integer('progress')->nullable();

$table->longText('exception')->nullable();
Expand Down
63 changes: 63 additions & 0 deletions migrations/2023_03_01_000000_update_queue_monitor_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use romanzipp\QueueMonitor\Enums\MonitorStatus;

class UpdateQueueMonitorTable extends Migration
{
public function up()
{
Schema::table(config('queue-monitor.table'), function (Blueprint $table) {
$table->unsignedInteger('status')->default(MonitorStatus::RUNNING)->after('queue');
});

$this->upgradeColumns();

Schema::table(config('queue-monitor.table'), function (Blueprint $table) {
$table->dropColumn(['failed', 'time_elapsed']);
});
}

public function upgradeColumns()
{
DB::table(config('queue-monitor.table'))->orderBy('id')->chunk(500, function (Collection $monitors) {
/** @var array<int, array<stdClass>> $matrix */
$matrix = [
MonitorStatus::RUNNING => [],
MonitorStatus::FAILED => [],
MonitorStatus::SUCCEEDED => [],
];

foreach ($monitors as $monitor) {
/** @phpstan-ignore-next-line */
if ($monitor->failed) {
$matrix[MonitorStatus::FAILED][] = $monitor;
} elseif (null !== $monitor->finished_at) {
$matrix[MonitorStatus::SUCCEEDED][] = $monitor;
} else {
$matrix[MonitorStatus::RUNNING][] = $monitor;
}
}

foreach ($matrix as $status => $monitors) {
DB::table(config('queue-monitor.table'))
->whereIn('id', array_map(fn (stdClass $monitor) => $monitor->id, $monitors))
->update(['status' => $status]);
}
});
}

public function down()
{
Schema::table(config('queue-monitor.table'), function (Blueprint $table) {
$table->dropColumn('status');

$table->float('time_elapsed', 12, 6)->nullable()->index();
$table->boolean('failed')->default(false)->index();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddJobUuidAndRetriedToQueueMonitorTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table(config('queue-monitor.table'), function (Blueprint $table) {
$table->uuid('job_uuid')->nullable()->after('id');
$table->boolean('retried')->default(false)->after('attempt');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table(config('queue-monitor.table'), function (Blueprint $table) {
$table->dropColumn([
'job_uuid',
'retried',
]);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddQueuedAtColumnToQueueMonitorTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table(config('queue-monitor.table'), function (Blueprint $table) {
$table->dateTime('queued_at')->nullable()->after('status');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table(config('queue-monitor.table'), function (Blueprint $table) {
$table->dropColumn('queued_at');
});
}
}
17 changes: 15 additions & 2 deletions tests/TestCases/DatabaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,24 @@

abstract class DatabaseTestCase extends TestCase
{
use RefreshDatabase;
use RefreshDatabase {
refreshTestDatabase as baseRefreshTestDatabase;
refreshInMemoryDatabase as baseRefreshInMemoryDatabase;
}

protected function refreshInMemoryDatabase(): void
{
$this->createJobsTable();
$this->createFailedJobsTable();

protected function afterRefreshingDatabase(): void
$this->baseRefreshInMemoryDatabase();
}

protected function refreshTestDatabase(): void
{
$this->tryCreateJobsTables();

$this->baseRefreshTestDatabase();
}

protected function tryCreateJobsTables(): void
Expand Down

0 comments on commit e516653

Please sign in to comment.