Skip to content

Commit

Permalink
Add mariadb driver support
Browse files Browse the repository at this point in the history
  • Loading branch information
jessarcher committed Mar 13, 2024
1 parent a641624 commit 180c42c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions database/migrations/2023_06_07_000001_create_pulse_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function up(): void
$table->string('type');
$table->mediumText('key');
match ($this->driver()) {
'mysql' => $table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))'),
'mariadb', 'mysql' => $table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))'),
'pgsql' => $table->uuid('key_hash')->storedAs('md5("key")::uuid'),
'sqlite' => $table->string('key_hash'),
};
Expand All @@ -38,7 +38,7 @@ public function up(): void
$table->string('type');
$table->mediumText('key');
match ($this->driver()) {
'mysql' => $table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))'),
'mariadb', 'mysql' => $table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))'),
'pgsql' => $table->uuid('key_hash')->storedAs('md5("key")::uuid'),
'sqlite' => $table->string('key_hash'),
};
Expand All @@ -57,7 +57,7 @@ public function up(): void
$table->string('type');
$table->mediumText('key');
match ($this->driver()) {
'mysql' => $table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))'),
'mariadb', 'mysql' => $table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))'),
'pgsql' => $table->uuid('key_hash')->storedAs('md5("key")::uuid'),
'sqlite' => $table->string('key_hash'),
};
Expand Down
10 changes: 5 additions & 5 deletions src/Storage/DatabaseStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ protected function upsertCount(array $values): int
['bucket', 'period', 'type', 'aggregate', 'key_hash'],
[
'value' => match ($driver = $this->connection()->getDriverName()) {
'mysql' => new Expression('`value` + values(`value`)'),
'mariadb', 'mysql' => new Expression('`value` + values(`value`)'),
'pgsql', 'sqlite' => new Expression('"pulse_aggregates"."value" + "excluded"."value"'),
default => throw new RuntimeException("Unsupported database driver [{$driver}]"),
},
Expand All @@ -202,7 +202,7 @@ protected function upsertMin(array $values): int
['bucket', 'period', 'type', 'aggregate', 'key_hash'],
[
'value' => match ($driver = $this->connection()->getDriverName()) {
'mysql' => new Expression('least(`value`, values(`value`))'),
'mariadb', 'mysql' => new Expression('least(`value`, values(`value`))'),
'pgsql' => new Expression('least("pulse_aggregates"."value", "excluded"."value")'),
'sqlite' => new Expression('min("pulse_aggregates"."value", "excluded"."value")'),
default => throw new RuntimeException("Unsupported database driver [{$driver}]"),
Expand All @@ -223,7 +223,7 @@ protected function upsertMax(array $values): int
['bucket', 'period', 'type', 'aggregate', 'key_hash'],
[
'value' => match ($driver = $this->connection()->getDriverName()) {
'mysql' => new Expression('greatest(`value`, values(`value`))'),
'mariadb', 'mysql' => new Expression('greatest(`value`, values(`value`))'),
'pgsql' => new Expression('greatest("pulse_aggregates"."value", "excluded"."value")'),
'sqlite' => new Expression('max("pulse_aggregates"."value", "excluded"."value")'),
default => throw new RuntimeException("Unsupported database driver [{$driver}]"),
Expand All @@ -244,7 +244,7 @@ protected function upsertSum(array $values): int
['bucket', 'period', 'type', 'aggregate', 'key_hash'],
[
'value' => match ($driver = $this->connection()->getDriverName()) {
'mysql' => new Expression('`value` + values(`value`)'),
'mariadb', 'mysql' => new Expression('`value` + values(`value`)'),
'pgsql', 'sqlite' => new Expression('"pulse_aggregates"."value" + "excluded"."value"'),
default => throw new RuntimeException("Unsupported database driver [{$driver}]"),
},
Expand All @@ -263,7 +263,7 @@ protected function upsertAvg(array $values): int
$values,
['bucket', 'period', 'type', 'aggregate', 'key_hash'],
match ($driver = $this->connection()->getDriverName()) {
'mysql' => [
'mariadb', 'mysql' => [
'value' => new Expression('(`value` * `count` + (values(`value`) * values(`count`))) / (`count` + values(`count`))'),
'count' => new Expression('`count` + values(`count`)'),
],
Expand Down
2 changes: 1 addition & 1 deletion src/Support/PulseMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function getConnection(): ?string
*/
protected function shouldRun(): bool
{
if (in_array($this->driver(), ['mysql', 'pgsql', 'sqlite'])) {
if (in_array($this->driver(), ['mariadb', 'mysql', 'pgsql', 'sqlite'])) {
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
function keyHash(string $string): string
{
return match (DB::connection()->getDriverName()) {
'mysql' => hex2bin(md5($string)),
'mariadb', 'mysql' => hex2bin(md5($string)),
'pgsql' => Uuid::fromString(md5($string)),
'sqlite' => md5($string),
};
Expand Down

0 comments on commit 180c42c

Please sign in to comment.