Skip to content

Commit

Permalink
refactor(other): Extract database extension checks into reusable func…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
Baraka24 authored and josaphatim committed Dec 19, 2024
1 parent 93d9c95 commit 5900008
Showing 1 changed file with 25 additions and 27 deletions.
52 changes: 25 additions & 27 deletions scripts/setup_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@
require VENDOR_PATH.'autoload.php';
require APP_PATH.'lib/framework.php';

/**
* Function to check required extensions for a database driver
*/
function checkRequiredExtensions($db_driver, $extensions) {
$missing_extensions = [];

foreach ($extensions as $extension) {
if (!extension_loaded($extension)) {
$missing_extensions[] = $extension;
}
}

if (!empty($missing_extensions)) {
error_log(
"The following required {$db_driver} extensions are missing: " .
implode(', ', $missing_extensions) .
". Please install them.\n"
);
exit(1);
}
}

$environment = Hm_Environment::getInstance();
$environment->load();

Expand All @@ -29,34 +51,10 @@

// Check if the required extensions for the configured DB driver are loaded
if ($db_driver == 'mysql') {
$required_extensions = ['mysqli', 'mysqlnd', 'pdo_mysql'];
$missing_extensions = [];

foreach ($required_extensions as $extension) {
if (!extension_loaded($extension)) {
$missing_extensions[] = $extension;
}
}

if (!empty($missing_extensions)) {
error_log('The following required MySQL extensions are missing: ' . implode(', ', $missing_extensions) . ". Please install them.\n");
exit(1);
}
checkRequiredExtensions('MySQL', ['mysqli', 'mysqlnd', 'pdo_mysql']);
} elseif ($db_driver == 'pgsql') {
$required_extensions = ['pgsql', 'pdo_pgsql'];
$missing_extensions = [];

foreach ($required_extensions as $extension) {
if (!extension_loaded($extension)) {
$missing_extensions[] = $extension;
}
}

if (!empty($missing_extensions)) {
error_log('The following required PostgreSQL extensions are missing: ' . implode(', ', $missing_extensions) . ". Please install them.\n");
exit(1);
}
} elseif ($db_driver !== 'sqlite'){
checkRequiredExtensions('PostgreSQL', ['pgsql', 'pdo_pgsql']);
} elseif ($db_driver !== 'sqlite') {
error_log("Unsupported DB driver: {$db_driver}");
exit(1);
}
Expand Down

0 comments on commit 5900008

Please sign in to comment.