Improve Mysql implementation

This commit is contained in:
Frederic Guillot 2017-01-11 20:08:39 -05:00
parent 5b3ab0036e
commit 32793ce7c0
7 changed files with 42 additions and 47 deletions

View File

@ -47,6 +47,7 @@ function get_connection_parameters()
'password' => DB_PASSWORD, 'password' => DB_PASSWORD,
'database' => DB_NAME, 'database' => DB_NAME,
'port' => DB_PORT, 'port' => DB_PORT,
'charset' => 'utf8',
); );
} else { } else {
require_once __DIR__.'/../schemas/sqlite.php'; require_once __DIR__.'/../schemas/sqlite.php';

View File

@ -5,25 +5,15 @@ namespace Miniflux\Schema;
use PDO; use PDO;
use Miniflux\Helper; use Miniflux\Helper;
const VERSION = 3; const VERSION = 1;
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)');
}
function version_1(PDO $pdo) function version_1(PDO $pdo)
{ {
$pdo->exec('CREATE TABLE users ( $pdo->exec("CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY, id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE, username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL,
is_admin TINYINT(1) DEFAULT FALSE, is_admin TINYINT(1) DEFAULT '0',
last_login BIGINT, last_login BIGINT,
api_token VARCHAR(255) NOT NULL UNIQUE, api_token VARCHAR(255) NOT NULL UNIQUE,
bookmarklet_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, feed_token VARCHAR(255) NOT NULL UNIQUE,
fever_token VARCHAR(255) NOT NULL UNIQUE, fever_token VARCHAR(255) NOT NULL UNIQUE,
fever_api_key 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, `user_id` INT NOT NULL,
`key` VARCHAR(255) NOT NULL, `key` VARCHAR(255) NOT NULL,
`value` TEXT NOT NULL, `value` TEXT NOT NULL,
PRIMARY KEY(`user_id`, `key`), PRIMARY KEY(`user_id`, `key`),
FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE 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, id BIGINT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL, user_id INT NOT NULL,
feed_url VARCHAR(255) NOT NULL, feed_url VARCHAR(255) NOT NULL,
site_url VARCHAR(255), site_url VARCHAR(255),
title VARCHAR(255) NOT NULL, title VARCHAR(255) NOT NULL,
expiration BIGINT DEFAULT 0,
last_checked BIGINT DEFAULT 0, last_checked BIGINT DEFAULT 0,
last_modified VARCHAR(255), last_modified VARCHAR(255),
etag VARCHAR(255), etag VARCHAR(255),
enabled TINYINT(1) DEFAULT TRUE, enabled TINYINT(1) DEFAULT TRUE,
download_content TINYINT(1) DEFAULT FALSE, download_content TINYINT(1) DEFAULT '0',
parsing_error INT DEFAULT 0, parsing_error INT DEFAULT 0,
rtl TINYINT(1) DEFAULT FALSE, parsing_error_message VARCHAR(255),
cloak_referrer TINYINT(1) DEFAULT FALSE, rtl TINYINT(1) DEFAULT '0',
cloak_referrer TINYINT(1) DEFAULT '0',
FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE,
UNIQUE(user_id, feed_url) 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, id BIGINT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL, user_id INT NOT NULL,
feed_id BIGINT NOT NULL, feed_id BIGINT NOT NULL,
@ -69,48 +61,48 @@ function version_1(PDO $pdo)
url TEXT NOT NULL, url TEXT NOT NULL,
title TEXT NOT NULL, title TEXT NOT NULL,
author TEXT, author TEXT,
content TEXT, content LONGTEXT,
updated BIGINT, updated BIGINT,
enclosure_url TEXT, enclosure_url TEXT,
enclosure_type VARCHAR(50), enclosure_type VARCHAR(50),
language 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(user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY(feed_id) REFERENCES feeds(id) ON DELETE CASCADE, FOREIGN KEY(feed_id) REFERENCES feeds(id) ON DELETE CASCADE,
UNIQUE(feed_id, checksum) UNIQUE(feed_id, checksum)
)'); ) ENGINE=InnoDB CHARSET=utf8");
$pdo->exec('CREATE TABLE `groups` ( $pdo->exec("CREATE TABLE `groups` (
id INT AUTO_INCREMENT PRIMARY KEY, id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL, user_id INT NOT NULL,
title VARCHAR(255) NOT NULL, title VARCHAR(255) NOT NULL,
FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE,
UNIQUE(user_id, title) 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, feed_id BIGINT NOT NULL,
group_id INT NOT NULL, group_id INT NOT NULL,
PRIMARY KEY(feed_id, group_id), PRIMARY KEY(feed_id, group_id),
FOREIGN KEY(group_id) REFERENCES groups(id) ON DELETE CASCADE, FOREIGN KEY(group_id) REFERENCES groups(id) ON DELETE CASCADE,
FOREIGN KEY(feed_id) REFERENCES feeds(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, id INT AUTO_INCREMENT PRIMARY KEY,
hash VARCHAR(255) UNIQUE, hash VARCHAR(255) UNIQUE,
type VARCHAR(50) type VARCHAR(50)
)'); ) ENGINE=InnoDB CHARSET=utf8");
$pdo->exec('CREATE TABLE `favicons_feeds` ( $pdo->exec("CREATE TABLE `favicons_feeds` (
feed_id BIGINT NOT NULL, feed_id BIGINT NOT NULL,
favicon_id INT NOT NULL, favicon_id INT NOT NULL,
PRIMARY KEY(feed_id, favicon_id), PRIMARY KEY(feed_id, favicon_id),
FOREIGN KEY(favicon_id) REFERENCES favicons(id) ON DELETE CASCADE, FOREIGN KEY(favicon_id) REFERENCES favicons(id) ON DELETE CASCADE,
FOREIGN KEY(feed_id) REFERENCES feeds(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, id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL, user_id INT NOT NULL,
ip VARCHAR(255), ip VARCHAR(255),
@ -120,7 +112,7 @@ function version_1(PDO $pdo)
expiration BIGINT, expiration BIGINT,
date_creation BIGINT, date_creation BIGINT,
FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
)'); ) ENGINE=InnoDB CHARSET=utf8");
$fever_token = Helper\generate_token(); $fever_token = Helper\generate_token();
$rq = $pdo->prepare(' $rq = $pdo->prepare('

View File

@ -23,7 +23,9 @@ $is_admin = isset($options['admin']) ? (int) $options['admin'] : 0;
$src = new PDO('sqlite:' . $src_file); $src = new PDO('sqlite:' . $src_file);
$src->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $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) function get_last_id(PDO $pdo)
@ -32,10 +34,6 @@ function get_last_id(PDO $pdo)
$rq = $pdo->prepare('SELECT LASTVAL()'); $rq = $pdo->prepare('SELECT LASTVAL()');
$rq->execute(); $rq->execute();
return $rq->fetchColumn(); return $rq->fetchColumn();
} elseif (DB_DRIVER === 'mysql') {
$rq = $pdo->prepare('SELECT LAST_INSERT_ID()');
$rq->execute();
return $rq->fetchColumn();
} }
return $pdo->lastInsertId(); return $pdo->lastInsertId();
@ -86,7 +84,7 @@ function create_user(PDO $db, array $settings, $is_admin)
return get_last_id($db); 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( $exclude_keys = array(
'username', 'username',
@ -100,11 +98,13 @@ function copy_settings(PDO $db, $user_id, array $settings)
'auto_update_url', 'auto_update_url',
); );
$rq = $db->prepare('INSERT INTO user_settings ("user_id", "key", "value") VALUES (?, ?, ?)');
foreach ($settings as $key => $value) { foreach ($settings as $key => $value) {
if (! in_array($key, $exclude_keys)) { 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); $user_id = create_user($dst, $settings, $is_admin);
echo '* Copy user settings'.PHP_EOL; echo '* Copy user settings'.PHP_EOL;
copy_settings($dst, $user_id, $settings); copy_settings($db, $user_id, $settings);
echo '* Copy feeds'.PHP_EOL; echo '* Copy feeds'.PHP_EOL;
$feed_ids = copy_feeds($dst, $user_id, $feeds); $feed_ids = copy_feeds($dst, $user_id, $feeds);

View File

@ -19,7 +19,7 @@ abstract class BaseApiTest extends PHPUnit_Framework_TestCase
} else if (DB_DRIVER === 'mysql') { } else if (DB_DRIVER === 'mysql') {
$pdo = new PDO('mysql:host='.DB_HOSTNAME, DB_USERNAME, DB_PASSWORD); $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()"); $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('KILL '.$row['id']);
} }
$pdo->exec('DROP DATABASE '.DB_NAME); $pdo->exec('DROP DATABASE '.DB_NAME);

View File

@ -9,6 +9,7 @@
<const name="FEVER_API_URL" value="http://127.0.0.1/fever/" /> <const name="FEVER_API_URL" value="http://127.0.0.1/fever/" />
<const name="DB_DRIVER" value="mysql" /> <const name="DB_DRIVER" value="mysql" />
<const name="DB_USERNAME" value="root" /> <const name="DB_USERNAME" value="root" />
<const name="DB_HOSTNAME" value="127.0.0.1" />
<const name="DB_PASSWORD" value="" /> <const name="DB_PASSWORD" value="" />
<const name="DB_NAME" value="miniflux_functional_test" /> <const name="DB_NAME" value="miniflux_functional_test" />
</php> </php>

View File

@ -6,6 +6,7 @@
</testsuites> </testsuites>
<php> <php>
<const name="DB_DRIVER" value="mysql" /> <const name="DB_DRIVER" value="mysql" />
<const name="DB_HOSTNAME" value="127.0.0.1" />
<const name="DB_USERNAME" value="root" /> <const name="DB_USERNAME" value="root" />
<const name="DB_PASSWORD" value="" /> <const name="DB_PASSWORD" value="" />
<const name="DB_NAME" value="miniflux_unit_test" /> <const name="DB_NAME" value="miniflux_unit_test" />

View File

@ -20,7 +20,7 @@ class ItemModelTest extends BaseTest
$this->assertEquals('some url', $item['url']); $this->assertEquals('some url', $item['url']);
$this->assertEquals('some content', $item['content']); $this->assertEquals('some content', $item['content']);
$this->assertEquals(Model\Item\STATUS_UNREAD, $item['status']); $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('', $item['author']);
$this->assertEquals(0, $item['bookmark']); $this->assertEquals(0, $item['bookmark']);
$this->assertEquals('', $item['enclosure_url']); $this->assertEquals('', $item['enclosure_url']);