From 2b8b5937f2291c3655311938ecb5615661513af7 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Thu, 12 Sep 2019 11:56:37 +0200 Subject: [PATCH] Remove special case for SQLite More uniform logic for the 3 databases. Fix wrong DROP TABLE for SQLite. --- app/Models/DatabaseDAO.php | 12 ++++++----- app/Models/UserDAO.php | 37 ++++++++++++++++++++++++---------- app/SQL/install.sql.sqlite.php | 10 ++++++++- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/app/Models/DatabaseDAO.php b/app/Models/DatabaseDAO.php index b8dc56c24..f61bed1ed 100644 --- a/app/Models/DatabaseDAO.php +++ b/app/Models/DatabaseDAO.php @@ -187,6 +187,9 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo { $error = 'Error: SQLite import file is not readable: ' . $filename; } elseif ($clearFirst) { $userDAO->deleteUser(); + if ($this->bd->dbType() === 'sqlite') { + $this->optimize(); //We cannot just delete the .sqlite file otherwise PDO gets buggy + } } else { $nbEntries = $entryDAO->countUnreadRead(); if (!empty($nbEntries['all'])) { @@ -194,11 +197,6 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo { break; } } - if ($this->bd->dbType() === 'sqlite') { - //For importing to SQLite, we can just copy the SQLite source (issues when using another PDO) - copy($filename, join_path(DATA_PATH, 'users', $this->current_user, 'db.sqlite')); - goto done; - } break; default: $error = 'Invalid copy mode!'; @@ -244,6 +242,10 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo { $idMaps = []; + if (defined('STDERR')) { + fwrite(STDERR, "Start SQL copy…\n"); + } + $userTo->createUser(); $catTo->beginTransaction(); diff --git a/app/Models/UserDAO.php b/app/Models/UserDAO.php index 694a489c6..9c0283225 100644 --- a/app/Models/UserDAO.php +++ b/app/Models/UserDAO.php @@ -51,27 +51,42 @@ class FreshRSS_UserDAO extends Minz_ModelPdo { return true; } else { $info = empty($stm) ? array(2 => 'syntax error') : $stm->errorInfo(); - Minz_Log::error('SQL error: ' . $info[2]); + Minz_Log::error(__METHOD__ . ' error: ' . $info[2]); return false; } } public function deleteUser() { + if (defined('STDERR')) { + fwrite(STDERR, 'Deleting SQL data for user “' . $this->current_user . "”…\n"); + } + require_once(APP_PATH . '/SQL/install.sql.' . $this->bd->dbType() . '.php'); - if ($this->bd->dbType() === 'sqlite') { - return unlink(USERS_PATH . '/' . $this->current_user . '/db.sqlite'); - } else { - $sql = sprintf(SQL_DROP_TABLES, $this->prefix); + $ok = false; + if (defined('SQL_DROP_TABLES')) { //E.g. MySQL + $sql = SQL_DROP_TABLES; $stm = $this->bd->prepare($sql); - if ($stm && $stm->execute()) { - return true; - } else { - $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo(); - Minz_Log::error('SQL error : ' . $info[2]); - return false; + $ok = $stm && $stm->execute(); + } else { //E.g. SQLite + global $SQL_DROP_TABLES; + if (is_array($SQL_DROP_TABLES)) { + $instructions = $SQL_DROP_TABLES; + $ok = !empty($instructions); + foreach ($instructions as $sql) { + $stm = $this->bd->prepare($sql); + $ok &= ($stm && $stm->execute()); + } } } + + if ($ok) { + return true; + } else { + $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo(); + Minz_Log::error(__METHOD__ . ' error: ' . $info[2]); + return false; + } } public static function exists($username) { diff --git a/app/SQL/install.sql.sqlite.php b/app/SQL/install.sql.sqlite.php index dbd111e44..88de84358 100644 --- a/app/SQL/install.sql.sqlite.php +++ b/app/SQL/install.sql.sqlite.php @@ -105,4 +105,12 @@ define( VALUES(:url, 1, :name, :website, :description, 86400);' ); -define('SQL_DROP_TABLES', 'DROP TABLE IF EXISTS `entrytag`, `tag`, `entrytmp`, `entry`, `feed`, `category`'); +global $SQL_DROP_TABLES; +$SQL_DROP_TABLES = [ + 'DROP TABLE IF EXISTS `entrytag`', + 'DROP TABLE IF EXISTS `tag`', + 'DROP TABLE IF EXISTS `entrytmp`', + 'DROP TABLE IF EXISTS `entry`', + 'DROP TABLE IF EXISTS `feed`', + 'DROP TABLE IF EXISTS `category`', +];