|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Database\Schema\Blueprint; |
| 4 | +use Illuminate\Support\Facades\Schema; |
| 5 | +use Laravel\Pulse\Support\PulseMigration; |
| 6 | + |
| 7 | +return new class extends PulseMigration |
| 8 | +{ |
| 9 | + /** |
| 10 | + * Run the migrations. |
| 11 | + */ |
| 12 | + public function up(): void |
| 13 | + { |
| 14 | + if (! $this->shouldRun()) { |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + Schema::create('pulse_values', function (Blueprint $table) { |
| 19 | + $table->id(); |
| 20 | + $table->unsignedInteger('timestamp'); |
| 21 | + $table->string('type'); |
| 22 | + $table->mediumText('key'); |
| 23 | + match ($this->driver()) { |
| 24 | + 'mysql' => $table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))'), |
| 25 | + 'pgsql' => $table->uuid('key_hash')->storedAs('md5("key")::uuid'), |
| 26 | + 'sqlite' => $table->string('key_hash'), |
| 27 | + }; |
| 28 | + $table->mediumText('value'); |
| 29 | + |
| 30 | + $table->index('timestamp'); // For trimming... |
| 31 | + $table->index('type'); // For fast lookups and purging... |
| 32 | + $table->unique(['type', 'key_hash']); // For data integrity and upserts... |
| 33 | + }); |
| 34 | + |
| 35 | + Schema::create('pulse_entries', function (Blueprint $table) { |
| 36 | + $table->id(); |
| 37 | + $table->unsignedInteger('timestamp'); |
| 38 | + $table->string('type'); |
| 39 | + $table->mediumText('key'); |
| 40 | + match ($this->driver()) { |
| 41 | + 'mysql' => $table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))'), |
| 42 | + 'pgsql' => $table->uuid('key_hash')->storedAs('md5("key")::uuid'), |
| 43 | + 'sqlite' => $table->string('key_hash'), |
| 44 | + }; |
| 45 | + $table->bigInteger('value')->nullable(); |
| 46 | + |
| 47 | + $table->index('timestamp'); // For trimming... |
| 48 | + $table->index('type'); // For purging... |
| 49 | + $table->index('key_hash'); // For mapping... |
| 50 | + $table->index(['timestamp', 'type', 'key_hash', 'value']); // For aggregate queries... |
| 51 | + }); |
| 52 | + |
| 53 | + Schema::create('pulse_aggregates', function (Blueprint $table) { |
| 54 | + $table->id(); |
| 55 | + $table->unsignedInteger('bucket'); |
| 56 | + $table->unsignedMediumInteger('period'); |
| 57 | + $table->string('type'); |
| 58 | + $table->mediumText('key'); |
| 59 | + match ($this->driver()) { |
| 60 | + 'mysql' => $table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))'), |
| 61 | + 'pgsql' => $table->uuid('key_hash')->storedAs('md5("key")::uuid'), |
| 62 | + 'sqlite' => $table->string('key_hash'), |
| 63 | + }; |
| 64 | + $table->string('aggregate'); |
| 65 | + $table->decimal('value', 20, 2); |
| 66 | + $table->unsignedInteger('count')->nullable(); |
| 67 | + |
| 68 | + $table->unique(['bucket', 'period', 'type', 'aggregate', 'key_hash']); // Force "on duplicate update"... |
| 69 | + $table->index(['period', 'bucket']); // For trimming... |
| 70 | + $table->index('type'); // For purging... |
| 71 | + $table->index(['period', 'type', 'aggregate', 'bucket']); // For aggregate queries... |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Reverse the migrations. |
| 77 | + */ |
| 78 | + public function down(): void |
| 79 | + { |
| 80 | + Schema::dropIfExists('pulse_values'); |
| 81 | + Schema::dropIfExists('pulse_entries'); |
| 82 | + Schema::dropIfExists('pulse_aggregates'); |
| 83 | + } |
| 84 | +}; |
0 commit comments