|
| 1 | +//! The migration that records ephemeral addresses used beyond the last known mined address, for each account. |
| 2 | +use std::collections::HashSet; |
| 3 | + |
| 4 | +use rusqlite; |
| 5 | +use schemer; |
| 6 | +use schemer_rusqlite::RusqliteMigration; |
| 7 | +use uuid::Uuid; |
| 8 | + |
| 9 | +use crate::wallet::init::WalletMigrationError; |
| 10 | + |
| 11 | +use super::full_account_ids; |
| 12 | + |
| 13 | +pub(super) const MIGRATION_ID: Uuid = Uuid::from_u128(0x0e1d4274_1f8e_44e2_909d_689a4bc2967b); |
| 14 | + |
| 15 | +pub(super) struct Migration; |
| 16 | + |
| 17 | +impl schemer::Migration for Migration { |
| 18 | + fn id(&self) -> Uuid { |
| 19 | + MIGRATION_ID |
| 20 | + } |
| 21 | + |
| 22 | + fn dependencies(&self) -> HashSet<Uuid> { |
| 23 | + [full_account_ids::MIGRATION_ID].into_iter().collect() |
| 24 | + } |
| 25 | + |
| 26 | + fn description(&self) -> &'static str { |
| 27 | + "Record ephemeral addresses used beyond the last known mined address, for each account." |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl RusqliteMigration for Migration { |
| 32 | + type Error = WalletMigrationError; |
| 33 | + |
| 34 | + fn up(&self, transaction: &rusqlite::Transaction) -> Result<(), WalletMigrationError> { |
| 35 | + transaction.execute_batch( |
| 36 | + r#" |
| 37 | + ALTER TABLE accounts ADD first_unmined_ephemeral_index INTEGER NOT NULL DEFAULT 0; |
| 38 | + ALTER TABLE accounts ADD first_unused_ephemeral_index INTEGER NOT NULL DEFAULT 0 |
| 39 | + CONSTRAINT unused_gte_unmined CHECK (first_unused_ephemeral_index >= first_unmined_ephemeral_index); |
| 40 | + "#, |
| 41 | + )?; |
| 42 | + Ok(()) |
| 43 | + } |
| 44 | + |
| 45 | + fn down(&self, transaction: &rusqlite::Transaction) -> Result<(), WalletMigrationError> { |
| 46 | + transaction.execute_batch( |
| 47 | + r#" |
| 48 | + -- Dropping first_unused_ephemeral_index also drops its constraint. |
| 49 | + ALTER TABLE accounts DROP COLUMN first_unused_ephemeral_index; |
| 50 | + ALTER TABLE accounts DROP COLUMN first_unmined_ephemeral_index; |
| 51 | + "#, |
| 52 | + )?; |
| 53 | + Ok(()) |
| 54 | + } |
| 55 | +} |
0 commit comments