discord/src/migration/m20260213_000002_invites.rs
2026-02-13 17:45:29 +01:00

91 lines
2.7 KiB
Rust

use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Invites::Table)
.if_not_exists()
.col(ColumnDef::new(Invites::Code).string().not_null().primary_key())
.col(ColumnDef::new(Invites::GuildId).uuid().not_null())
.col(ColumnDef::new(Invites::CreatedByUserId).uuid().not_null())
.col(
ColumnDef::new(Invites::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(ColumnDef::new(Invites::ExpiresAt).timestamp_with_time_zone())
.col(ColumnDef::new(Invites::MaxUses).integer())
.col(
ColumnDef::new(Invites::UseCount)
.integer()
.not_null()
.default(0),
)
.foreign_key(
ForeignKey::create()
.name("fk_invites_guild")
.from(Invites::Table, Invites::GuildId)
.to(Guilds::Table, Guilds::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("fk_invites_created_by")
.from(Invites::Table, Invites::CreatedByUserId)
.to(Users::Table, Users::Id),
)
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx_invites_guild_id")
.table(Invites::Table)
.col(Invites::GuildId)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Invites::Table).to_owned())
.await?;
Ok(())
}
}
#[derive(DeriveIden)]
enum Invites {
Table,
Code,
GuildId,
CreatedByUserId,
CreatedAt,
ExpiresAt,
MaxUses,
UseCount,
}
#[derive(DeriveIden)]
enum Guilds {
Table,
Id,
}
#[derive(DeriveIden)]
enum Users {
Table,
Id,
}