Skip to main content

sova_activity/
migration.rs

1//! `activity_log` schema.
2
3use sea_orm_migration::prelude::*;
4
5pub struct ActivityMigrator;
6
7#[async_trait::async_trait]
8impl MigratorTrait for ActivityMigrator {
9    fn migrations() -> Vec<Box<dyn MigrationTrait>> {
10        vec![Box::new(m20260308_000001_activity_log::Migration)]
11    }
12}
13
14mod m20260308_000001_activity_log {
15    use sea_orm_migration::prelude::*;
16
17    #[derive(DeriveMigrationName)]
18    pub struct Migration;
19
20    #[async_trait::async_trait]
21    impl MigrationTrait for Migration {
22        async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
23            manager
24                .create_table(
25                    Table::create()
26                        .table(ActivityLog::Table)
27                        .if_not_exists()
28                        .col(
29                            ColumnDef::new(ActivityLog::Id)
30                                .big_integer()
31                                .not_null()
32                                .auto_increment()
33                                .primary_key(),
34                        )
35                        .col(ColumnDef::new(ActivityLog::ActorId).big_integer().null())
36                        .col(ColumnDef::new(ActivityLog::SubjectType).string().not_null())
37                        .col(ColumnDef::new(ActivityLog::SubjectId).string().not_null())
38                        .col(ColumnDef::new(ActivityLog::Event).string().not_null())
39                        .col(
40                            ColumnDef::new(ActivityLog::Properties)
41                                .text()
42                                .not_null()
43                                .default("{}"),
44                        )
45                        .col(ColumnDef::new(ActivityLog::Ip).string().null())
46                        .col(ColumnDef::new(ActivityLog::UserAgent).text().null())
47                        .col(
48                            ColumnDef::new(ActivityLog::CreatedAt)
49                                .timestamp_with_time_zone()
50                                .not_null(),
51                        )
52                        .to_owned(),
53                )
54                .await?;
55
56            manager
57                .create_index(
58                    Index::create()
59                        .if_not_exists()
60                        .name("idx_activity_subject")
61                        .table(ActivityLog::Table)
62                        .col(ActivityLog::SubjectType)
63                        .col(ActivityLog::SubjectId)
64                        .to_owned(),
65                )
66                .await?;
67            manager
68                .create_index(
69                    Index::create()
70                        .if_not_exists()
71                        .name("idx_activity_actor")
72                        .table(ActivityLog::Table)
73                        .col(ActivityLog::ActorId)
74                        .to_owned(),
75                )
76                .await?;
77            manager
78                .create_index(
79                    Index::create()
80                        .if_not_exists()
81                        .name("idx_activity_event")
82                        .table(ActivityLog::Table)
83                        .col(ActivityLog::Event)
84                        .to_owned(),
85                )
86                .await?;
87            manager
88                .create_index(
89                    Index::create()
90                        .if_not_exists()
91                        .name("idx_activity_created")
92                        .table(ActivityLog::Table)
93                        .col(ActivityLog::CreatedAt)
94                        .to_owned(),
95                )
96                .await?;
97
98            Ok(())
99        }
100
101        async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
102            manager
103                .drop_table(Table::drop().table(ActivityLog::Table).to_owned())
104                .await?;
105            Ok(())
106        }
107    }
108
109    #[derive(Iden)]
110    enum ActivityLog {
111        Table,
112        Id,
113        ActorId,
114        SubjectType,
115        SubjectId,
116        Event,
117        Properties,
118        Ip,
119        UserAgent,
120        CreatedAt,
121    }
122}