Skip to content

Commit 0b1a399

Browse files
committed
Update sqlite schema with unique index for utxos, change insert_utxo to upsert
1 parent cea7987 commit 0b1a399

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/database/sqlite.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ static MIGRATIONS: &[&str] = &[
4141
"INSERT INTO transaction_details SELECT txid, timestamp, received, sent, fee, height FROM transaction_details_old;",
4242
"DROP TABLE transaction_details_old;",
4343
"ALTER TABLE utxos ADD COLUMN is_spent;",
44+
// drop all data due to possible inconsistencies with duplicate utxos, re-sync required
45+
"DELETE FROM checksums;",
46+
"DELETE FROM last_derivation_indices;",
47+
"DELETE FROM script_pubkeys;",
48+
"DELETE FROM sync_time;",
49+
"DELETE FROM transaction_details;",
50+
"DELETE FROM transactions;",
51+
"DELETE FROM utxos;",
52+
"DROP INDEX idx_txid_vout;",
53+
"CREATE UNIQUE INDEX idx_utxos_txid_vout ON utxos(txid, vout);"
4454
];
4555

4656
/// Sqlite database stored on filesystem
@@ -86,7 +96,7 @@ impl SqliteDatabase {
8696
script: &[u8],
8797
is_spent: bool,
8898
) -> Result<i64, Error> {
89-
let mut statement = self.connection.prepare_cached("INSERT INTO utxos (value, keychain, vout, txid, script, is_spent) VALUES (:value, :keychain, :vout, :txid, :script, :is_spent)")?;
99+
let mut statement = self.connection.prepare_cached("INSERT INTO utxos (value, keychain, vout, txid, script, is_spent) VALUES (:value, :keychain, :vout, :txid, :script, :is_spent) ON CONFLICT(txid, vout) DO UPDATE SET value=:value, keychain=:keychain, script=:script, is_spent=:is_spent")?;
90100
statement.execute(named_params! {
91101
":value": value,
92102
":keychain": keychain,

0 commit comments

Comments
 (0)