migration/
m20240301_000011_tickets_create_ticket_stats.rs

1use sea_orm_migration::prelude::*;
2
3use crate::BackendType;
4
5#[derive(DeriveMigrationName)]
6#[allow(dead_code)] // backend type not supported yet but added for future compatibility
7pub struct Migration(pub BackendType);
8
9#[async_trait::async_trait]
10impl MigrationTrait for Migration {
11    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
12        manager
13            .create_table(
14                Table::create()
15                    .table(TicketStatistics::Table)
16                    .if_not_exists()
17                    .col(
18                        ColumnDef::new(TicketStatistics::Id)
19                            .integer()
20                            .not_null()
21                            .auto_increment()
22                            .primary_key(),
23                    )
24                    .col(
25                        ColumnDef::new(TicketStatistics::ChannelId)
26                            .string_len(64)
27                            .not_null()
28                            .unique_key(),
29                    )
30                    .col(ColumnDef::new(TicketStatistics::LastUpdated).timestamp().null())
31                    .col(
32                        ColumnDef::new(TicketStatistics::WinningTickets)
33                            .not_null()
34                            .integer()
35                            .default(0),
36                    )
37                    .col(
38                        ColumnDef::new(TicketStatistics::RedeemedValue)
39                            .binary_len(12)
40                            .not_null()
41                            .default(vec![0u8; 12]),
42                    )
43                    .col(
44                        ColumnDef::new(TicketStatistics::NeglectedValue)
45                            .binary_len(12)
46                            .not_null()
47                            .default(vec![0u8; 12]),
48                    )
49                    .col(
50                        ColumnDef::new(TicketStatistics::RejectedValue)
51                            .binary_len(12)
52                            .not_null()
53                            .default(vec![0u8; 12]),
54                    )
55                    .to_owned(),
56            )
57            .await?;
58
59        let conn = manager.get_connection();
60
61        conn.execute_unprepared(
62            r#"
63            CREATE TRIGGER IF NOT EXISTS trig_ticket_stats_update_timestamp
64                AFTER UPDATE
65                ON ticket_statistics
66                FOR EACH ROW
67                WHEN OLD.last_updated IS NULL OR NEW.last_updated < OLD.last_updated --- avoid infinite loop
68            BEGIN
69                UPDATE ticket_statistics SET last_updated=CURRENT_TIMESTAMP WHERE id=OLD.id;
70            END;
71        "#,
72        )
73        .await?;
74
75        Ok(())
76    }
77
78    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
79        let conn = manager.get_connection();
80
81        conn.execute_unprepared("DROP TRIGGER trig_ticket_stats_update_timestamp;")
82            .await?;
83
84        manager
85            .drop_table(Table::drop().table(TicketStatistics::Table).to_owned())
86            .await
87    }
88}
89
90#[derive(DeriveIden)]
91enum TicketStatistics {
92    Table,
93    Id,
94    ChannelId,
95    LastUpdated,
96    WinningTickets,
97    RedeemedValue,
98    NeglectedValue,
99    RejectedValue,
100}