From d82d7324afadc6b9f88c0c464d2208c9464be7e3 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Mon, 9 Jan 2017 18:05:38 -0500 Subject: [PATCH] Make import script compatible with Postgresql --- ChangeLog | 2 ++ scripts/migrate-db.php | 19 +++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5246709..5dc47b5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -28,6 +28,8 @@ To import your old database to the new database format, use this script: php scripts/migrate-db.php --sqlite-db=/path/to/my/db.sqlite --admin=1 +The script is located in the Git repository (not in the archive). + Previous versions ----------------- diff --git a/scripts/migrate-db.php b/scripts/migrate-db.php index 939b539..31db83f 100644 --- a/scripts/migrate-db.php +++ b/scripts/migrate-db.php @@ -26,6 +26,17 @@ $src->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dst = PicoDb\Database::getInstance('db')->getConnection(); +function get_last_id(PDO $pdo) +{ + if (DB_DRIVER === 'postgres') { + $rq = $pdo->prepare('SELECT LASTVAL()'); + $rq->execute(); + return $rq->fetchColumn(); + } + + return $pdo->lastInsertId(); +} + function get_settings(PDO $db) { $rq = $db->prepare('SELECT * FROM settings'); @@ -68,7 +79,7 @@ function create_user(PDO $db, array $settings, $is_admin) md5($settings['username'] . ':' . $settings['fever_token']), )); - return $db->lastInsertId(); + return get_last_id($db); } function copy_settings(PDO $db, $user_id, array $settings) @@ -115,7 +126,7 @@ function copy_feeds(PDO $db, $user_id, array $feeds) isset($feed['cloak_referrer']) ? (int) $feed['cloak_referrer'] : 0, )); - $feed_ids[$feed['id']] = $db->lastInsertId(); + $feed_ids[$feed['id']] = get_last_id($db); } return $feed_ids; @@ -171,7 +182,7 @@ function copy_favicons(PDO $db, array $feed_ids, array $favicons, array $favicon $favicon['type'], )); - $favicon_ids[$favicon['id']] = $db->lastInsertId(); + $favicon_ids[$favicon['id']] = get_last_id($db); } } @@ -205,7 +216,7 @@ function copy_groups(PDO $db, $user_id, array $feed_ids, array $groups, array $f $group['title'], )); - $group_ids[$group['id']] = $db->lastInsertId(); + $group_ids[$group['id']] = get_last_id($db); } $rq = $db->prepare('INSERT INTO feeds_groups