Make import script compatible with Postgresql

This commit is contained in:
Frederic Guillot 2017-01-09 18:05:38 -05:00
parent 31bf0711ee
commit d82d7324af
2 changed files with 17 additions and 4 deletions

View File

@ -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 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 Previous versions
----------------- -----------------

View File

@ -26,6 +26,17 @@ $src->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dst = PicoDb\Database::getInstance('db')->getConnection(); $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) function get_settings(PDO $db)
{ {
$rq = $db->prepare('SELECT * FROM settings'); $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']), md5($settings['username'] . ':' . $settings['fever_token']),
)); ));
return $db->lastInsertId(); return get_last_id($db);
} }
function copy_settings(PDO $db, $user_id, array $settings) 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, 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; return $feed_ids;
@ -171,7 +182,7 @@ function copy_favicons(PDO $db, array $feed_ids, array $favicons, array $favicon
$favicon['type'], $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['title'],
)); ));
$group_ids[$group['id']] = $db->lastInsertId(); $group_ids[$group['id']] = get_last_id($db);
} }
$rq = $db->prepare('INSERT INTO feeds_groups $rq = $db->prepare('INSERT INTO feeds_groups