Keepstar icon indicating copy to clipboard operation
Keepstar copied to clipboard

[Improvement] Database

Open drkthunder02 opened this issue 6 years ago • 1 comments

It would make more sense to have a configuration option to allow the tables to utilize mysql or sqlite. Another way to improve this transaction on large servers is to just check for the file in the cron job rather than build the structure to add the sqlite file then check to see if it's been created. You could have the configuration file specify whether mysql is being utilized which then you don't have to check for the tables each and every time or if sqlite is being utilized check for the file existing first before anything else goes on in the createAuthDb() function.

drkthunder02 avatar Jan 21 '19 23:01 drkthunder02

This is how I would reorder the function for an sqlite only implementation.

<?php
function createAuthDb()
{
	// Does the file exist?
    if (!file_exists(__DIR__ . '/database/auth.sqlite')) {
        touch(__DIR__ . '/database/auth.sqlite');
		
		$tables = array('authed', 'keepstar');
		$createCode = array(
			'authed' => '
				BEGIN;
				CREATE TABLE IF NOT EXISTS `authed` (
					`id`	INTEGER PRIMARY KEY AUTOINCREMENT,
					`characterID`	INTEGER NOT NULL UNIQUE,
					`discordID`	INTEGER NOT NULL UNIQUE,
					`groups`	TEXT NOT NULL
				);
				COMMIT;',
			'keepstar' => '
				BEGIN;
				CREATE TABLE IF NOT EXISTS `keepstar` (
					`id`	INTEGER PRIMARY KEY AUTOINCREMENT,
					`variable`	TEXT NOT NULL UNIQUE,
					`value`	TEXT NOT NULL
				);
				COMMIT;');
		
		// Create table if not exists
		foreach ($tables as $table) {
			$exists = dbQueryField("SELECT name FROM sqlite_master WHERE type = 'table' AND name = :name", 'name', array(':name' => $table));
			if (!$exists) {
				dbExecute(trim($createCode[$table]));
			}
		}
    } else {
		return;
	}
}

drkthunder02 avatar Jan 21 '19 23:01 drkthunder02