-
-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PostgreSQL Support #896
base: main
Are you sure you want to change the base?
PostgreSQL Support #896
Conversation
database/migrations/2025_01_08_052636_add_driver_to_database_hosts copy.php
Outdated
Show resolved
Hide resolved
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
switch (Schema::getConnection()->getDriverName()) { | ||
case 'pgsql': | ||
$database = DB::connection()->getDatabaseName(); | ||
DB::statement(sprintf('REVOKE CONNECT ON DATABASE "%s" FROM public', $database)); | ||
break; | ||
} | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
switch (Schema::getConnection()->getDriverName()) { | ||
case 'pgsql': | ||
$database = DB::connection()->getDatabaseName(); | ||
DB::statement(sprintf('GRANT CONNECT ON DATABASE "%s" TO public', $database)); | ||
break; | ||
} | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The user should do it not migrations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or should it be part of the installer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should probably be documented somewhere then that it's important if your panel database is on the same server as the server one.
database/migrations/2024_11_04_185326_revamp_api_keys_permissions.php
Outdated
Show resolved
Hide resolved
}); | ||
break; | ||
case 'pgsql': | ||
DB::statement('ALTER TABLE egg_variables ALTER COLUMN rules TYPE jsonb USING to_jsonb(rules)'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless absolute necessity you shouldn't use raw statements and what i see from other migrations tells me its not mandatory
switch (Schema::getConnection()->getDriverName()) { | ||
case 'mariadb': | ||
case 'mysql': | ||
$table->dropForeign('service_options_nest_id_foreign'); | ||
break; | ||
case 'sqlite': | ||
$table->dropForeign(['nest_id']); | ||
break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
switch (Schema::getConnection()->getDriverName()) { | |
case 'mariadb': | |
case 'mysql': | |
$table->dropForeign('service_options_nest_id_foreign'); | |
break; | |
case 'sqlite': | |
$table->dropForeign(['nest_id']); | |
break; | |
$table->dropForeign(['nest_id']); |
Those are old format
database/migrations/2021_03_21_104718_force_cron_month_field_to_have_value_if_missing.php
Outdated
Show resolved
Hide resolved
database/migrations/2020_03_22_163911_merge_permissions_table_into_subusers.php
Outdated
Show resolved
Hide resolved
database/migrations/2017_02_10_171858_UpdateAPIKeyColumnNames.php
Outdated
Show resolved
Hide resolved
database/migrations/2016_09_04_182835_create_notifications_table.php
Outdated
Show resolved
Hide resolved
switch ($database->host->driver) { | ||
case 'mysql': | ||
case 'mariadb': | ||
$database->dropUser($database->username, $database->remote); | ||
$database->createUser($database->database, $database->username, $database->remote, $password, $database->max_connections); | ||
$database->assignUserToDatabase($database->database, $database->username, $database->remote); | ||
$database->flush(); | ||
break; | ||
case 'pgsql': | ||
$database->updateUserPassword($database->database, $database->username, $database->remote, $password); | ||
break; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use if/else or match
@@ -57,6 +64,7 @@ class DatabaseHost extends Model | |||
'password' => 'nullable|string', | |||
'node_ids' => 'nullable|array', | |||
'node_ids.*' => 'required|integer,exists:nodes,id', | |||
'driver' => 'required|string', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'driver' => 'required|string', | |
'driver' => 'required|string|in:' . self::DRIVERS, |
public const DRIVERS = 'mysql,mariadb,pgsql';
if ($driver === 'sqlite') { | ||
return true; | ||
} | ||
switch ($driver) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use match
config()->set('database.connections._panel_install_test', [ | ||
'driver' => $driver, | ||
'host' => $host, | ||
'port' => $port, | ||
'database' => $database, | ||
'username' => $username, | ||
'password' => $password, | ||
'charset' => 'utf8mb4', | ||
'collation' => 'utf8mb4_unicode_ci', | ||
'strict' => true, | ||
]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use DynamicDatabaseConnection instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why DynamicDatabaseConnection doesn't just have a static set
function?
public const DB_DEFAULTS = [ | ||
'mysql' => [ | ||
'DB_CHARSET' => 'utf8', | ||
'DB_COLLATION' => 'utf8_unicode_ci', | ||
'DEFAULT_DB' => 'mysql', | ||
], | ||
'mariadb' => [ | ||
'DB_CHARSET' => 'utf8', | ||
'DB_COLLATION' => 'utf8_unicode_ci', | ||
'DEFAULT_DB' => 'mysql', | ||
], | ||
'pgsql' => [ | ||
'DB_CHARSET' => 'utf8', | ||
'DB_COLLATION' => 'en_US', | ||
'DEFAULT_DB' => 'postgres', | ||
], | ||
]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use config instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost there, please rebase to not use the PR #894
e8978b8
to
b8e343c
Compare
Sorry, I'm not comfortable with changing that many past migrations and adding switch statements to that many of them, especially from that many years back. Most likely what would have to happen is migration squashing to get it to work properly with Postgresql. Please see here: https://laravel.com/docs/11.x/migrations#squashing-migrations |
Adds PostgreSQL as an alternative database option. Additionally, adds configurable driver option to the DynamicDatabaseConnection part.
The database driver type is saved in a new column
driver
of the DatabaseHost, allowing for potentially other databases in the future if desired.I've tried to test everything I could think of, but please let me know if there's anything I missed, or if something should be adjusted. I want this to be as minimal of a maintenance burden as possible, but I do really want Postgres support because there's much better support for running it in a Kubernetes HA setup with CloudnativePG.