1
0
mirror of https://github.com/flarum/core.git synced 2025-08-06 16:36:47 +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

@@ -32,11 +32,10 @@ return [
$table->unique(['discussion_id', 'number']);
});
$connection = $schema->getConnection();
$prefix = $connection->getTablePrefix();
if ($connection->getDriverName() !== 'sqlite') {
$connection->statement('ALTER TABLE '.$prefix.'posts ADD FULLTEXT content (content)');
if ($schema->getConnection()->getDriverName() !== 'sqlite') {
$schema->table('posts', function (Blueprint $table) {
$table->fullText('content');
});
}
},

View File

@@ -26,11 +26,22 @@ return [
$table->integer('user_id')->unsigned()->change();
});
// Use a separate schema instance because this column gets renamed
// in the previous one.
$schema->table('access_tokens', function (Blueprint $table) {
$table->dateTime('last_activity_at')->change();
});
if ($schema->getConnection()->getDriverName() === 'pgsql') {
$prefix = $schema->getConnection()->getTablePrefix();
// Changing an integer col to datetime is an unusual operation in PostgreSQL.
$schema->getConnection()->statement(<<<SQL
ALTER TABLE {$prefix}access_tokens
ALTER COLUMN last_activity_at TYPE TIMESTAMP(0) WITHOUT TIME ZONE
USING to_timestamp(last_activity_at)
SQL);
} else {
// Use a separate schema instance because this column gets renamed
// in the previous one.
$schema->table('access_tokens', function (Blueprint $table) {
$table->dateTime('last_activity_at')->change();
});
}
},
'down' => function (Builder $schema) {

View File

@@ -7,21 +7,23 @@
* LICENSE file that was distributed with this source code.
*/
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
return [
'up' => function (Builder $schema) {
$connection = $schema->getConnection();
$prefix = $connection->getTablePrefix();
if ($connection->getDriverName() !== 'sqlite') {
$connection->statement('ALTER TABLE '.$prefix.'discussions ADD FULLTEXT title (title)');
if ($schema->getConnection()->getDriverName() !== 'sqlite') {
$schema->table('discussions', function (Blueprint $table) {
$table->fullText('title');
});
}
},
'down' => function (Builder $schema) {
$connection = $schema->getConnection();
$prefix = $connection->getTablePrefix();
$connection->statement('ALTER TABLE '.$prefix.'discussions DROP INDEX title');
if ($schema->getConnection()->getDriverName() !== 'sqlite') {
$schema->table('discussions', function (Blueprint $table) {
$table->dropFullText('title');
});
}
}
];

View File

@@ -28,6 +28,13 @@ return [
$db->table('groups')->insert(array_combine(['id', 'name_singular', 'name_plural', 'color', 'icon'], $group));
}
// PgSQL doesn't auto-increment the sequence when inserting the IDs manually.
if ($db->getDriverName() === 'pgsql') {
$table = $db->getSchemaGrammar()->wrapTable('groups');
$seq = $db->getSchemaGrammar()->wrapTable('groups_id_seq');
$db->statement("SELECT setval('$seq', (SELECT MAX(id) FROM $table))");
}
},
'down' => function (Builder $schema) {

View File

@@ -0,0 +1,37 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
return [
'up' => function (Builder $schema) {
if ($schema->getConnection()->getDriverName() === 'pgsql') {
$users = $schema->getConnection()->getSchemaGrammar()->wrapTable('users');
$preferences = $schema->getConnection()->getSchemaGrammar()->wrap('preferences');
$schema->getConnection()->statement("ALTER TABLE $users ALTER COLUMN $preferences TYPE JSON USING preferences::TEXT::JSON");
} else {
$schema->table('users', function (Blueprint $table) {
$table->json('preferences')->nullable()->change();
});
}
},
'down' => function (Builder $schema) {
if ($schema->getConnection()->getDriverName() === 'pgsql') {
$users = $schema->getConnection()->getSchemaGrammar()->wrapTable('users');
$preferences = $schema->getConnection()->getSchemaGrammar()->wrap('preferences');
$schema->getConnection()->statement("ALTER TABLE $users ALTER COLUMN $preferences TYPE BYTEA USING preferences::TEXT::BYTEA");
} else {
$schema->table('users', function (Blueprint $table) {
$table->binary('preferences')->nullable()->change();
});
}
}
];

View File

@@ -0,0 +1,37 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
return [
'up' => function (Builder $schema) {
if ($schema->getConnection()->getDriverName() === 'pgsql') {
$notifications = $schema->getConnection()->getSchemaGrammar()->wrapTable('notifications');
$data = $schema->getConnection()->getSchemaGrammar()->wrap('data');
$schema->getConnection()->statement("ALTER TABLE $notifications ALTER COLUMN $data TYPE JSON USING data::TEXT::JSON");
} else {
$schema->table('notifications', function (Blueprint $table) {
$table->json('data')->nullable()->change();
});
}
},
'down' => function (Builder $schema) {
if ($schema->getConnection()->getDriverName() === 'pgsql') {
$notifications = $schema->getConnection()->getSchemaGrammar()->wrapTable('notifications');
$data = $schema->getConnection()->getSchemaGrammar()->wrap('data');
$schema->getConnection()->statement("ALTER TABLE $notifications ALTER COLUMN $data TYPE BYTEA USING data::TEXT::BYTEA");
} else {
$schema->table('notifications', function (Blueprint $table) {
$table->binary('data')->nullable()->change();
});
}
}
];

File diff suppressed because it is too large Load Diff