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:
@@ -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');
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
|
@@ -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) {
|
||||
|
@@ -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');
|
||||
});
|
||||
}
|
||||
}
|
||||
];
|
||||
|
@@ -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) {
|
||||
|
@@ -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();
|
||||
});
|
||||
}
|
||||
}
|
||||
];
|
@@ -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();
|
||||
});
|
||||
}
|
||||
}
|
||||
];
|
1247
framework/core/migrations/pgsql-install.dump
Normal file
1247
framework/core/migrations/pgsql-install.dump
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user