From 30165f1f346673dfe773648120143d20b0828acc Mon Sep 17 00:00:00 2001 From: Evert Pot Date: Thu, 4 Feb 2016 03:01:30 -0500 Subject: [PATCH] SQLite database is now recreated when installing. --- .../Controller/Install/Database.php | 58 +++++++++++++++++-- Core/Frameworks/Flake/Framework.php | 32 +++------- Specific/ENABLE_INSTALL | 0 3 files changed, 63 insertions(+), 27 deletions(-) delete mode 100644 Specific/ENABLE_INSTALL diff --git a/Core/Frameworks/BaikalAdmin/Controller/Install/Database.php b/Core/Frameworks/BaikalAdmin/Controller/Install/Database.php index 7ebcdea..d3a1f8b 100644 --- a/Core/Frameworks/BaikalAdmin/Controller/Install/Database.php +++ b/Core/Frameworks/BaikalAdmin/Controller/Install/Database.php @@ -37,7 +37,7 @@ class Database extends \Flake\Core\Controller { $this->oForm = $this->oModel->formForThisModelInstance(array( "close" => FALSE, - "hook.validation" => array($this, "validateMySQLConnection"), + "hook.validation" => array($this, "validateConnection"), "hook.morphology" => array($this, "hideMySQLFieldWhenNeeded"), )); @@ -77,7 +77,7 @@ class Database extends \Flake\Core\Controller { return $oView->render(); } - public function validateMySQLConnection($oForm, $oMorpho) { + public function validateConnection($oForm, $oMorpho) { $bMySQLEnabled = $oMorpho->element("PROJECT_DB_MYSQL")->value(); @@ -141,7 +141,57 @@ class Database extends \Flake\Core\Controller { $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 = "

Database is not structurally complete.

"; + $sMessage .= "

Missing tables are: " . implode(", ", $aMissingTables) . "

"; + $sMessage .= "

You will find the SQL definition of Baïkal tables in this file: Core/Resources/Db/SQLite/db.sql

"; + $sMessage .= "

Nothing has been saved. Please, add these tables to the database before pursuing Baïkal initialization.

"; + + $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.
SQLite says: " . $e->getMessage() . (string)$e + ); + } + // SQLite + } } 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"); } } -} \ No newline at end of file +} diff --git a/Core/Frameworks/Flake/Framework.php b/Core/Frameworks/Flake/Framework.php index e5a8c8d..a96bca2 100644 --- a/Core/Frameworks/Flake/Framework.php +++ b/Core/Frameworks/Flake/Framework.php @@ -244,18 +244,8 @@ class Framework extends \Flake\Core\Framework { return FALSE; } - # Asserting DB file exists - if(!file_exists(PROJECT_SQLITE_FILE)) { - die("

DB file does not exist. To create it, please copy 'Core/Resources/Db/SQLite/db.sqlite' to '" . PROJECT_SQLITE_FILE . "'

"); - } - - # Asserting DB file is readable - if(!is_readable(PROJECT_SQLITE_FILE)) { - die("

DB file is not readable. Please give read permissions on file '" . PROJECT_SQLITE_FILE . "'

"); - } - # Asserting DB file is writable - if(!is_writable(PROJECT_SQLITE_FILE)) { + if(file_exists(PROJECT_SQLITE_FILE) && !is_writable(PROJECT_SQLITE_FILE)) { die("

DB file is not writable. Please give write permissions on file '" . PROJECT_SQLITE_FILE . "'

"); } @@ -290,19 +280,15 @@ class Framework extends \Flake\Core\Framework { die("

The constant PROJECT_DB_MYSQL_PASSWORD, containing the MySQL database password, is not set.
You should set it in Specific/config.system.php

"); } - try { - $GLOBALS["DB"] = new \Flake\Core\Database\Mysql( - PROJECT_DB_MYSQL_HOST, - PROJECT_DB_MYSQL_DBNAME, - PROJECT_DB_MYSQL_USERNAME, - PROJECT_DB_MYSQL_PASSWORD - ); + $GLOBALS["DB"] = new \Flake\Core\Database\Mysql( + PROJECT_DB_MYSQL_HOST, + PROJECT_DB_MYSQL_DBNAME, + PROJECT_DB_MYSQL_USERNAME, + PROJECT_DB_MYSQL_PASSWORD + ); - # We now setup the connexion to use UTF8 - $GLOBALS["DB"]->query("SET NAMES UTF8"); - } catch(\Exception $e) { - #die("

Baïkal was not able to establish a connexion to the configured MySQL database (as configured in Specific/config.system.php).

"); - } + # We now setup the connection to use UTF8 + $GLOBALS["DB"]->query("SET NAMES UTF8"); return TRUE; } diff --git a/Specific/ENABLE_INSTALL b/Specific/ENABLE_INSTALL deleted file mode 100644 index e69de29..0000000