SQLite database is now recreated when installing.

This commit is contained in:
Evert Pot 2016-02-04 03:01:30 -05:00
parent 78512eba7e
commit 30165f1f34
3 changed files with 63 additions and 27 deletions

View file

@ -37,7 +37,7 @@ class Database extends \Flake\Core\Controller {
$this->oForm = $this->oModel->formForThisModelInstance(array( $this->oForm = $this->oModel->formForThisModelInstance(array(
"close" => FALSE, "close" => FALSE,
"hook.validation" => array($this, "validateMySQLConnection"), "hook.validation" => array($this, "validateConnection"),
"hook.morphology" => array($this, "hideMySQLFieldWhenNeeded"), "hook.morphology" => array($this, "hideMySQLFieldWhenNeeded"),
)); ));
@ -77,7 +77,7 @@ class Database extends \Flake\Core\Controller {
return $oView->render(); return $oView->render();
} }
public function validateMySQLConnection($oForm, $oMorpho) { public function validateConnection($oForm, $oMorpho) {
$bMySQLEnabled = $oMorpho->element("PROJECT_DB_MYSQL")->value(); $bMySQLEnabled = $oMorpho->element("PROJECT_DB_MYSQL")->value();
@ -141,7 +141,57 @@ class Database extends \Flake\Core\Controller {
$oMorpho->element("PROJECT_DB_MYSQL_PASSWORD") $oMorpho->element("PROJECT_DB_MYSQL_PASSWORD")
); );
} }
} } else {
$sFile = $oMorpho->element("PROJECT_SQLITE_FILE")->value();
try {
// not sure yet how to better address this
// yup! this is mental, but even if we don't use eval, effectively these
// config settings are eval'ed because they are written as raw php files.
// We'll have to clean this up later.
$sFile = eval('return ' . $sFile . ';');
$oDb = new \Flake\Core\Database\Sqlite(
$sFile
);
if(($aMissingTables = \Baikal\Core\Tools::isDBStructurallyComplete($oDb)) !== TRUE) {
# Checking if all tables are missing
$aRequiredTables = \Baikal\Core\Tools::getRequiredTablesList();
if(count($aRequiredTables) !== count($aMissingTables)) {
$sMessage = "<br /><p><strong>Database is not structurally complete.</strong></p>";
$sMessage .= "<p>Missing tables are: <strong>" . implode("</strong>, <strong>", $aMissingTables) . "</strong></p>";
$sMessage .= "<p>You will find the SQL definition of Baïkal tables in this file: <strong>Core/Resources/Db/SQLite/db.sql</strong></p>";
$sMessage .= "<br /><p>Nothing has been saved. <strong>Please, add these tables to the database before pursuing Baïkal initialization.</strong></p>";
$oForm->declareError(
$oMorpho->element("PROJECT_SQLITE_FILE"),
$sMessage
);
} else {
# All tables are missing
# We add these tables ourselves to the database, to initialize Baïkal
$sSqlDefinition = file_get_contents(PROJECT_PATH_CORERESOURCES . "Db/SQLite/db.sql");
foreach(explode(';', $sSqlDefinition) as $query) {
if (!trim($query)) continue;
$oDb->query($query);
}
}
}
return TRUE;
} catch(\Exception $e) {
$oForm->declareError(
$oMorpho->element("PROJECT_SQLITE_FILE"),
"Baïkal was not able to establish a connexion to the SQLite database as configured.<br />SQLite says: " . $e->getMessage() . (string)$e
);
}
// SQLite
}
} }
public function hideMySQLFieldWhenNeeded(\Formal\Form $oForm, \Formal\Form\Morphology $oMorpho) { public function hideMySQLFieldWhenNeeded(\Formal\Form $oForm, \Formal\Form\Morphology $oMorpho) {
@ -162,4 +212,4 @@ class Database extends \Flake\Core\Controller {
$oMorpho->remove("PROJECT_DB_MYSQL_PASSWORD"); $oMorpho->remove("PROJECT_DB_MYSQL_PASSWORD");
} }
} }
} }

View file

@ -244,18 +244,8 @@ class Framework extends \Flake\Core\Framework {
return FALSE; return FALSE;
} }
# Asserting DB file exists
if(!file_exists(PROJECT_SQLITE_FILE)) {
die("<h3>DB file does not exist. To create it, please copy '<span style='font-family: monospace; background: yellow;'>Core/Resources/Db/SQLite/db.sqlite</span>' to '<span style='font-family: monospace;background: yellow;'>" . PROJECT_SQLITE_FILE . "</span>'</h3>");
}
# Asserting DB file is readable
if(!is_readable(PROJECT_SQLITE_FILE)) {
die("<h3>DB file is not readable. Please give read permissions on file '<span style='font-family: monospace; background: yellow;'>" . PROJECT_SQLITE_FILE . "</span>'</h3>");
}
# Asserting DB file is writable # Asserting DB file is writable
if(!is_writable(PROJECT_SQLITE_FILE)) { if(file_exists(PROJECT_SQLITE_FILE) && !is_writable(PROJECT_SQLITE_FILE)) {
die("<h3>DB file is not writable. Please give write permissions on file '<span style='font-family: monospace; background: yellow;'>" . PROJECT_SQLITE_FILE . "</span>'</h3>"); die("<h3>DB file is not writable. Please give write permissions on file '<span style='font-family: monospace; background: yellow;'>" . PROJECT_SQLITE_FILE . "</span>'</h3>");
} }
@ -290,19 +280,15 @@ class Framework extends \Flake\Core\Framework {
die("<h3>The constant PROJECT_DB_MYSQL_PASSWORD, containing the MySQL database password, is not set.<br />You should set it in Specific/config.system.php</h3>"); die("<h3>The constant PROJECT_DB_MYSQL_PASSWORD, containing the MySQL database password, is not set.<br />You should set it in Specific/config.system.php</h3>");
} }
try { $GLOBALS["DB"] = new \Flake\Core\Database\Mysql(
$GLOBALS["DB"] = new \Flake\Core\Database\Mysql( PROJECT_DB_MYSQL_HOST,
PROJECT_DB_MYSQL_HOST, PROJECT_DB_MYSQL_DBNAME,
PROJECT_DB_MYSQL_DBNAME, PROJECT_DB_MYSQL_USERNAME,
PROJECT_DB_MYSQL_USERNAME, PROJECT_DB_MYSQL_PASSWORD
PROJECT_DB_MYSQL_PASSWORD );
);
# We now setup the connexion to use UTF8 # We now setup the connection to use UTF8
$GLOBALS["DB"]->query("SET NAMES UTF8"); $GLOBALS["DB"]->query("SET NAMES UTF8");
} catch(\Exception $e) {
#die("<h3>Baïkal was not able to establish a connexion to the configured MySQL database (as configured in Specific/config.system.php).</h3>");
}
return TRUE; return TRUE;
} }