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