Skip to content

Commit

Permalink
refactor(setup database script): Review and improve database schema s…
Browse files Browse the repository at this point in the history
…etup with column migration logic
  • Loading branch information
Shadow243 committed Nov 29, 2024
1 parent 2657f72 commit d945bbc
Showing 1 changed file with 101 additions and 42 deletions.
143 changes: 101 additions & 42 deletions scripts/setup_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

$connected = false;
$create_table = "CREATE TABLE IF NOT EXISTS";
$alter_table = "ALTER TABLE";
$bad_driver = "Unsupported db driver: {$db_driver}";

// NOTE: these sql commands could be db agnostic if we change the blobs to text
Expand Down Expand Up @@ -83,56 +84,114 @@
}
}

if (strcasecmp($session_type, 'DB')==0) {
printf("Creating database table hm_user_session ...\n");

if ($db_driver == 'mysql') {
$stmt = "{$create_table} hm_user_session (hm_id varchar(255), data longblob, date timestamp, primary key (hm_id));";
} elseif($db_driver == 'sqlite') {
//0 means unlocked, 1 means locked
$stmt = "{$create_table} hm_user_session (hm_id varchar(255), data longblob, lock INTEGER DEFAULT 0, date timestamp, primary key (hm_id));";
} elseif ($db_driver == 'pgsql') {
$stmt = "{$create_table} hm_user_session (hm_id varchar(255) primary key not null, data text, date timestamp);";
} else {
die($bad_driver);
}

$conn->exec($stmt);
}
if (strcasecmp($auth_type, 'DB')==0) {

printf("Creating database table hm_user ...\n");
function get_existing_columns($conn, $table_name, $db_driver) {
$columns = [];
try {
if ($db_driver == 'mysql') {
$query = "SHOW COLUMNS FROM {$table_name};";
} elseif ($db_driver == 'pgsql') {
$query = "SELECT column_name FROM information_schema.columns WHERE table_name = '{$table_name}';";
} elseif ($db_driver == 'sqlite') {
$query = "PRAGMA table_info({$table_name});";
} else {
throw new Exception("Unsupported DB driver for column retrieval.");
}

if ($db_driver == 'mysql' || $db_driver == 'sqlite') {
$stmt = "{$create_table} hm_user (username varchar(255), hash varchar(255), primary key (username));";
} elseif ($db_driver == 'pgsql') {
$stmt = "{$create_table} hm_user (username varchar(255) primary key not null, hash varchar(255));";
} else {
die($bad_driver);
$stmt = $conn->query($query);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
if ($db_driver == 'sqlite') {
$columns[] = $row['name'];
} else {
$columns[] = $row['Field'] ?? $row['column_name'];
}
}
} catch (Exception $e) {
printf("Error retrieving columns: %s\n", $e->getMessage());
}
return $columns;
}

try {
$rows = $conn->exec($stmt);
printf("{$stmt}\n");
} catch (PDOException $e) {
print($e);
exit (1);
function add_missing_columns($conn, $table_name, $required_columns, $db_driver) {
global $alter_table;
$existing_columns = get_existing_columns($conn, $table_name, $db_driver);

foreach ($required_columns as $column_name => $column_def) {
if (!in_array($column_name, $existing_columns)) {
printf("Adding column %s to table %s ...\n", $column_name, $table_name);
$query = "{$alter_table} {$table_name} ADD COLUMN {$column_name} {$column_def};";
try {
$conn->exec($query);
} catch (PDOException $e) {
printf("Error adding column %s: %s\n", $column_name, $e->getMessage());
exit(1);
}
}
}

}
if (strcasecmp($user_config_type, 'DB')==0) {

printf("Creating database table hm_user_settings ...\n");
$tables = [
'hm_user_session' => [
'mysql' => [
'hm_id' => 'varchar(255) PRIMARY KEY',
'data' => 'longblob',
'date' => 'timestamp',
],
'sqlite' => [
'hm_id' => 'varchar(255) PRIMARY KEY',
'data' => 'longblob',
'lock' => 'INTEGER DEFAULT 0',
'date' => 'timestamp',
],
'pgsql' => [
'hm_id' => 'varchar(255) PRIMARY KEY',
'data' => 'text',
'date' => 'timestamp',
],
],
'hm_user' => [
'mysql' => [
'username' => 'varchar(255) PRIMARY KEY',
'hash' => 'varchar(255)',
],
'sqlite' => [
'username' => 'varchar(255) PRIMARY KEY',
'hash' => 'varchar(255)',
],
'pgsql' => [
'username' => 'varchar(255) PRIMARY KEY',
'hash' => 'varchar(255)',
],
],
'hm_user_settings' => [
'mysql' => [
'username' => 'varchar(255) PRIMARY KEY',
'settings' => 'longblob',
],
'sqlite' => [
'username' => 'varchar(255) PRIMARY KEY',
'settings' => 'longblob',
],
'pgsql' => [
'username' => 'varchar(255) PRIMARY KEY',
'settings' => 'text',
],
],
];

if ($db_driver == 'mysql' || $db_driver == 'sqlite') {
$stmt = "{$create_table} hm_user_settings(username varchar(255), settings longblob, primary key (username));";
} elseif ($db_driver == 'pgsql') {
$stmt = "{$create_table} hm_user_settings (username varchar(255) primary key not null, settings text);";
} else {
die($bad_driver);
if (strcasecmp($session_type, 'DB')==0) {
foreach ($tables as $table_name => $definitions) {
$required_columns = $definitions[$db_driver];

// Create table if it doesn't exist
$columns = implode(', ', array_map(fn($col, $def) => "$col $def", array_keys($required_columns), $required_columns));
$query = "{$create_table} {$table_name} ({$columns});";
$conn->exec($query);

// Add any missing columns using ALTER TABLE
add_missing_columns($conn, $table_name, $required_columns, $db_driver);
}

$conn->exec($stmt);
print("\nDatabase setup and migration finished\n");
}

print("\nDb setup finished\n");

0 comments on commit d945bbc

Please sign in to comment.