mithril_signer/database/
migration.rs1use mithril_persistence::database::SqlMigration;
4
5pub fn get_migrations() -> Vec<SqlMigration> {
9 vec![
10 SqlMigration::new(
14 1,
15 r"
16-- In some context, most likely tests, the db_version isn't created since the migrator isn't used
17create table if not exists 'db_version' (application_type text not null primary key, version integer not null, updated_at text not null);
18
19create table new_db_version (application_type text not null primary key, version integer not null, updated_at text not null);
20insert into new_db_version select * from db_version order by rowid asc;
21
22drop table db_version;
23alter table new_db_version rename to db_version;
24 ",
25 ),
26 SqlMigration::new(
29 2,
30 r#"
31create table signed_entity_type (
32 signed_entity_type_id integer not null,
33 name text not null,
34 primary key (signed_entity_type_id)
35);
36insert into signed_entity_type (signed_entity_type_id, name)
37 values (0, 'Mithril Stake Distribution'),
38 (1, 'Cardano Stake Distribution'),
39 (2, 'Full Cardano Immutable Files'),
40 (3, 'Cardano Transactions');
41"#,
42 ),
43 SqlMigration::new(
46 3,
47 r"
48create table if not exists signed_beacon (
49 epoch integer not null,
50 beacon text not null,
51 signed_entity_type_id integer not null,
52 initiated_at text not null,
53 signed_at text not null,
54
55 primary key (epoch, beacon, signed_entity_type_id),
56 foreign key (signed_entity_type_id) references signed_entity_type (signed_entity_type_id)
57);
58
59create index signed_beacon_epoch on signed_beacon(epoch);
60create index signed_beacon_signed_entity_type_id on signed_beacon(signed_entity_type_id);
61 ",
62 ),
63 SqlMigration::new(
66 4,
67 r#"
68update signed_beacon
69 set beacon = json_remove(beacon, '$.network')
70 where signed_beacon.signed_entity_type_id = 2;
71 "#,
72 ),
73 SqlMigration::new(
77 5,
78 r#"
79create table stake_pool (
80 stake_pool_id text not null,
81 epoch integer not null,
82 stake integer not null,
83 created_at text not null,
84 primary key (epoch, stake_pool_id)
85);
86create table if not exists stake (key_hash text primary key, key json not null, value json not null);
87insert into stake_pool (epoch, stake_pool_id, stake, created_at)
88 select
89 stake.key as epoch,
90 stake_dis.key as stake_pool_id,
91 stake_dis.value as stake,
92 strftime('%Y-%m-%dT%H:%M:%fZ', current_timestamp)
93 from stake, json_each(stake.value) as stake_dis
94 order by epoch asc;
95drop table stake;
96"#,
97 ),
98 SqlMigration::new(
102 6,
103 r#"
104create table new_protocol_initializer (
105 epoch integer not null,
106 protocol json not null,
107 created_at text not null,
108 primary key (epoch)
109);
110create table if not exists protocol_initializer (key_hash text primary key, key json not null, value json not null);
111insert into new_protocol_initializer (epoch, protocol, created_at)
112 select
113 protocol_initializer.key as epoch,
114 protocol_initializer.value,
115 strftime('%Y-%m-%dT%H:%M:%fZ', current_timestamp)
116 from protocol_initializer
117 order by epoch asc;
118drop table protocol_initializer;
119alter table new_protocol_initializer rename to protocol_initializer;
120"#,
121 ),
122 SqlMigration::new(
125 7,
126 r#"
127insert into signed_entity_type (signed_entity_type_id, name)
128 values (4, 'Cardano Database');
129"#,
130 ),
131 ]
132}