Skip to content

Commit

Permalink
[1.x] Improve migration errors to guide developers with unsupported d…
Browse files Browse the repository at this point in the history
…rivers (#233)

* Improve migration errors to guide developers with unsupported drivers

* Fix code styling

* Fix

* Update PulseMigration.php

---------

Co-authored-by: timacdonald <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
3 people authored Dec 14, 2023
1 parent 0d91d94 commit 7cf1593
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 31 deletions.
39 changes: 8 additions & 31 deletions database/migrations/2023_06_07_000001_create_pulse_tables.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Laravel\Pulse\Support\PulseMigration;

return new class extends Migration
return new class extends PulseMigration
{
/**
* Run the migrations.
Expand All @@ -17,17 +15,14 @@ public function up(): void
return;
}

$connection = DB::connection($this->getConnection());

Schema::create('pulse_values', function (Blueprint $table) use ($connection) {
Schema::create('pulse_values', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('timestamp');
$table->string('type');
$table->mediumText('key');
match ($driver = $connection->getDriverName()) {
match ($this->driver()) {
'mysql' => $table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))'),
'pgsql' => $table->uuid('key_hash')->storedAs('md5("key")::uuid'),
default => throw new RuntimeException("Unsupported database driver [{$driver}]."),
};
$table->mediumText('value');

Expand All @@ -36,15 +31,14 @@ public function up(): void
$table->unique(['type', 'key_hash']); // For data integrity and upserts...
});

Schema::create('pulse_entries', function (Blueprint $table) use ($connection) {
Schema::create('pulse_entries', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('timestamp');
$table->string('type');
$table->mediumText('key');
match ($driver = $connection->getDriverName()) {
match ($this->driver()) {
'mysql' => $table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))'),
'pgsql' => $table->uuid('key_hash')->storedAs('md5("key")::uuid'),
default => throw new RuntimeException("Unsupported database driver [{$driver}]."),
};
$table->bigInteger('value')->nullable();

Expand All @@ -54,16 +48,15 @@ public function up(): void
$table->index(['timestamp', 'type', 'key_hash', 'value']); // For aggregate queries...
});

Schema::create('pulse_aggregates', function (Blueprint $table) use ($connection) {
Schema::create('pulse_aggregates', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('bucket');
$table->unsignedMediumInteger('period');
$table->string('type');
$table->mediumText('key');
match ($driver = $connection->getDriverName()) {
match ($this->driver()) {
'mysql' => $table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))'),
'pgsql' => $table->uuid('key_hash')->storedAs('md5("key")::uuid'),
default => throw new RuntimeException("Unsupported database driver [{$driver}]."),
};
$table->string('aggregate');
$table->decimal('value', 20, 2);
Expand All @@ -85,20 +78,4 @@ public function down(): void
Schema::dropIfExists('pulse_entries');
Schema::dropIfExists('pulse_aggregates');
}

/**
* Get the migration connection name.
*/
public function getConnection(): ?string
{
return Config::get('pulse.storage.database.connection');
}

/**
* Determine if the migration should run.
*/
public function shouldRun(): bool
{
return ! App::environment('testing') || config('pulse.enabled');
}
};
49 changes: 49 additions & 0 deletions src/Support/PulseMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Laravel\Pulse\Support;

use Illuminate\Database\Connection;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use RuntimeException;

class PulseMigration extends Migration
{
/**
* Get the migration connection name.
*/
public function getConnection(): ?string
{
return Config::get('pulse.storage.database.connection');
}

/**
* Determine if the migration should run.
*/
protected function shouldRun(): bool
{
if (in_array($this->driver(), ['mysql', 'pgsql'])) {
return true;
}

if (! App::environment('testing')) {
throw new RuntimeException("Pulse does not support the [{$this->driver()}] database driver.");
}

if (Config::get('pulse.enabled')) {
throw new RuntimeException("Pulse does not support the [{$this->driver()}] database driver. You can disable Pulse in your testsuite by adding `<env name=\"PULSE_ENABLED\" value=\"false\"/>` to your project's `phpunit.xml` file.");
}

return false;
}

/**
* Get the database connection driver.
*/
protected function driver(): string
{
return DB::connection($this->getConnection())->getDriverName();
}
}

0 comments on commit 7cf1593

Please sign in to comment.