Skip to content

Commit

Permalink
Use const for dbname and dbtableprefix defaults
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
  • Loading branch information
kesselb committed Sep 23, 2018
1 parent a5f0cc6 commit 0ee191b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
10 changes: 8 additions & 2 deletions lib/private/DB/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
* Takes care of creating and configuring Doctrine connections.
*/
class ConnectionFactory {
/** @var string default database name */
const DEFAULT_DBNAME = 'owncloud';

/** @var string default database table prefix */
const DEFAULT_DBTABLEPREFIX = 'oc_';

/**
* @var array
*
Expand Down Expand Up @@ -186,7 +192,7 @@ public function createConnectionParams() {
'user' => $this->config->getValue('dbuser', ''),
'password' => $this->config->getValue('dbpassword', ''),
];
$name = $this->config->getValue('dbname', 'owncloud');
$name = $this->config->getValue('dbname', self::DEFAULT_DBNAME);

if ($this->normalizeType($type) === 'sqlite3') {
$dataDir = $this->config->getValue("datadirectory", \OC::$SERVERROOT . '/data');
Expand All @@ -197,7 +203,7 @@ public function createConnectionParams() {
$connectionParams['dbname'] = $name;
}

$connectionParams['tablePrefix'] = $this->config->getValue('dbtableprefix', 'oc_');
$connectionParams['tablePrefix'] = $this->config->getValue('dbtableprefix', self::DEFAULT_DBTABLEPREFIX);
$connectionParams['sqlite.journal_mode'] = $this->config->getValue('sqlite.journal_mode', 'WAL');

//additional driver options, eg. for mysql ssl
Expand Down
39 changes: 33 additions & 6 deletions lib/private/Setup/Sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,48 @@

namespace OC\Setup;

use OC\DB\ConnectionFactory;

class Sqlite extends AbstractDatabase {
public $dbprettyname = 'Sqlite';

public function validate($config) {
return array();
}

public function initialize($config) {
/*
* Web: When using web based installer its not possible to set dbname
* or dbtableprefix. Defaults used from ConnectionFactory and dbtype = 'sqlite'
* is written to config.php.
*
* Cli: When --database-name or --database-table-prefix empty or default
* dbtype = 'sqlite' is written to config.php. If you choose a value different
* from default these values are written to config.php. This is required because
* in connection factory configuration is obtained from config.php.
*/

$this->dbName = $config['dbname'] ?? ConnectionFactory::DEFAULT_DBNAME;
$this->tablePrefix = $config['dbtableprefix'] ?? ConnectionFactory::DEFAULT_DBTABLEPREFIX;

if ($this->dbName !== ConnectionFactory::DEFAULT_DBNAME) {
$this->config->setValue('dbname', $this->dbName);
}

if ($this->tablePrefix !== ConnectionFactory::DEFAULT_DBTABLEPREFIX) {
$this->config->setValue('dbtableprefix', $this->tablePrefix);
}
}

public function setupDatabase($username) {
$datadir = $this->config->getValue('datadirectory', \OC::$SERVERROOT . '/data');
$datadir = $this->config->getValue(
'datadirectory',
\OC::$SERVERROOT . '/data'
);

//delete the old sqlite database first, might cause infinte loops otherwise
if (file_exists("$datadir/owncloud.db")) {
unlink("$datadir/owncloud.db");
$sqliteFile = $datadir . '/' . $this->dbName . 'db';
if (file_exists($sqliteFile)) {
unlink($sqliteFile);
}
//in case of sqlite, we can always fill the database
error_log("creating sqlite db");
}
}

0 comments on commit 0ee191b

Please sign in to comment.