mithril_persistence/database/
cardano_transaction_migration.rs

1//! Migration module for cardano transactions store
2//!
3use crate::database::SqlMigration;
4
5/// Get all the migrations required by this version of the software.
6/// There shall be one migration per database version. There could be several
7/// statements per migration.
8pub fn get_migrations() -> Vec<SqlMigration> {
9    vec![
10        // Migration 1
11        // Add the `cardano_tx` table.
12        SqlMigration::new(
13            1,
14            r#"
15create table cardano_tx (
16    transaction_hash        text      not null,
17    block_number            integer   not null,
18    immutable_file_number   integer   not null,
19    primary key (transaction_hash)
20);
21
22create unique index cardano_tx_immutable_file_number_index on cardano_tx(immutable_file_number);
23"#,
24        ),
25        // Migration 2
26        // Fix the `cardano_tx` table index on immutable_file number, incorrectly marked as unique.
27        SqlMigration::new(
28            2,
29            r#"
30-- remove all data from the cardano tx table since a lot of transactions where missing for each
31-- block and we rely on their insert order.
32delete from cardano_tx;
33
34drop index cardano_tx_immutable_file_number_index;
35create index cardano_tx_immutable_file_number_index on cardano_tx(immutable_file_number);
36
37vacuum;
38"#,
39        ),
40        // Migration 3
41        // Add `slot_number` and `block_hash` columns to `cardano_tx`.
42        SqlMigration::new(
43            3,
44            r#"
45-- remove all data from the cardano tx table since the new columns are mandatory
46delete from cardano_tx;
47
48alter table cardano_tx add column slot_number integer not null;
49alter table cardano_tx add column block_hash text not null;
50
51vacuum;
52        "#,
53        ),
54        // Migration 4
55        // Add index on `block_number` column of `cardano_tx` table
56        SqlMigration::new(
57            4,
58            r#"
59create index block_number_index on cardano_tx(block_number);
60"#,
61        ),
62        // Migration 5
63        // Add `block_range_root` table
64        SqlMigration::new(
65            5,
66            r#"
67create table block_range_root (
68    start         integer   not null,
69    end           integer   not null,
70    merkle_root   text      not null,
71    primary key (start, end)
72);
73"#,
74        ),
75        // Migration 6
76        // Add composite index on `block_number/transaction_hash` column of `cardano_tx` table
77        // Truncate `block_range_root` table after changing the order of retrieval of the transactions
78        SqlMigration::new(
79            6,
80            r#"
81create index block_number_transaction_hash_index on cardano_tx(block_number, transaction_hash);
82
83-- remove all data from the block_range_root table since the order used to create them has changed
84delete from block_range_root;
85
86vacuum;
87"#,
88        ),
89        // Migration 7
90        // Enable full `auto_vacuum` on the database to prevent the database from growing
91        // indefinitely since data is often deleted with chain rollbacks or, only on signers,
92        // transactions pruning.
93        SqlMigration::new(
94            7,
95            r#"
96-- 'pragma auto_vacuum = full' can't be applied to an existing database, so we need to recreate
97-- the database by using 'vacuum'.
98pragma auto_vacuum = full;
99vacuum;
100"#,
101        ),
102        // Migration 8
103        // Remove Immutable File Number in Cardano Transaction
104        SqlMigration::new(
105            8,
106            r#"
107drop index cardano_tx_immutable_file_number_index;
108alter table cardano_tx drop column immutable_file_number;
109 "#,
110        ),
111        // Migration 9
112        // Truncate Cardano transaction related tables to avoid data inconsistency
113        // with previous versions of the nodes.
114        SqlMigration::new(
115            9,
116            r#"
117delete from cardano_tx;
118delete from block_range_root;
119vacuum;
120 "#,
121        ),
122    ]
123}