Skip to content

Commit

Permalink
[1.x] Postgres support (#64)
Browse files Browse the repository at this point in the history
* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Fix code styling

* wip

* wip

* wip

* wip

---------

Co-authored-by: jessarcher <[email protected]>
  • Loading branch information
jessarcher and jessarcher authored Dec 5, 2023
1 parent 1f58a95 commit 23ce6d4
Show file tree
Hide file tree
Showing 8 changed files with 389 additions and 125 deletions.
60 changes: 57 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ on:
- cron: '0 0 * * *'

jobs:
tests:
mysql:
runs-on: ubuntu-22.04

services:
mysql:
image: mysql:5.7
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: testing
MYSQL_DATABASE: forge
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
Expand All @@ -35,7 +35,7 @@ jobs:
laravel: [10]
stability: [prefer-lowest, prefer-stable]

name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }} - Stability ${{ matrix.stability }}
name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }} - Stability ${{ matrix.stability }} - MySQL 5.7

steps:
- name: Checkout code
Expand All @@ -62,3 +62,57 @@ jobs:
env:
DB_CONNECTION: mysql
DB_USERNAME: root

pgsql:
runs-on: ubuntu-22.04

services:
postgresql:
image: postgres:14
env:
POSTGRES_DB: forge
POSTGRES_USER: forge
POSTGRES_PASSWORD: password
ports:
- 5432:5432
options: --health-cmd=pg_isready --health-interval=10s --health-timeout=5s --health-retries=3
redis:
image: redis
ports:
- 6379:6379
options: --entrypoint redis-server

strategy:
fail-fast: true
matrix:
php: [8.2, 8.3]
laravel: [10]

name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }} - Stability ${{ matrix.stability }} - PostgreSQL 14

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
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 "illuminate/contracts=^${{ matrix.laravel }}" --dev --no-update
composer update --prefer-dist --no-interaction --no-progress
- name: Execute tests
run: vendor/bin/pest
env:
DB_CONNECTION: pgsql
DB_PASSWORD: password
29 changes: 22 additions & 7 deletions database/migrations/2023_06_07_000001_create_pulse_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;

return new class extends Migration
Expand All @@ -20,27 +21,37 @@ public function getConnection(): ?string
*/
public function up(): void
{
Schema::create('pulse_values', function (Blueprint $table) {
$connection = $this->getConnection() ?? DB::connection();

Schema::create('pulse_values', function (Blueprint $table) use ($connection) {
$table->engine = 'InnoDB';
$table->id();
$table->unsignedInteger('timestamp');
$table->string('type');
$table->text('key');
$table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))');
match ($driver = $connection->getDriverName()) {
'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->text('value');

$table->index('timestamp'); // For trimming...
$table->index('type'); // For fast lookups and purging...
$table->unique(['type', 'key_hash']); // For data integrity...
$table->unique(['type', 'key_hash']); // For data integrity and upserts...
});

Schema::create('pulse_entries', function (Blueprint $table) {
Schema::create('pulse_entries', function (Blueprint $table) use ($connection) {
$table->engine = 'InnoDB';
$table->id();
$table->unsignedInteger('timestamp');
$table->string('type');
$table->text('key');
$table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))');
match ($driver = $connection->getDriverName()) {
'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();

$table->index('timestamp'); // For trimming...
Expand All @@ -49,14 +60,18 @@ public function up(): void
$table->index(['timestamp', 'type', 'key_hash', 'value']); // For aggregate queries...
});

Schema::create('pulse_aggregates', function (Blueprint $table) {
Schema::create('pulse_aggregates', function (Blueprint $table) use ($connection) {
$table->engine = 'InnoDB';
$table->id();
$table->unsignedInteger('bucket');
$table->unsignedMediumInteger('period');
$table->string('type');
$table->text('key');
$table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))');
match ($driver = $connection->getDriverName()) {
'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);
$table->unsignedInteger('count')->nullable();
Expand Down
3 changes: 0 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,5 @@
</testsuites>
<php>
<env name="APP_KEY" value="base64:uz4B1RtFO57QGzbZX1kRYX9hIRB50+QzqFeg9zbFJlY="/>
<env name="DB_CONNECTION" value="mysql"/>
<env name="DB_DATABASE" value="testing"/>
<env name="DB_USERNAME" value="root"/>
</php>
</phpunit>
Loading

0 comments on commit 23ce6d4

Please sign in to comment.