Browse Source

db: Adjust some db migrations to be compatible with postgres

Needed to change a couple of migrations. The changes are mostly innocuous:

 - changing BLOB to TEXT for short_channel_ids which is the correct type
   anyway, and sqlite3 treats them the same anyway.
 - Use `int` for version since the byte representation is checked by postgres.
 - Change anything that is INT, but will be bound to u64 to BIGINT (again
   postgres checks these more carefully than sqlite3).

Two migrations were replaced with dummy values, since they are buried deep
enough, and I found no portable way of expressing `strftime()` and `INSERT OR
IGNORE`.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
travis-debug
Christian Decker 5 years ago
committed by Rusty Russell
parent
commit
db6abda3a4
  1. 24
      wallet/db.c

24
wallet/db.c

@ -65,7 +65,7 @@ static struct migration dbmigrations[] = {
{SQL("CREATE TABLE channels (" {SQL("CREATE TABLE channels ("
" id INTEGER," /* chan->id */ " id INTEGER," /* chan->id */
" peer_id INTEGER REFERENCES peers(id) ON DELETE CASCADE," " peer_id INTEGER REFERENCES peers(id) ON DELETE CASCADE,"
" short_channel_id BLOB," " short_channel_id TEXT,"
" channel_config_local INTEGER," " channel_config_local INTEGER,"
" channel_config_remote INTEGER," " channel_config_remote INTEGER,"
" state INTEGER," " state INTEGER,"
@ -159,7 +159,7 @@ static struct migration dbmigrations[] = {
");"), ");"),
NULL}, NULL},
/* Add expiry field to invoices (effectively infinite). */ /* Add expiry field to invoices (effectively infinite). */
{SQL("ALTER TABLE invoices ADD expiry_time INTEGER;"), NULL}, {SQL("ALTER TABLE invoices ADD expiry_time BIGINT;"), NULL},
{SQL("UPDATE invoices SET expiry_time=9223372036854775807;"), NULL}, {SQL("UPDATE invoices SET expiry_time=9223372036854775807;"), NULL},
/* Add pay_index field to paid invoices (initially, same order as id). */ /* Add pay_index field to paid invoices (initially, same order as id). */
{SQL("ALTER TABLE invoices ADD pay_index INTEGER;"), NULL}, {SQL("ALTER TABLE invoices ADD pay_index INTEGER;"), NULL},
@ -168,7 +168,7 @@ static struct migration dbmigrations[] = {
{SQL("UPDATE invoices SET pay_index=id WHERE state=1;"), {SQL("UPDATE invoices SET pay_index=id WHERE state=1;"),
NULL}, /* only paid invoice */ NULL}, /* only paid invoice */
/* Create next_pay_index variable (highest pay_index). */ /* Create next_pay_index variable (highest pay_index). */
{SQL("INSERT OR REPLACE INTO vars(name, val)" {SQL("INSERT INTO vars(name, val)"
" VALUES('next_pay_index', " " VALUES('next_pay_index', "
" COALESCE((SELECT MAX(pay_index) FROM invoices WHERE state=1), 0) " " COALESCE((SELECT MAX(pay_index) FROM invoices WHERE state=1), 0) "
"+ 1" "+ 1"
@ -178,8 +178,7 @@ static struct migration dbmigrations[] = {
* This fails for channels still awaiting lockin, but that only applies to * This fails for channels still awaiting lockin, but that only applies to
* pre-release software, so it's forgivable. */ * pre-release software, so it's forgivable. */
{SQL("ALTER TABLE channels ADD first_blocknum INTEGER;"), NULL}, {SQL("ALTER TABLE channels ADD first_blocknum INTEGER;"), NULL},
{SQL("UPDATE channels SET first_blocknum=CAST(short_channel_id AS INTEGER) " {SQL("UPDATE channels SET first_blocknum=1 WHERE short_channel_id IS NOT NULL;"),
"WHERE short_channel_id IS NOT NULL;"),
NULL}, NULL},
{SQL("ALTER TABLE outputs ADD COLUMN channel_id INTEGER;"), NULL}, {SQL("ALTER TABLE outputs ADD COLUMN channel_id INTEGER;"), NULL},
{SQL("ALTER TABLE outputs ADD COLUMN peer_id BLOB;"), NULL}, {SQL("ALTER TABLE outputs ADD COLUMN peer_id BLOB;"), NULL},
@ -215,7 +214,7 @@ static struct migration dbmigrations[] = {
* invoices to current time. */ * invoices to current time. */
{SQL("ALTER TABLE invoices ADD paid_timestamp INTEGER;"), NULL}, {SQL("ALTER TABLE invoices ADD paid_timestamp INTEGER;"), NULL},
{SQL("UPDATE invoices" {SQL("UPDATE invoices"
" SET paid_timestamp = strftime('%s', 'now')" " SET paid_timestamp = CURRENT_TIMESTAMP()"
" WHERE state = 1;"), " WHERE state = 1;"),
NULL}, NULL},
/* We need to keep the route node pubkeys and short channel ids to /* We need to keep the route node pubkeys and short channel ids to
@ -296,7 +295,7 @@ static struct migration dbmigrations[] = {
NULL}, /* erring_index */ NULL}, /* erring_index */
{SQL("ALTER TABLE payments ADD failcode INTEGER;"), NULL}, /* failcode */ {SQL("ALTER TABLE payments ADD failcode INTEGER;"), NULL}, /* failcode */
{SQL("ALTER TABLE payments ADD failnode BLOB;"), NULL}, /* erring_node */ {SQL("ALTER TABLE payments ADD failnode BLOB;"), NULL}, /* erring_node */
{SQL("ALTER TABLE payments ADD failchannel BLOB;"), {SQL("ALTER TABLE payments ADD failchannel TEXT;"),
NULL}, /* erring_channel */ NULL}, /* erring_channel */
{SQL("ALTER TABLE payments ADD failupdate BLOB;"), {SQL("ALTER TABLE payments ADD failupdate BLOB;"),
NULL}, /* channel_update - can be NULL*/ NULL}, /* channel_update - can be NULL*/
@ -331,7 +330,7 @@ static struct migration dbmigrations[] = {
/* Delete dangling utxoset entries due to Issue #1280 */ /* Delete dangling utxoset entries due to Issue #1280 */
{SQL("DELETE FROM utxoset WHERE blockheight IN (" {SQL("DELETE FROM utxoset WHERE blockheight IN ("
" SELECT DISTINCT(blockheight)" " SELECT DISTINCT(blockheight)"
" FROM utxoset LEFT OUTER JOIN blocks on (blockheight == " " FROM utxoset LEFT OUTER JOIN blocks on (blockheight = "
"blocks.height) " "blocks.height) "
" WHERE blocks.hash IS NULL" " WHERE blocks.hash IS NULL"
");"), ");"),
@ -393,8 +392,9 @@ static struct migration dbmigrations[] = {
/* Now make sure we have the lower bound block with the first_blocknum /* Now make sure we have the lower bound block with the first_blocknum
* height. This may introduce a block with NULL height if we didn't have any * height. This may introduce a block with NULL height if we didn't have any
* blocks, remove that in the next. */ * blocks, remove that in the next. */
{SQL("INSERT OR IGNORE INTO blocks (height) VALUES ((SELECT " {SQL("INSERT INTO blocks (height) VALUES ((SELECT "
"MIN(first_blocknum) FROM channels));"), "MIN(first_blocknum) FROM channels)) "
"ON CONFLICT(height) DO NOTHING;"),
NULL}, NULL},
{SQL("DELETE FROM blocks WHERE height IS NULL;"), NULL}, {SQL("DELETE FROM blocks WHERE height IS NULL;"), NULL},
/* -- End of PR #1398 -- */ /* -- End of PR #1398 -- */
@ -746,7 +746,7 @@ static void db_migrate(struct lightningd *ld, struct db *db, struct log *log)
/* Finally update the version number in the version table */ /* Finally update the version number in the version table */
stmt = db_prepare_v2(db, SQL("UPDATE version SET version=?;")); stmt = db_prepare_v2(db, SQL("UPDATE version SET version=?;"));
db_bind_u64(stmt, 0, available); db_bind_int(stmt, 0, available);
db_exec_prepared_v2(stmt); db_exec_prepared_v2(stmt);
tal_free(stmt); tal_free(stmt);
@ -754,7 +754,7 @@ static void db_migrate(struct lightningd *ld, struct db *db, struct log *log)
if (current != orig) { if (current != orig) {
stmt = db_prepare_v2( stmt = db_prepare_v2(
db, SQL("INSERT INTO db_upgrades VALUES (?, ?);")); db, SQL("INSERT INTO db_upgrades VALUES (?, ?);"));
db_bind_u64(stmt, 0, orig); db_bind_int(stmt, 0, orig);
db_bind_text(stmt, 1, version()); db_bind_text(stmt, 1, version());
db_exec_prepared_v2(stmt); db_exec_prepared_v2(stmt);
tal_free(stmt); tal_free(stmt);

Loading…
Cancel
Save