Skip to main content

sova_scope/
migration.rs

1//! Scope schema migrations.
2
3use sea_orm_migration::prelude::*;
4
5pub struct ScopeMigrator;
6
7#[async_trait::async_trait]
8impl MigratorTrait for ScopeMigrator {
9    fn migrations() -> Vec<Box<dyn MigrationTrait>> {
10        let mut v = vec![Box::new(m20260811_scope_memberships::Migration) as Box<dyn MigrationTrait>];
11        #[cfg(feature = "invites")]
12        v.push(Box::new(m20260811_scope_invitations::Migration));
13        v.push(Box::new(m20260811_scope_membership_audit::Migration));
14        v
15    }
16}
17
18mod m20260811_scope_memberships {
19    use super::*;
20
21    pub struct Migration;
22
23    impl MigrationName for Migration {
24        fn name(&self) -> &str {
25            "m20260811_scope_memberships"
26        }
27    }
28
29    #[async_trait::async_trait]
30    impl MigrationTrait for Migration {
31        async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
32            manager
33                .create_table(
34                    Table::create()
35                        .table(ScopeMemberships::Table)
36                        .if_not_exists()
37                        .col(
38                            ColumnDef::new(ScopeMemberships::Id)
39                                .big_integer()
40                                .not_null()
41                                .auto_increment()
42                                .primary_key(),
43                        )
44                        .col(
45                            ColumnDef::new(ScopeMemberships::ScopeKind)
46                                .string_len(64)
47                                .not_null(),
48                        )
49                        .col(
50                            ColumnDef::new(ScopeMemberships::ScopeId)
51                                .big_integer()
52                                .not_null(),
53                        )
54                        .col(
55                            ColumnDef::new(ScopeMemberships::UserId)
56                                .big_integer()
57                                .not_null(),
58                        )
59                        .col(
60                            ColumnDef::new(ScopeMemberships::Role)
61                                .string_len(32)
62                                .not_null(),
63                        )
64                        .to_owned(),
65                )
66                .await?;
67            manager
68                .create_index(
69                    Index::create()
70                        .if_not_exists()
71                        .name("idx_scope_memberships_unique")
72                        .table(ScopeMemberships::Table)
73                        .col(ScopeMemberships::ScopeKind)
74                        .col(ScopeMemberships::ScopeId)
75                        .col(ScopeMemberships::UserId)
76                        .unique()
77                        .to_owned(),
78                )
79                .await?;
80            Ok(())
81        }
82
83        async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
84            manager
85                .drop_table(Table::drop().table(ScopeMemberships::Table).to_owned())
86                .await
87        }
88    }
89
90    #[derive(Iden)]
91    enum ScopeMemberships {
92        Table,
93        Id,
94        ScopeKind,
95        ScopeId,
96        UserId,
97        Role,
98    }
99}
100
101#[cfg(feature = "invites")]
102mod m20260811_scope_invitations {
103    use super::*;
104
105    pub struct Migration;
106
107    impl MigrationName for Migration {
108        fn name(&self) -> &str {
109            "m20260811_scope_invitations"
110        }
111    }
112
113    #[async_trait::async_trait]
114    impl MigrationTrait for Migration {
115        async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
116            manager
117                .create_table(
118                    Table::create()
119                        .table(ScopeInvitations::Table)
120                        .if_not_exists()
121                        .col(
122                            ColumnDef::new(ScopeInvitations::Id)
123                                .big_integer()
124                                .not_null()
125                                .auto_increment()
126                                .primary_key(),
127                        )
128                        .col(
129                            ColumnDef::new(ScopeInvitations::ScopeKind)
130                                .string_len(64)
131                                .not_null(),
132                        )
133                        .col(
134                            ColumnDef::new(ScopeInvitations::ScopeId)
135                                .big_integer()
136                                .not_null(),
137                        )
138                        .col(
139                            ColumnDef::new(ScopeInvitations::Email)
140                                .string_len(320)
141                                .not_null(),
142                        )
143                        .col(
144                            ColumnDef::new(ScopeInvitations::Role)
145                                .string_len(32)
146                                .not_null(),
147                        )
148                        .col(
149                            ColumnDef::new(ScopeInvitations::InvitedBy)
150                                .big_integer()
151                                .not_null(),
152                        )
153                        .col(
154                            ColumnDef::new(ScopeInvitations::Token)
155                                .string_len(64)
156                                .not_null(),
157                        )
158                        .col(
159                            ColumnDef::new(ScopeInvitations::Status)
160                                .string_len(16)
161                                .not_null(),
162                        )
163                        .col(
164                            ColumnDef::new(ScopeInvitations::CreatedAt)
165                                .timestamp_with_time_zone()
166                                .not_null()
167                                .default(Expr::current_timestamp()),
168                        )
169                        .to_owned(),
170                )
171                .await?;
172            manager
173                .create_index(
174                    Index::create()
175                        .if_not_exists()
176                        .name("idx_scope_invite_scope_email")
177                        .table(ScopeInvitations::Table)
178                        .col(ScopeInvitations::ScopeKind)
179                        .col(ScopeInvitations::ScopeId)
180                        .col(ScopeInvitations::Email)
181                        .unique()
182                        .to_owned(),
183                )
184                .await?;
185            Ok(())
186        }
187
188        async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
189            manager
190                .drop_table(Table::drop().table(ScopeInvitations::Table).to_owned())
191                .await
192        }
193    }
194
195    #[derive(Iden)]
196    enum ScopeInvitations {
197        Table,
198        Id,
199        ScopeKind,
200        ScopeId,
201        Email,
202        Role,
203        InvitedBy,
204        Token,
205        Status,
206        CreatedAt,
207    }
208}
209
210mod m20260811_scope_membership_audit {
211    use super::*;
212
213    pub struct Migration;
214
215    impl MigrationName for Migration {
216        fn name(&self) -> &str {
217            "m20260811_scope_membership_audit"
218        }
219    }
220
221    #[async_trait::async_trait]
222    impl MigrationTrait for Migration {
223        async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
224            manager
225                .create_table(
226                    Table::create()
227                        .table(ScopeMembershipAudit::Table)
228                        .if_not_exists()
229                        .col(
230                            ColumnDef::new(ScopeMembershipAudit::Id)
231                                .big_integer()
232                                .not_null()
233                                .auto_increment()
234                                .primary_key(),
235                        )
236                        .col(
237                            ColumnDef::new(ScopeMembershipAudit::ScopeKind)
238                                .string_len(64)
239                                .not_null(),
240                        )
241                        .col(
242                            ColumnDef::new(ScopeMembershipAudit::ScopeId)
243                                .big_integer()
244                                .not_null(),
245                        )
246                        .col(
247                            ColumnDef::new(ScopeMembershipAudit::UserId)
248                                .big_integer()
249                                .not_null(),
250                        )
251                        .col(
252                            ColumnDef::new(ScopeMembershipAudit::ActorId)
253                                .big_integer()
254                                .null(),
255                        )
256                        .col(
257                            ColumnDef::new(ScopeMembershipAudit::Action)
258                                .string_len(32)
259                                .not_null(),
260                        )
261                        .col(
262                            ColumnDef::new(ScopeMembershipAudit::OldRole)
263                                .string_len(32)
264                                .null(),
265                        )
266                        .col(
267                            ColumnDef::new(ScopeMembershipAudit::NewRole)
268                                .string_len(32)
269                                .null(),
270                        )
271                        .col(
272                            ColumnDef::new(ScopeMembershipAudit::CreatedAt)
273                                .timestamp_with_time_zone()
274                                .not_null()
275                                .default(Expr::current_timestamp()),
276                        )
277                        .to_owned(),
278                )
279                .await?;
280            manager
281                .create_index(
282                    Index::create()
283                        .if_not_exists()
284                        .name("idx_scope_membership_audit_scope")
285                        .table(ScopeMembershipAudit::Table)
286                        .col(ScopeMembershipAudit::ScopeKind)
287                        .col(ScopeMembershipAudit::ScopeId)
288                        .col(ScopeMembershipAudit::CreatedAt)
289                        .to_owned(),
290                )
291                .await?;
292            Ok(())
293        }
294
295        async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
296            manager
297                .drop_table(Table::drop().table(ScopeMembershipAudit::Table).to_owned())
298                .await
299        }
300    }
301
302    #[derive(Iden)]
303    enum ScopeMembershipAudit {
304        Table,
305        Id,
306        ScopeKind,
307        ScopeId,
308        UserId,
309        ActorId,
310        Action,
311        OldRole,
312        NewRole,
313        CreatedAt,
314    }
315}