From 32793ce7c00e41ce0f42fffcb67edef36237462c Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Wed, 11 Jan 2017 20:08:39 -0500 Subject: [PATCH] Improve Mysql implementation --- app/core/database.php | 1 + app/schemas/mysql.php | 62 +++++++++++++----------------- scripts/migrate-db.php | 20 +++++----- tests/functional/BaseApiTest.php | 2 +- tests/phpunit.functional.mysql.xml | 1 + tests/phpunit.unit.mysql.xml | 1 + tests/unit/ItemModelTest.php | 2 +- 7 files changed, 42 insertions(+), 47 deletions(-) diff --git a/app/core/database.php b/app/core/database.php index 55ca8cf..addfc59 100644 --- a/app/core/database.php +++ b/app/core/database.php @@ -47,6 +47,7 @@ function get_connection_parameters() 'password' => DB_PASSWORD, 'database' => DB_NAME, 'port' => DB_PORT, + 'charset' => 'utf8', ); } else { require_once __DIR__.'/../schemas/sqlite.php'; diff --git a/app/schemas/mysql.php b/app/schemas/mysql.php index ff7e4ea..a7d44ed 100644 --- a/app/schemas/mysql.php +++ b/app/schemas/mysql.php @@ -5,25 +5,15 @@ namespace Miniflux\Schema; use PDO; use Miniflux\Helper; -const VERSION = 3; - -function version_3(PDO $pdo) -{ - $pdo->exec('ALTER TABLE feeds ADD COLUMN expiration BIGINT DEFAULT 0'); -} - -function version_2(PDO $pdo) -{ - $pdo->exec('ALTER TABLE feeds ADD COLUMN parsing_error_message VARCHAR(255)'); -} +const VERSION = 1; function version_1(PDO $pdo) { - $pdo->exec('CREATE TABLE users ( + $pdo->exec("CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL, - is_admin TINYINT(1) DEFAULT FALSE, + is_admin TINYINT(1) DEFAULT '0', last_login BIGINT, api_token VARCHAR(255) NOT NULL UNIQUE, bookmarklet_token VARCHAR(255) NOT NULL UNIQUE, @@ -31,35 +21,37 @@ function version_1(PDO $pdo) feed_token VARCHAR(255) NOT NULL UNIQUE, fever_token VARCHAR(255) NOT NULL UNIQUE, fever_api_key VARCHAR(255) NOT NULL UNIQUE - )'); + ) ENGINE=InnoDB CHARSET=utf8"); - $pdo->exec('CREATE TABLE user_settings ( + $pdo->exec("CREATE TABLE user_settings ( `user_id` INT NOT NULL, `key` VARCHAR(255) NOT NULL, `value` TEXT NOT NULL, PRIMARY KEY(`user_id`, `key`), FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE - )'); + ) ENGINE=InnoDB CHARSET=utf8"); - $pdo->exec('CREATE TABLE feeds ( + $pdo->exec("CREATE TABLE feeds ( id BIGINT AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL, feed_url VARCHAR(255) NOT NULL, site_url VARCHAR(255), title VARCHAR(255) NOT NULL, + expiration BIGINT DEFAULT 0, last_checked BIGINT DEFAULT 0, last_modified VARCHAR(255), etag VARCHAR(255), enabled TINYINT(1) DEFAULT TRUE, - download_content TINYINT(1) DEFAULT FALSE, + download_content TINYINT(1) DEFAULT '0', parsing_error INT DEFAULT 0, - rtl TINYINT(1) DEFAULT FALSE, - cloak_referrer TINYINT(1) DEFAULT FALSE, + parsing_error_message VARCHAR(255), + rtl TINYINT(1) DEFAULT '0', + cloak_referrer TINYINT(1) DEFAULT '0', FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, UNIQUE(user_id, feed_url) - )'); + ) ENGINE=InnoDB CHARSET=utf8"); - $pdo->exec('CREATE TABLE items ( + $pdo->exec("CREATE TABLE items ( id BIGINT AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL, feed_id BIGINT NOT NULL, @@ -69,48 +61,48 @@ function version_1(PDO $pdo) url TEXT NOT NULL, title TEXT NOT NULL, author TEXT, - content TEXT, + content LONGTEXT, updated BIGINT, enclosure_url TEXT, enclosure_type VARCHAR(50), language VARCHAR(50), - rtl TINYINT(1) DEFAULT FALSE, + rtl TINYINT(1) DEFAULT '0', FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, FOREIGN KEY(feed_id) REFERENCES feeds(id) ON DELETE CASCADE, UNIQUE(feed_id, checksum) - )'); + ) ENGINE=InnoDB CHARSET=utf8"); - $pdo->exec('CREATE TABLE `groups` ( + $pdo->exec("CREATE TABLE `groups` ( id INT AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL, title VARCHAR(255) NOT NULL, FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, UNIQUE(user_id, title) - )'); + ) ENGINE=InnoDB CHARSET=utf8"); - $pdo->exec('CREATE TABLE `feeds_groups` ( + $pdo->exec("CREATE TABLE `feeds_groups` ( feed_id BIGINT NOT NULL, group_id INT NOT NULL, PRIMARY KEY(feed_id, group_id), FOREIGN KEY(group_id) REFERENCES groups(id) ON DELETE CASCADE, FOREIGN KEY(feed_id) REFERENCES feeds(id) ON DELETE CASCADE - )'); + ) ENGINE=InnoDB CHARSET=utf8"); - $pdo->exec('CREATE TABLE favicons ( + $pdo->exec("CREATE TABLE favicons ( id INT AUTO_INCREMENT PRIMARY KEY, hash VARCHAR(255) UNIQUE, type VARCHAR(50) - )'); + ) ENGINE=InnoDB CHARSET=utf8"); - $pdo->exec('CREATE TABLE `favicons_feeds` ( + $pdo->exec("CREATE TABLE `favicons_feeds` ( feed_id BIGINT NOT NULL, favicon_id INT NOT NULL, PRIMARY KEY(feed_id, favicon_id), FOREIGN KEY(favicon_id) REFERENCES favicons(id) ON DELETE CASCADE, FOREIGN KEY(feed_id) REFERENCES feeds(id) ON DELETE CASCADE - )'); + ) ENGINE=InnoDB CHARSET=utf8"); - $pdo->exec('CREATE TABLE remember_me ( + $pdo->exec("CREATE TABLE remember_me ( id INT AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL, ip VARCHAR(255), @@ -120,7 +112,7 @@ function version_1(PDO $pdo) expiration BIGINT, date_creation BIGINT, FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE - )'); + ) ENGINE=InnoDB CHARSET=utf8"); $fever_token = Helper\generate_token(); $rq = $pdo->prepare(' diff --git a/scripts/migrate-db.php b/scripts/migrate-db.php index a02e836..dd8106f 100644 --- a/scripts/migrate-db.php +++ b/scripts/migrate-db.php @@ -23,7 +23,9 @@ $is_admin = isset($options['admin']) ? (int) $options['admin'] : 0; $src = new PDO('sqlite:' . $src_file); $src->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); -$dst = PicoDb\Database::getInstance('db')->getConnection(); + +$db = PicoDb\Database::getInstance('db'); +$dst = $db->getConnection(); function get_last_id(PDO $pdo) @@ -32,10 +34,6 @@ function get_last_id(PDO $pdo) $rq = $pdo->prepare('SELECT LASTVAL()'); $rq->execute(); return $rq->fetchColumn(); - } elseif (DB_DRIVER === 'mysql') { - $rq = $pdo->prepare('SELECT LAST_INSERT_ID()'); - $rq->execute(); - return $rq->fetchColumn(); } return $pdo->lastInsertId(); @@ -86,7 +84,7 @@ function create_user(PDO $db, array $settings, $is_admin) return get_last_id($db); } -function copy_settings(PDO $db, $user_id, array $settings) +function copy_settings(\PicoDb\Database $db, $user_id, array $settings) { $exclude_keys = array( 'username', @@ -100,11 +98,13 @@ function copy_settings(PDO $db, $user_id, array $settings) 'auto_update_url', ); - $rq = $db->prepare('INSERT INTO user_settings ("user_id", "key", "value") VALUES (?, ?, ?)'); - foreach ($settings as $key => $value) { if (! in_array($key, $exclude_keys)) { - $rq->execute(array($user_id, $key, $value ?: '')); + $db->table('user_settings')->insert(array( + 'user_id' => $user_id, + 'key' => $key, + 'value' => $value ?: '', + )); } } } @@ -252,7 +252,7 @@ try { $user_id = create_user($dst, $settings, $is_admin); echo '* Copy user settings'.PHP_EOL; - copy_settings($dst, $user_id, $settings); + copy_settings($db, $user_id, $settings); echo '* Copy feeds'.PHP_EOL; $feed_ids = copy_feeds($dst, $user_id, $feeds); diff --git a/tests/functional/BaseApiTest.php b/tests/functional/BaseApiTest.php index 17548c8..d5848b7 100644 --- a/tests/functional/BaseApiTest.php +++ b/tests/functional/BaseApiTest.php @@ -19,7 +19,7 @@ abstract class BaseApiTest extends PHPUnit_Framework_TestCase } else if (DB_DRIVER === 'mysql') { $pdo = new PDO('mysql:host='.DB_HOSTNAME, DB_USERNAME, DB_PASSWORD); $stmt = $pdo->query("SELECT information_schema.processlist.id FROM information_schema.processlist WHERE information_schema.processlist.DB = '".DB_NAME."' AND id <> connection_id()"); - while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { + while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $pdo->exec('KILL '.$row['id']); } $pdo->exec('DROP DATABASE '.DB_NAME); diff --git a/tests/phpunit.functional.mysql.xml b/tests/phpunit.functional.mysql.xml index b5c6655..4916546 100644 --- a/tests/phpunit.functional.mysql.xml +++ b/tests/phpunit.functional.mysql.xml @@ -9,6 +9,7 @@ + diff --git a/tests/phpunit.unit.mysql.xml b/tests/phpunit.unit.mysql.xml index ba82901..fce20bb 100644 --- a/tests/phpunit.unit.mysql.xml +++ b/tests/phpunit.unit.mysql.xml @@ -6,6 +6,7 @@ + diff --git a/tests/unit/ItemModelTest.php b/tests/unit/ItemModelTest.php index 9ad3cbe..b29ea4d 100644 --- a/tests/unit/ItemModelTest.php +++ b/tests/unit/ItemModelTest.php @@ -20,7 +20,7 @@ class ItemModelTest extends BaseTest $this->assertEquals('some url', $item['url']); $this->assertEquals('some content', $item['content']); $this->assertEquals(Model\Item\STATUS_UNREAD, $item['status']); - $this->assertEquals(time(), $item['updated'], '', 1); + $this->assertEquals(time(), $item['updated'], '', 2); $this->assertEquals('', $item['author']); $this->assertEquals(0, $item['bookmark']); $this->assertEquals('', $item['enclosure_url']);