migration/
m20240930_000017_logs_create_log.rs

1use sea_orm_migration::prelude::*;
2
3#[derive(DeriveMigrationName)]
4pub struct Migration;
5
6#[async_trait::async_trait]
7impl MigrationTrait for Migration {
8    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
9        // Log and LogStatus tables are kept separate to allow for easier export of the logs
10        // themselves.
11
12        manager
13            .create_table(
14                Table::create()
15                    .table(LogStatus::Table)
16                    .if_not_exists()
17                    .primary_key(
18                        Index::create()
19                            .name("pk_log_status")
20                            .table(LogStatus::Table)
21                            .col(LogStatus::BlockNumber)
22                            .col(LogStatus::TransactionIndex)
23                            .col(LogStatus::LogIndex),
24                    )
25                    .col(ColumnDef::new(LogStatus::TransactionIndex).not_null().binary_len(8))
26                    .col(ColumnDef::new(LogStatus::LogIndex).not_null().binary_len(8))
27                    .col(ColumnDef::new(LogStatus::BlockNumber).not_null().binary_len(8))
28                    .col(ColumnDef::new(LogStatus::Processed).boolean().not_null().default(false))
29                    .col(ColumnDef::new(LogStatus::ProcessedAt).date_time())
30                    .col(ColumnDef::new(LogStatus::Checksum).binary_len(32))
31                    .to_owned(),
32            )
33            .await?;
34
35        manager
36            .create_table(
37                Table::create()
38                    .table(Log::Table)
39                    .if_not_exists()
40                    .primary_key(
41                        Index::create()
42                            .name("pk_log")
43                            .table(Log::Table)
44                            .col(Log::BlockNumber)
45                            .col(Log::TransactionIndex)
46                            .col(Log::LogIndex),
47                    )
48                    .col(ColumnDef::new(Log::TransactionIndex).not_null().binary_len(8))
49                    .col(ColumnDef::new(Log::LogIndex).not_null().binary_len(8))
50                    .col(ColumnDef::new(Log::BlockNumber).not_null().binary_len(8))
51                    .col(ColumnDef::new(Log::BlockHash).binary_len(32).not_null())
52                    .col(ColumnDef::new(Log::TransactionHash).binary_len(32).not_null())
53                    .col(ColumnDef::new(Log::Address).binary_len(20).not_null())
54                    .col(ColumnDef::new(Log::Topics).binary().not_null())
55                    .col(ColumnDef::new(Log::Data).binary().not_null())
56                    .col(ColumnDef::new(Log::Removed).boolean().not_null().default(false))
57                    .foreign_key(
58                        ForeignKey::create()
59                            .name("fk_log_status_log")
60                            .from(Log::Table, (Log::BlockNumber, Log::TransactionIndex, Log::LogIndex))
61                            .to(
62                                LogStatus::Table,
63                                (LogStatus::BlockNumber, LogStatus::TransactionIndex, LogStatus::LogIndex),
64                            )
65                            .on_delete(ForeignKeyAction::Cascade)
66                            .on_update(ForeignKeyAction::Cascade),
67                    )
68                    .to_owned(),
69            )
70            .await
71    }
72
73    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
74        manager.drop_table(Table::drop().table(Log::Table).to_owned()).await?;
75        manager
76            .drop_table(Table::drop().table(LogStatus::Table).to_owned())
77            .await
78    }
79}
80
81#[allow(clippy::enum_variant_names)]
82#[derive(DeriveIden)]
83enum Log {
84    Table,
85    // address from which this log originated.
86    Address,
87    // Array of 0 to 4 32 Bytes DATA of indexed log arguments. The first topic is the
88    // hash of the signature of the event.
89    Topics,
90    // contains zero or more 32 Bytes non-indexed arguments of the log.
91    Data,
92    // the block number where this log was in. null when it's a pending log.
93    BlockNumber,
94    // hash of the transactions this log was created from. null when its pending log.
95    // hash of the transaction this log was created from. null when it's a pending log.
96    TransactionHash,
97    // integer of the transaction's index position this log was created from. null when it's a pending log.
98    TransactionIndex,
99    // hash of the block where this log was in. null when its pending. null when its pending log.
100    BlockHash,
101    // integer of the log index position in the block. null when its pending log.
102    LogIndex,
103    // true when the log was removed, due to a chain reorganization. false if its a valid log.
104    Removed,
105}
106
107#[derive(DeriveIden)]
108enum LogStatus {
109    Table,
110    // Values to identify the log.
111    BlockNumber,
112    TransactionIndex,
113    LogIndex,
114    // Indicates whether the log has been processed.
115    Processed,
116    // Time when the log was processed.
117    ProcessedAt,
118    // Computed checksum of this log and previous logs
119    Checksum,
120}