Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use a provided sqlite database #443

Merged
merged 5 commits into from
Feb 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.


Expand Down
58 changes: 54 additions & 4 deletions Core/Frameworks/BaikalAdmin/Controller/Install/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function execute() {

$this->oForm = $this->oModel->formForThisModelInstance(array(
"close" => FALSE,
"hook.validation" => array($this, "validateMySQLConnection"),
"hook.validation" => array($this, "validateConnection"),
"hook.morphology" => array($this, "hideMySQLFieldWhenNeeded"),
));

Expand Down Expand Up @@ -77,7 +77,7 @@ public function render() {
return $oView->render();
}

public function validateMySQLConnection($oForm, $oMorpho) {
public function validateConnection($oForm, $oMorpho) {

$bMySQLEnabled = $oMorpho->element("PROJECT_DB_MYSQL")->value();

Expand Down Expand Up @@ -141,7 +141,57 @@ public function validateMySQLConnection($oForm, $oMorpho) {
$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) {
Expand All @@ -162,4 +212,4 @@ public function hideMySQLFieldWhenNeeded(\Formal\Form $oForm, \Formal\Form\Morph
$oMorpho->remove("PROJECT_DB_MYSQL_PASSWORD");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,6 @@ public function execute() {

$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);
}
}
}
}
Expand Down Expand Up @@ -121,4 +114,4 @@ protected function createHtaccessFilesIfNeeded() {
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");
}
}
}
}
34 changes: 10 additions & 24 deletions Core/Frameworks/Flake/Framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,8 @@ protected static function initDbSqlite() {
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>");
}

Expand Down Expand Up @@ -290,19 +280,15 @@ protected static function initDbMysql() {
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
);

# 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>");
}
$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 connection to use UTF8
$GLOBALS["DB"]->query("SET NAMES UTF8");

return TRUE;
}
Expand Down
Binary file removed Core/Resources/Db/SQLite/db.sqlite
Binary file not shown.
Empty file removed Specific/ENABLE_INSTALL
Empty file.