mithril_signer/database/
migration.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//! Migration module
//!
use mithril_persistence::database::SqlMigration;

/// Get all the migrations required by this version of the software.
/// There shall be one migration per database version. There could be several
/// statements per migration.
pub fn get_migrations() -> Vec<SqlMigration> {
    vec![
        // Migration 1
        // Alter `db_version` tables to remove `default current_timestamp` clause from its
        // `updated_at` field, and migrate old date data to rfc 3339.
        SqlMigration::new(
            1,
            r"
-- In some context, most likely tests, the db_version isn't created since the migrator isn't used
create table if not exists 'db_version' (application_type text not null primary key, version integer not null, updated_at text not null);

create table new_db_version (application_type text not null primary key, version integer not null, updated_at text not null);
insert into new_db_version select * from db_version order by rowid asc;

drop table db_version;
alter table new_db_version rename to db_version;
            ",
        ),
        // Migration 2
        // Add the `signed_entity_type` table and insert first types
        SqlMigration::new(
            2,
            r#"
create table signed_entity_type (
    signed_entity_type_id       integer     not null,
    name                        text        not null,
    primary key (signed_entity_type_id)
);
insert into signed_entity_type (signed_entity_type_id, name)
    values  (0, 'Mithril Stake Distribution'),
            (1, 'Cardano Stake Distribution'),
            (2, 'Full Cardano Immutable Files'),
            (3, 'Cardano Transactions');
"#,
        ),
        // Migration 3
        // Create the `signed_beacon` table.
        SqlMigration::new(
            3,
            r"
create table if not exists signed_beacon (
    epoch                       integer     not null,
    beacon                      text        not null,
    signed_entity_type_id       integer     not null,
    initiated_at                text        not null,
    signed_at                   text        not null,

    primary key (epoch, beacon, signed_entity_type_id),
    foreign key (signed_entity_type_id) references signed_entity_type (signed_entity_type_id)
);

create index signed_beacon_epoch on signed_beacon(epoch);
create index signed_beacon_signed_entity_type_id on signed_beacon(signed_entity_type_id);
            ",
        ),
        // Migration 4
        // Remove `network` from cardano immutable files full beacons in `signed_beacon` table
        SqlMigration::new(
            4,
            r#"
update signed_beacon
    set beacon = json_remove(beacon, '$.network')
    where signed_beacon.signed_entity_type_id = 2;
        "#,
        ),
        // Migration 5
        // Add the `stake_pool` table and migration data from the previous
        // `stake_store` JSON format.
        SqlMigration::new(
            5,
            r#"
create table stake_pool (
    stake_pool_id text      not null,
    epoch         integer   not null,
    stake         integer   not null,
    created_at    text      not null,
    primary key (epoch, stake_pool_id)
);
create table if not exists stake (key_hash text primary key, key json not null, value json not null);
insert into stake_pool (epoch, stake_pool_id, stake, created_at) 
    select 
        stake.key as epoch, 
        stake_dis.key as stake_pool_id, 
        stake_dis.value as stake,
        strftime('%Y-%m-%dT%H:%M:%fZ', current_timestamp)
    from stake, json_each(stake.value) as stake_dis 
    order by epoch asc;
drop table stake;
"#,
        ),
        // Migration 6
        // Add the `protocol_initializer` table and migration data from the previous
        // `protocol_initializer` JSON format.
        SqlMigration::new(
            6,
            r#"
create table new_protocol_initializer (
    epoch         integer   not null,
    protocol      json      not null,
    created_at    text      not null,
    primary key (epoch)
);
create table if not exists protocol_initializer (key_hash text primary key, key json not null, value json not null);
insert into new_protocol_initializer (epoch, protocol, created_at) 
    select 
        protocol_initializer.key as epoch, 
        protocol_initializer.value, 
        strftime('%Y-%m-%dT%H:%M:%fZ', current_timestamp)
    from protocol_initializer
    order by epoch asc;
drop table protocol_initializer;
alter table new_protocol_initializer rename to protocol_initializer;
"#,
        ),
        // Migration 7
        // Add the `signed_entity_type` record for 'CardanoDatabase'
        SqlMigration::new(
            7,
            r#"
insert into signed_entity_type (signed_entity_type_id, name) 
    values  (4, 'Cardano Database');
"#,
        ),
    ]
}