2023_06_07_000001_create_pulse_tables.php 3.2 KB

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