Merge pull request #443 from fruux/no-standard-sqlite-db

Don't use a provided sqlite database
This commit is contained in:
Evert Pot 2016-02-09 21:48:11 -05:00
commit d6ab8941bc
6 changed files with 66 additions and 35 deletions

View file

@ -18,6 +18,8 @@ ChangeLog
* Fixed: bug when using a MySQL schema name that contains a whitespace.
* Twig is now a composer dependency.
* Moved documentation to sabre.io.
* #381: SQLite database woes. The database is now created from scratch when
installing.
* #320: Allow underscores in calendar/addressbook uris.

View file

@ -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 = "<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) {
@ -162,4 +212,4 @@ class Database extends \Flake\Core\Controller {
$oMorpho->remove("PROJECT_DB_MYSQL_PASSWORD");
}
}
}
}

View file

@ -67,13 +67,6 @@ class Initialize extends \Flake\Core\Controller {
$oSystemConfig->persist();
# Using default PROJECT_SQLITE_FILE
$PROJECT_SQLITE_FILE = PROJECT_PATH_SPECIFIC . "db/db.sqlite";
if(!file_exists($PROJECT_SQLITE_FILE)) {
# Installing default sqlite database
@copy(PROJECT_PATH_CORERESOURCES . "Db/SQLite/db.sqlite", $PROJECT_SQLITE_FILE);
}
}
}
}
@ -121,4 +114,4 @@ class Initialize extends \Flake\Core\Controller {
throw new \Exception("Unable to create " . PROJECT_PATH_SPECIFIC . ".htaccess; you may try to create it manually by copying " . PROJECT_PATH_CORERESOURCES . "System/htaccess-specific");
}
}
}
}

View file

@ -244,18 +244,8 @@ class Framework extends \Flake\Core\Framework {
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
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>");
}
@ -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>");
}
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("<h3>Baïkal was not able to establish a connexion to the configured MySQL database (as configured in Specific/config.system.php).</h3>");
}
# We now setup the connection to use UTF8
$GLOBALS["DB"]->query("SET NAMES UTF8");
return TRUE;
}

Binary file not shown.