migration/
m20240301_000011_tickets_create_ticket_stats.rs

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