migration/
m20240226_000002_index_create_account.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        manager
10            .create_table(
11                Table::create()
12                    .table(Account::Table)
13                    .if_not_exists()
14                    .col(
15                        ColumnDef::new(Account::Id)
16                            .integer()
17                            .not_null()
18                            .auto_increment()
19                            .primary_key(),
20                    )
21                    .col(ColumnDef::new(Account::ChainKey).string_len(40).not_null())
22                    .col(ColumnDef::new(Account::PacketKey).string_len(64).not_null())
23                    .to_owned(),
24            )
25            .await?;
26
27        manager
28            .create_index(
29                Index::create()
30                    .if_not_exists()
31                    .name("idx_account_chain_key")
32                    .table(Account::Table)
33                    .col(Account::ChainKey)
34                    .to_owned(),
35            )
36            .await?;
37
38        manager
39            .create_index(
40                Index::create()
41                    .if_not_exists()
42                    .name("idx_account_packet_key")
43                    .table(Account::Table)
44                    .col(Account::PacketKey)
45                    .to_owned(),
46            )
47            .await?;
48
49        manager
50            .create_index(
51                Index::create()
52                    .if_not_exists()
53                    .name("idx_account_chain_packet_key")
54                    .table(Account::Table)
55                    .col(Account::ChainKey)
56                    .col(Account::PacketKey)
57                    .unique()
58                    .to_owned(),
59            )
60            .await?;
61
62        manager
63            .create_table(
64                Table::create()
65                    .table(Announcement::Table)
66                    .if_not_exists()
67                    .col(
68                        ColumnDef::new(Announcement::Id)
69                            .integer()
70                            .not_null()
71                            .auto_increment()
72                            .primary_key(),
73                    )
74                    .col(ColumnDef::new(Announcement::AccountId).integer().not_null())
75                    .col(ColumnDef::new(Announcement::Multiaddress).string().not_null())
76                    .col(ColumnDef::new(Announcement::AtBlock).integer().unsigned().not_null())
77                    .foreign_key(
78                        ForeignKey::create()
79                            .name("fk_announcement_account")
80                            .from(Announcement::Table, Announcement::AccountId)
81                            .to(Account::Table, Account::Id)
82                            .on_delete(ForeignKeyAction::Cascade)
83                            .on_update(ForeignKeyAction::Restrict),
84                    )
85                    .to_owned(),
86            )
87            .await?;
88
89        manager
90            .create_index(
91                Index::create()
92                    .name("idx_announcement_account_id")
93                    .if_not_exists()
94                    .table(Announcement::Table)
95                    .col(Announcement::AccountId)
96                    .to_owned(),
97            )
98            .await?;
99
100        manager
101            .create_index(
102                Index::create()
103                    .name("idx_announcement_account_id_multiaddress")
104                    .if_not_exists()
105                    .table(Announcement::Table)
106                    .col(Announcement::AccountId)
107                    .col(Announcement::Multiaddress)
108                    .unique()
109                    .to_owned(),
110            )
111            .await
112    }
113
114    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
115        manager
116            .drop_index(
117                Index::drop()
118                    .name("idx_announcement_account_id_multiaddress")
119                    .to_owned(),
120            )
121            .await?;
122
123        manager
124            .drop_index(Index::drop().name("idx_announcement_account_id").to_owned())
125            .await?;
126
127        manager
128            .drop_table(Table::drop().table(Announcement::Table).to_owned())
129            .await?;
130
131        manager
132            .drop_index(Index::drop().name("idx_account_chain_packet_key").to_owned())
133            .await?;
134
135        manager
136            .drop_index(Index::drop().name("idx_account_chain_key").to_owned())
137            .await?;
138
139        manager
140            .drop_index(Index::drop().name("idx_account_packet_key").to_owned())
141            .await?;
142
143        manager.drop_table(Table::drop().table(Account::Table).to_owned()).await
144    }
145}
146
147#[derive(DeriveIden)]
148enum Account {
149    Table,
150    Id,
151    PacketKey,
152    ChainKey,
153}
154
155#[derive(DeriveIden)]
156enum Announcement {
157    Table,
158    Id,
159    AccountId,
160    Multiaddress,
161    AtBlock,
162}