migration/
m20240301_000010_tickets_create_ticket.rs

1use sea_orm_migration::prelude::*;
2
3use crate::BackendType;
4
5#[derive(DeriveMigrationName)]
6pub struct Migration(pub crate::BackendType);
7
8#[async_trait::async_trait]
9impl MigrationTrait for Migration {
10    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
11        let mut table = Table::create()
12            .table(Ticket::Table)
13            .if_not_exists()
14            .col(
15                ColumnDef::new(Ticket::Id)
16                    .integer()
17                    .not_null()
18                    .auto_increment()
19                    .primary_key(),
20            )
21            .col(ColumnDef::new(Ticket::ChannelId).string_len(64).not_null())
22            .col(ColumnDef::new(Ticket::Amount).binary_len(12).not_null())
23            .col(ColumnDef::new(Ticket::Index).binary_len(8).not_null())
24            .col(ColumnDef::new(Ticket::IndexOffset).unsigned().not_null())
25            .col(ColumnDef::new(Ticket::WinningProbability).binary_len(7).not_null())
26            .col(ColumnDef::new(Ticket::ChannelEpoch).binary_len(8).not_null())
27            .col(ColumnDef::new(Ticket::Signature).binary_len(64).not_null())
28            .col(ColumnDef::new(Ticket::Response).binary_len(32).not_null())
29            .col(ColumnDef::new(Ticket::State).tiny_unsigned().not_null().default(0))
30            .clone();
31
32        manager
33            .create_table(if self.0 == BackendType::SQLite {
34                table.to_owned()
35            } else {
36                table
37                    .foreign_key(
38                        ForeignKey::create()
39                            .name("fk_ticket_channel")
40                            .from_tbl(Ticket::Table)
41                            .from_col(Ticket::ChannelId)
42                            .from_col(Ticket::ChannelEpoch)
43                            .to_tbl(Channel::Table)
44                            .to_col(Channel::ChannelId)
45                            .to_col(Channel::Epoch)
46                            .on_delete(ForeignKeyAction::Cascade)
47                            .on_update(ForeignKeyAction::Restrict),
48                    )
49                    .to_owned()
50            })
51            .await?;
52
53        manager
54            .create_index(
55                Index::create()
56                    .name("idx_fk_ticket_channel")
57                    .if_not_exists()
58                    .table(Ticket::Table)
59                    .col(Ticket::ChannelId)
60                    .col(Ticket::ChannelEpoch)
61                    .to_owned(),
62            )
63            .await?;
64
65        manager
66            .create_index(
67                Index::create()
68                    .name("idx_ticket_channel_id_epoch_index")
69                    .if_not_exists()
70                    .table(Ticket::Table)
71                    .col(Ticket::ChannelId)
72                    .col(Ticket::ChannelEpoch)
73                    .col(Ticket::Index)
74                    .unique()
75                    .to_owned(),
76            )
77            .await
78    }
79
80    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
81        manager
82            .drop_index(Index::drop().name("idx_ticket_channel_id_epoch_index").to_owned())
83            .await?;
84
85        manager
86            .drop_index(Index::drop().name("idx_fk_ticket_channel").to_owned())
87            .await?;
88
89        manager.drop_table(Table::drop().table(Ticket::Table).to_owned()).await
90    }
91}
92
93#[derive(DeriveIden)]
94enum Ticket {
95    Table,
96    Id,
97    ChannelId,
98    Amount,
99    Index,
100    IndexOffset,
101    WinningProbability,
102    ChannelEpoch,
103    Signature,
104    Response,
105    State,
106}
107
108#[allow(clippy::enum_variant_names)]
109#[derive(DeriveIden)]
110enum Channel {
111    Table,
112    ChannelId,
113    Epoch,
114}