migration/
m20240226_000001_index_create_channel.rs

1use hopr_primitive_types::prelude::*;
2use sea_orm_migration::prelude::*;
3
4#[derive(DeriveMigrationName)]
5pub struct Migration;
6
7#[async_trait::async_trait]
8impl MigrationTrait for Migration {
9    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
10        manager
11            .create_table(
12                Table::create()
13                    .table(Channel::Table)
14                    .if_not_exists()
15                    .col(
16                        ColumnDef::new(Channel::Id)
17                            .integer()
18                            .not_null()
19                            .auto_increment()
20                            .primary_key(),
21                    )
22                    .col(
23                        ColumnDef::new(Channel::ChannelId)
24                            .string_len(64)
25                            .not_null()
26                            .unique_key(),
27                    )
28                    .col(ColumnDef::new(Channel::Source).string_len(40).not_null())
29                    .col(ColumnDef::new(Channel::Destination).string_len(40).not_null())
30                    .col(ColumnDef::new(Channel::Balance).binary_len(12).not_null())
31                    .col(ColumnDef::new(Channel::Status).tiny_unsigned().not_null())
32                    .col(
33                        ColumnDef::new(Channel::Epoch)
34                            .binary_len(8)
35                            .not_null()
36                            .default(U256::one().to_be_bytes().to_vec()), // Default set in the SC
37                    )
38                    .col(
39                        ColumnDef::new(Channel::TicketIndex)
40                            .binary_len(8)
41                            .not_null()
42                            .default(U256::zero().to_be_bytes().to_vec()), // Default set in the SC
43                    )
44                    .col(ColumnDef::new(Channel::ClosureTime).timestamp().null())
45                    .to_owned(),
46            )
47            .await?;
48
49        manager
50            .create_index(
51                Index::create()
52                    .name("idx_channel_id_channel_epoch")
53                    .if_not_exists()
54                    .table(Channel::Table)
55                    .col(Channel::ChannelId)
56                    .col(Channel::Epoch)
57                    .unique()
58                    .to_owned(),
59            )
60            .await?;
61
62        manager
63            .create_index(
64                Index::create()
65                    .name("idx_channel_source_destination")
66                    .if_not_exists()
67                    .unique()
68                    .table(Channel::Table)
69                    .col(Channel::Source)
70                    .col(Channel::Destination)
71                    .to_owned(),
72            )
73            .await?;
74
75        manager
76            .create_index(
77                Index::create()
78                    .name("idx_channel_source")
79                    .if_not_exists()
80                    .table(Channel::Table)
81                    .col(Channel::Source)
82                    .to_owned(),
83            )
84            .await?;
85
86        manager
87            .create_index(
88                Index::create()
89                    .name("idx_channel_destination")
90                    .if_not_exists()
91                    .table(Channel::Table)
92                    .col(Channel::Destination)
93                    .to_owned(),
94            )
95            .await
96    }
97
98    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
99        manager
100            .drop_index(Index::drop().name("idx_channel_destination").to_owned())
101            .await?;
102
103        manager
104            .drop_index(Index::drop().name("idx_channel_source").to_owned())
105            .await?;
106
107        manager
108            .drop_index(Index::drop().name("idx_channel_source_destination").to_owned())
109            .await?;
110
111        manager
112            .drop_index(Index::drop().name("idx_channel_id_channel_epoch").to_owned())
113            .await?;
114
115        manager.drop_table(Table::drop().table(Channel::Table).to_owned()).await
116    }
117}
118
119#[allow(clippy::enum_variant_names)]
120#[derive(DeriveIden)]
121enum Channel {
122    Table,
123    Id,
124    ChannelId,
125    Source,
126    Destination,
127    Status,
128    Balance,
129    TicketIndex,
130    Epoch,
131    ClosureTime,
132}