risingwave_meta_model_migration/
m20250810_000000_add_user_admin_field.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        // Add is_admin column to user table
10        manager
11            .alter_table(
12                Table::alter()
13                    .table(UserEnum::User)
14                    .add_column(
15                        ColumnDef::new(UserEnum::IsAdmin)
16                            .boolean()
17                            .not_null()
18                            .default(false),
19                    )
20                    .to_owned(),
21            )
22            .await?;
23
24        // Set is_admin = true and is_super = true for the rwadmin user if it exists
25        // Note: rwadmin is already a superuser by default, but we ensure consistency
26        let update_stmt = Query::update()
27            .table(UserEnum::User)
28            .values([
29                (UserEnum::IsAdmin, true.into()),
30                (UserEnum::IsSuper, true.into()),
31            ])
32            .and_where(Expr::col(UserEnum::Name).eq("rwadmin"))
33            .to_owned();
34
35        manager.exec_stmt(update_stmt).await?;
36
37        Ok(())
38    }
39
40    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
41        manager
42            .alter_table(
43                Table::alter()
44                    .table(UserEnum::User)
45                    .drop_column(UserEnum::IsAdmin)
46                    .to_owned(),
47            )
48            .await?;
49        Ok(())
50    }
51}
52
53#[derive(DeriveIden)]
54enum UserEnum {
55    #[sea_orm(iden = "user")]
56    User,
57    Name,
58    IsAdmin,
59    IsSuper,
60}