Improve database migration script and postgres schema

This commit is contained in:
Frederic Guillot 2016-12-26 20:32:58 -05:00
parent 09dd04f698
commit be76f89408
2 changed files with 24 additions and 13 deletions

View File

@ -56,15 +56,15 @@ function version_1(PDO $pdo)
checksum VARCHAR(255) NOT NULL, checksum VARCHAR(255) NOT NULL,
status VARCHAR(10) NOT NULL, status VARCHAR(10) NOT NULL,
bookmark INTEGER DEFAULT 0, bookmark INTEGER DEFAULT 0,
url VARCHAR(255) NOT NULL, url TEXT NOT NULL,
title VARCHAR(255) NOT NULL, title TEXT NOT NULL,
author VARCHAR(255), author VARCHAR(255),
content TEXT, content TEXT,
updated BIGINT, updated BIGINT,
enclosure_url VARCHAR(255), enclosure_url TEXT,
enclosure_type VARCHAR(50), enclosure_type VARCHAR(50),
language VARCHAR(50), language VARCHAR(50),
rtl INTEGER DEFAULT 0, rtl BOOLEAN DEFAULT FALSE,
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)

View File

@ -58,7 +58,7 @@ function create_user(PDO $db, array $settings, $is_admin)
$settings['username'], $settings['username'],
$settings['password'], $settings['password'],
$is_admin, $is_admin,
$settings['last_login'], isset($settings['last_login']) ? $settings['last_login'] : 0,
$settings['api_token'], $settings['api_token'],
$settings['bookmarklet_token'], $settings['bookmarklet_token'],
Helper\generate_token(), Helper\generate_token(),
@ -88,7 +88,7 @@ function copy_settings(PDO $db, $user_id, array $settings)
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)); $rq->execute(array($user_id, $key, $value ?: ''));
} }
} }
} }
@ -108,10 +108,10 @@ function copy_feeds(PDO $db, $user_id, array $feeds)
$feed['feed_url'], $feed['feed_url'],
$feed['site_url'], $feed['site_url'],
$feed['title'], $feed['title'],
$feed['enabled'], $feed['enabled'] ?: 1,
$feed['download_content'], $feed['download_content'] ?: 0,
$feed['rtl'], $feed['rtl'] ?: 0,
$feed['cloak_referrer'], $feed['cloak_referrer'] ?: 0,
)); ));
$feed_ids[$feed['id']] = $db->lastInsertId(); $feed_ids[$feed['id']] = $db->lastInsertId();
@ -134,12 +134,12 @@ function copy_items(PDO $db, $user_id, array $feed_ids, array $items)
$feed_ids[$item['feed_id']], $feed_ids[$item['feed_id']],
$item['id'], $item['id'],
$item['status'], $item['status'],
$item['bookmark'], $item['bookmark'] ?: 0,
$item['url'], $item['url'],
$item['title'], $item['title'],
$item['author'], mb_strlen($item['author']) < 255 ? $item['author'] : '',
$item['content'], $item['content'],
$item['updated'], $item['updated'] ?: 0,
$item['enclosure'], $item['enclosure'],
$item['enclosure_type'], $item['enclosure_type'],
$item['language'], $item['language'],
@ -232,11 +232,22 @@ $favicons_feeds = get_table($src, 'favicons_feeds');
try { try {
$dst->beginTransaction(); $dst->beginTransaction();
echo '* Create user'.PHP_EOL;
$user_id = create_user($dst, $settings, $is_admin); $user_id = create_user($dst, $settings, $is_admin);
echo '* Copy user settings'.PHP_EOL;
copy_settings($dst, $user_id, $settings); copy_settings($dst, $user_id, $settings);
echo '* Copy feeds'.PHP_EOL;
$feed_ids = copy_feeds($dst, $user_id, $feeds); $feed_ids = copy_feeds($dst, $user_id, $feeds);
echo '* Copy items'.PHP_EOL;
copy_items($dst, $user_id, $feed_ids, $items); copy_items($dst, $user_id, $feed_ids, $items);
echo '* Copy favicons'.PHP_EOL;
copy_favicons($dst, $feed_ids, $favicons, $favicons_feeds); copy_favicons($dst, $feed_ids, $favicons, $favicons_feeds);
echo '* Copy groups'.PHP_EOL;
copy_groups($dst, $user_id, $feed_ids, $groups, $feeds_groups); copy_groups($dst, $user_id, $feed_ids, $groups, $feeds_groups);
$dst->commit(); $dst->commit();