risingwave_meta_model_migration/
m20240506_112555_subscription_partial_ckpt.rs

1use sea_orm_migration::prelude::{Table as MigrationTable, *};
2
3use crate::drop_tables;
4use crate::utils::ColumnDefExt;
5
6#[derive(DeriveMigrationName)]
7pub struct Migration;
8
9#[async_trait::async_trait]
10impl MigrationTrait for Migration {
11    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
12        // drop tables cascade.
13        drop_tables!(manager, Subscription);
14        manager
15            .create_table(
16                MigrationTable::create()
17                    .table(Subscription::Table)
18                    .col(
19                        ColumnDef::new(Subscription::SubscriptionId)
20                            .integer()
21                            .primary_key(),
22                    )
23                    .col(ColumnDef::new(Subscription::Name).string().not_null())
24                    .col(
25                        ColumnDef::new(Subscription::Definition)
26                            .rw_long_text(manager)
27                            .not_null(),
28                    )
29                    .col(
30                        ColumnDef::new(Subscription::RetentionSeconds)
31                            .string()
32                            .big_integer(),
33                    )
34                    .col(
35                        ColumnDef::new(Subscription::SubscriptionState)
36                            .string()
37                            .integer(),
38                    )
39                    .col(
40                        ColumnDef::new(Subscription::DependentTableId)
41                            .integer()
42                            .not_null(),
43                    )
44                    .foreign_key(
45                        &mut ForeignKey::create()
46                            .name("FK_subscription_object_id")
47                            .from(Subscription::Table, Subscription::SubscriptionId)
48                            .to(
49                                crate::m20230908_072257_init::Object::Table,
50                                crate::m20230908_072257_init::Object::Oid,
51                            )
52                            .on_delete(ForeignKeyAction::Cascade)
53                            .to_owned(),
54                    )
55                    .to_owned(),
56            )
57            .await?;
58        Ok(())
59    }
60
61    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
62        // drop tables cascade.
63        drop_tables!(manager, Subscription);
64        manager
65            .create_table(
66                MigrationTable::create()
67                    .table(Subscription::Table)
68                    .col(
69                        ColumnDef::new(Subscription::SubscriptionId)
70                            .integer()
71                            .primary_key(),
72                    )
73                    .col(ColumnDef::new(Subscription::Name).string().not_null())
74                    .col(
75                        ColumnDef::new(Subscription::Definition)
76                            .rw_long_text(manager)
77                            .not_null(),
78                    )
79                    .col(
80                        ColumnDef::new(Subscription::Columns)
81                            .rw_binary(manager)
82                            .not_null(),
83                    )
84                    .col(
85                        ColumnDef::new(Subscription::PlanPk)
86                            .rw_binary(manager)
87                            .not_null(),
88                    )
89                    .col(
90                        ColumnDef::new(Subscription::DistributionKey)
91                            .json_binary()
92                            .not_null(),
93                    )
94                    .col(
95                        ColumnDef::new(Subscription::Properties)
96                            .json_binary()
97                            .not_null(),
98                    )
99                    .col(
100                        ColumnDef::new(Subscription::SubscriptionFromName)
101                            .string()
102                            .not_null(),
103                    )
104                    .col(ColumnDef::new(Subscription::SubscriptionInternalTableName).string())
105                    .foreign_key(
106                        &mut ForeignKey::create()
107                            .name("FK_subscription_object_id")
108                            .from(Subscription::Table, Subscription::SubscriptionId)
109                            .to(
110                                crate::m20230908_072257_init::Object::Table,
111                                crate::m20230908_072257_init::Object::Oid,
112                            )
113                            .on_delete(ForeignKeyAction::Cascade)
114                            .to_owned(),
115                    )
116                    .to_owned(),
117            )
118            .await?;
119        Ok(())
120    }
121}
122
123#[derive(DeriveIden)]
124enum Subscription {
125    Table,
126    // common
127    SubscriptionId,
128    Name,
129    Definition,
130    // before
131    Columns,
132    PlanPk,
133    DistributionKey,
134    Properties,
135    SubscriptionFromName,
136    SubscriptionInternalTableName,
137    // after
138    RetentionSeconds,
139    SubscriptionState,
140    DependentTableId,
141}