1
0
mirror of https://github.com/flarum/core.git synced 2025-08-06 08:27:42 +02:00

feat: add support for PgSQL (#3985)

* feat: add support for `PgSQL`
* chore: generate dump
* feat: query exception errors db driver hint
* feat: allow defining supported databases
* chore: review comments
* feat: setting for pgsql preferred search config
This commit is contained in:
Sami Mazouz
2024-06-22 08:03:56 +01:00
committed by GitHub
parent d04cda6ca3
commit 379298acb0
76 changed files with 2097 additions and 261 deletions

View File

@@ -40,7 +40,11 @@ class SetupScript
{
$this->driver = getenv('DB_DRIVER') ?: 'mysql';
$this->host = getenv('DB_HOST') ?: 'localhost';
$this->port = intval(getenv('DB_PORT') ?: 3306);
$this->port = intval(getenv('DB_PORT') ?: match ($this->driver) {
'mysql' => 3306,
'pgsql' => 5432,
default => 0,
});
$this->name = getenv('DB_DATABASE') ?: 'flarum_test';
$this->user = getenv('DB_USERNAME') ?: 'root';
$this->pass = getenv('DB_PASSWORD') ?? 'root';

View File

@@ -201,6 +201,10 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
*/
$this->database()->getSchemaBuilder()->disableForeignKeyConstraints();
if ($this->database()->getDriverName() === 'pgsql') {
$this->database()->statement("SET session_replication_role = 'replica'");
}
$databaseContent = [];
foreach ($this->databaseContent as $tableOrModelClass => $_rows) {
@@ -224,6 +228,8 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
}
}
$tables = [];
// Then, insert all rows required for this test case.
foreach ($databaseContent as $table => $data) {
foreach ($data['rows'] as $row) {
@@ -238,9 +244,24 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
}
$this->database()->table($table)->updateOrInsert($unique, $row);
if (isset($row['id'])) {
$tables[$table] = 'id';
}
}
}
if ($this->database()->getDriverName() === 'pgsql') {
// PgSQL doesn't auto-increment the sequence when inserting the IDs manually.
foreach ($tables as $table => $id) {
$wrappedTable = $this->database()->getSchemaGrammar()->wrapTable($table);
$seq = $this->database()->getSchemaGrammar()->wrapTable($table.'_'.$id.'_seq');
$this->database()->statement("SELECT setval('$seq', (SELECT MAX($id) FROM $wrappedTable))");
}
$this->database()->statement("SET session_replication_role = 'origin'");
}
// And finally, turn on foreign key checks again.
$this->database()->getSchemaBuilder()->enableForeignKeyConstraints();
}