init
This commit is contained in:
commit
c7a592933e
32 changed files with 7871 additions and 0 deletions
274
src/migration/m20260213_000001_init.rs
Normal file
274
src/migration/m20260213_000001_init.rs
Normal file
|
|
@ -0,0 +1,274 @@
|
|||
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(Users::Table)
|
||||
.if_not_exists()
|
||||
.col(
|
||||
ColumnDef::new(Users::Id)
|
||||
.uuid()
|
||||
.not_null()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(Users::OidcSub).string().not_null().unique_key())
|
||||
.col(ColumnDef::new(Users::Email).string())
|
||||
.col(ColumnDef::new(Users::DisplayName).string().not_null())
|
||||
.col(ColumnDef::new(Users::AvatarUrl).string())
|
||||
.col(
|
||||
ColumnDef::new(Users::CreatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(Users::UpdatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(Guilds::Table)
|
||||
.if_not_exists()
|
||||
.col(
|
||||
ColumnDef::new(Guilds::Id)
|
||||
.uuid()
|
||||
.not_null()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(Guilds::Name).string().not_null())
|
||||
.col(ColumnDef::new(Guilds::OwnerUserId).uuid().not_null())
|
||||
.col(
|
||||
ColumnDef::new(Guilds::CreatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_guilds_owner_user")
|
||||
.from(Guilds::Table, Guilds::OwnerUserId)
|
||||
.to(Users::Table, Users::Id),
|
||||
)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(GuildMembers::Table)
|
||||
.if_not_exists()
|
||||
.col(ColumnDef::new(GuildMembers::GuildId).uuid().not_null())
|
||||
.col(ColumnDef::new(GuildMembers::UserId).uuid().not_null())
|
||||
.col(
|
||||
ColumnDef::new(GuildMembers::CreatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.primary_key(
|
||||
Index::create()
|
||||
.name("pk_guild_members")
|
||||
.col(GuildMembers::GuildId)
|
||||
.col(GuildMembers::UserId),
|
||||
)
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_guild_members_guild")
|
||||
.from(GuildMembers::Table, GuildMembers::GuildId)
|
||||
.to(Guilds::Table, Guilds::Id)
|
||||
.on_delete(ForeignKeyAction::Cascade),
|
||||
)
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_guild_members_user")
|
||||
.from(GuildMembers::Table, GuildMembers::UserId)
|
||||
.to(Users::Table, Users::Id)
|
||||
.on_delete(ForeignKeyAction::Cascade),
|
||||
)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(Channels::Table)
|
||||
.if_not_exists()
|
||||
.col(
|
||||
ColumnDef::new(Channels::Id)
|
||||
.uuid()
|
||||
.not_null()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(Channels::GuildId).uuid().not_null())
|
||||
.col(ColumnDef::new(Channels::Name).string().not_null())
|
||||
.col(
|
||||
ColumnDef::new(Channels::CreatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_channels_guild")
|
||||
.from(Channels::Table, Channels::GuildId)
|
||||
.to(Guilds::Table, Guilds::Id)
|
||||
.on_delete(ForeignKeyAction::Cascade),
|
||||
)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(Messages::Table)
|
||||
.if_not_exists()
|
||||
.col(
|
||||
ColumnDef::new(Messages::Id)
|
||||
.uuid()
|
||||
.not_null()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(Messages::ChannelId).uuid().not_null())
|
||||
.col(ColumnDef::new(Messages::AuthorUserId).uuid().not_null())
|
||||
.col(ColumnDef::new(Messages::Body).text().not_null())
|
||||
.col(
|
||||
ColumnDef::new(Messages::CreatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_messages_channel")
|
||||
.from(Messages::Table, Messages::ChannelId)
|
||||
.to(Channels::Table, Channels::Id)
|
||||
.on_delete(ForeignKeyAction::Cascade),
|
||||
)
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_messages_author")
|
||||
.from(Messages::Table, Messages::AuthorUserId)
|
||||
.to(Users::Table, Users::Id),
|
||||
)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_guild_members_user_id")
|
||||
.table(GuildMembers::Table)
|
||||
.col(GuildMembers::UserId)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_channels_guild_id")
|
||||
.table(Channels::Table)
|
||||
.col(Channels::GuildId)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_messages_channel_created_at")
|
||||
.table(Messages::Table)
|
||||
.col(Messages::ChannelId)
|
||||
.col(Messages::CreatedAt)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(Messages::Table).to_owned())
|
||||
.await?;
|
||||
manager
|
||||
.drop_table(Table::drop().table(Channels::Table).to_owned())
|
||||
.await?;
|
||||
manager
|
||||
.drop_table(Table::drop().table(GuildMembers::Table).to_owned())
|
||||
.await?;
|
||||
manager
|
||||
.drop_table(Table::drop().table(Guilds::Table).to_owned())
|
||||
.await?;
|
||||
manager
|
||||
.drop_table(Table::drop().table(Users::Table).to_owned())
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum Users {
|
||||
Table,
|
||||
Id,
|
||||
OidcSub,
|
||||
Email,
|
||||
DisplayName,
|
||||
AvatarUrl,
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum Guilds {
|
||||
Table,
|
||||
Id,
|
||||
Name,
|
||||
OwnerUserId,
|
||||
CreatedAt,
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum GuildMembers {
|
||||
Table,
|
||||
GuildId,
|
||||
UserId,
|
||||
CreatedAt,
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum Channels {
|
||||
Table,
|
||||
Id,
|
||||
GuildId,
|
||||
Name,
|
||||
CreatedAt,
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum Messages {
|
||||
Table,
|
||||
Id,
|
||||
ChannelId,
|
||||
AuthorUserId,
|
||||
Body,
|
||||
CreatedAt,
|
||||
}
|
||||
91
src/migration/m20260213_000002_invites.rs
Normal file
91
src/migration/m20260213_000002_invites.rs
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
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,
|
||||
}
|
||||
65
src/migration/m20260213_000003_channel_kind.rs
Normal file
65
src/migration/m20260213_000003_channel_kind.rs
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
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
|
||||
.alter_table(
|
||||
Table::alter()
|
||||
.table(Channels::Table)
|
||||
.add_column(
|
||||
ColumnDef::new(Channels::Kind)
|
||||
.string()
|
||||
.not_null()
|
||||
.default("text"),
|
||||
)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_channels_guild_kind")
|
||||
.table(Channels::Table)
|
||||
.col(Channels::GuildId)
|
||||
.col(Channels::Kind)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_index(
|
||||
Index::drop()
|
||||
.name("idx_channels_guild_kind")
|
||||
.table(Channels::Table)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
manager
|
||||
.alter_table(
|
||||
Table::alter()
|
||||
.table(Channels::Table)
|
||||
.drop_column(Channels::Kind)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum Channels {
|
||||
Table,
|
||||
GuildId,
|
||||
Kind,
|
||||
}
|
||||
95
src/migration/m20260213_000004_direct_messages.rs
Normal file
95
src/migration/m20260213_000004_direct_messages.rs
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
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(DirectMessages::Table)
|
||||
.if_not_exists()
|
||||
.col(
|
||||
ColumnDef::new(DirectMessages::Id)
|
||||
.uuid()
|
||||
.not_null()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(DirectMessages::SenderUserId).uuid().not_null())
|
||||
.col(ColumnDef::new(DirectMessages::RecipientUserId).uuid().not_null())
|
||||
.col(ColumnDef::new(DirectMessages::Body).text().not_null())
|
||||
.col(
|
||||
ColumnDef::new(DirectMessages::CreatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_direct_messages_sender_user")
|
||||
.from(DirectMessages::Table, DirectMessages::SenderUserId)
|
||||
.to(Users::Table, Users::Id)
|
||||
.on_delete(ForeignKeyAction::Cascade),
|
||||
)
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_direct_messages_recipient_user")
|
||||
.from(DirectMessages::Table, DirectMessages::RecipientUserId)
|
||||
.to(Users::Table, Users::Id)
|
||||
.on_delete(ForeignKeyAction::Cascade),
|
||||
)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_direct_messages_sender_created_at")
|
||||
.table(DirectMessages::Table)
|
||||
.col(DirectMessages::SenderUserId)
|
||||
.col(DirectMessages::CreatedAt)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_direct_messages_recipient_created_at")
|
||||
.table(DirectMessages::Table)
|
||||
.col(DirectMessages::RecipientUserId)
|
||||
.col(DirectMessages::CreatedAt)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(DirectMessages::Table).to_owned())
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum DirectMessages {
|
||||
Table,
|
||||
Id,
|
||||
SenderUserId,
|
||||
RecipientUserId,
|
||||
Body,
|
||||
CreatedAt,
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum Users {
|
||||
Table,
|
||||
Id,
|
||||
}
|
||||
20
src/migration/mod.rs
Normal file
20
src/migration/mod.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
use sea_orm_migration::prelude::*;
|
||||
|
||||
mod m20260213_000001_init;
|
||||
mod m20260213_000002_invites;
|
||||
mod m20260213_000003_channel_kind;
|
||||
mod m20260213_000004_direct_messages;
|
||||
|
||||
pub struct Migrator;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigratorTrait for Migrator {
|
||||
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
|
||||
vec![
|
||||
Box::new(m20260213_000001_init::Migration),
|
||||
Box::new(m20260213_000002_invites::Migration),
|
||||
Box::new(m20260213_000003_channel_kind::Migration),
|
||||
Box::new(m20260213_000004_direct_messages::Migration),
|
||||
]
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue