Skip to content

Commit

Permalink
Draft for SQL Server support
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenobird committed Oct 22, 2024
1 parent 5dd3cd2 commit 36729ff
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 4 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,58 @@ jobs:
run: vendor/bin/pest -vvv
env:
DB_CONNECTION: sqlite

sqlsrv:
runs-on: ubuntu-22.04

services:
sqlsrv:
image: mcr.microsoft.com/mssql/server:2022-latest
env:
ACCEPT_EULA: 'Y'
MSSQL_SA_PASSWORD: 'sTr0ngP@ssw0rd!'
ports:
- 1433:1433
redis:
image: redis
ports:
- 6379:6379
options: --entrypoint redis-server

strategy:
fail-fast: true
matrix:
php: [8.3]
laravel: [11]
stability: [prefer-stable]

name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }} - Stability ${{ matrix.stability }} - SQL Server 2022

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, redis, pcntl, zip, relay, sqlsrv, pdo_sqlsrv, odbc, pdo_odbc,
ini-values: error_reporting=E_ALL
tools: composer:v2
coverage: none

- name: Install redis-cli
run: sudo apt-get install -qq redis-tools

- name: Install dependencies
run: |
composer require cachewerk/relay --no-interaction --no-update
composer update --prefer-dist --no-interaction --no-progress --${{ matrix.stability }}
- name: Execute tests
run: vendor/bin/pest -vvv
env:
DB_CONNECTION: sqlsrv
DB_DATABASE: master
DB_USERNAME: sa
DB_PASSWORD: 'sTr0ngP@ssw0rd!'
3 changes: 3 additions & 0 deletions database/migrations/2023_06_07_000001_create_pulse_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function up(): void
'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'),
'sqlsrv' => $table->string('key_hash', 32),
};
$table->mediumText('value');

Expand All @@ -41,6 +42,7 @@ public function up(): void
'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'),
'sqlsrv' => $table->string('key_hash', 32),
};
$table->bigInteger('value')->nullable();

Expand All @@ -60,6 +62,7 @@ public function up(): void
'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'),
'sqlsrv' => $table->string('key_hash', 32),
};
$table->string('aggregate');
$table->decimal('value', 20, 2);
Expand Down
24 changes: 22 additions & 2 deletions src/Storage/DatabaseStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ protected function upsertCount(array $values): int
'pgsql', 'sqlite' => new Expression(<<<SQL
{$this->wrap('pulse_aggregates.value')} + "excluded"."value"
SQL),
'sqlsrv' => new Expression(<<<SQL
{$this->wrap('pulse_aggregates.value')} + [laravel_source].[value]
SQL),
default => throw new RuntimeException("Unsupported database driver [{$driver}]"),
},
]
Expand All @@ -207,6 +210,9 @@ protected function upsertMin(array $values): int
'sqlite' => new Expression(<<<SQL
min({$this->wrap('pulse_aggregates.value')}, "excluded"."value")
SQL),
'sqlsrv' => new Expression(<<<SQL
least({$this->wrap('pulse_aggregates.value')}, + [laravel_source].[value])
SQL),
default => throw new RuntimeException("Unsupported database driver [{$driver}]"),
},
]
Expand All @@ -232,6 +238,9 @@ protected function upsertMax(array $values): int
'sqlite' => new Expression(<<<SQL
max({$this->wrap('pulse_aggregates.value')}, "excluded"."value")
SQL),
'sqlsrv' => new Expression(<<<SQL
greatest({$this->wrap('pulse_aggregates.value')}, + [laravel_source].[value])
SQL),
default => throw new RuntimeException("Unsupported database driver [{$driver}]"),
},
]
Expand All @@ -254,6 +263,9 @@ protected function upsertSum(array $values): int
'pgsql', 'sqlite' => new Expression(<<<SQL
{$this->wrap('pulse_aggregates.value')} + "excluded"."value"
SQL),
'sqlsrv' => new Expression(<<<SQL
{$this->wrap('pulse_aggregates.value')} + [laravel_source].[value]
SQL),
default => throw new RuntimeException("Unsupported database driver [{$driver}]"),
},
]
Expand Down Expand Up @@ -283,6 +295,14 @@ protected function upsertAvg(array $values): int
{$this->wrap('pulse_aggregates.count')} + "excluded"."count"
SQL),
],
'sqlsrv' => [
'value' => new Expression(<<<SQL
({$this->wrap('pulse_aggregates.value')} * {$this->wrap('pulse_aggregates.count')} + ([laravel_source].[value] * [laravel_source].[count])) / ({$this->wrap('pulse_aggregates.count')} + [laravel_source].[count])
SQL),
'count' => new Expression(<<<SQL
{$this->wrap('pulse_aggregates.count')} + [laravel_source].[count]
SQL),
],
default => throw new RuntimeException("Unsupported database driver [{$driver}]"),
}
);
Expand Down Expand Up @@ -672,7 +692,7 @@ public function aggregateTypes(

foreach ($types as $type) {
$query->selectRaw(match ($aggregate) {
'count' => "count(case when ({$this->wrap('type')} = ?) then true else null end)",
'count' => "count(case when ({$this->wrap('type')} = ?) then 1 else null end)",
'min' => "min(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
'max' => "max(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
'sum' => "sum(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
Expand Down Expand Up @@ -820,6 +840,6 @@ protected function wrap(string $value): string
*/
protected function requiresManualKeyHash(): bool
{
return $this->connection()->getDriverName() === 'sqlite';
return in_array($this->connection()->getDriverName(), ['sqlite', 'sqlsrv']);
}
}
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(), ['mariadb', 'mysql', 'pgsql', 'sqlite'])) {
if (in_array($this->driver(), ['mariadb', 'mysql', 'pgsql', 'sqlite', 'sqlsrv'])) {
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 @@ -105,7 +105,7 @@ function keyHash(string $string): string
return match (DB::connection()->getDriverName()) {
'mariadb', 'mysql' => hex2bin(md5($string)),
'pgsql' => Uuid::fromString(md5($string)),
'sqlite' => md5($string),
'sqlite', 'sqlsrv' => md5($string),
};
}

Expand Down

0 comments on commit 36729ff

Please sign in to comment.