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> {
// Admin-configurable reaction set.
manager
.create_table(
Table::create()
.table(ReactionTypes::Table)
.if_not_exists()
.col(ColumnDef::new(ReactionTypes::Id).uuid().not_null().primary_key())
.col(ColumnDef::new(ReactionTypes::Key).string_len(32).unique_key().not_null())
.col(ColumnDef::new(ReactionTypes::Emoji).string_len(16).not_null())
.col(ColumnDef::new(ReactionTypes::Position).integer().not_null().default(0))
.col(ColumnDef::new(ReactionTypes::IsActive).boolean().not_null().default(true))
.col(ColumnDef::new(ReactionTypes::CreatedAt).timestamp_with_time_zone().not_null().default(Expr::current_timestamp()))
.to_owned(),
)
.await?;
// One reaction per user per post (changing it replaces the old one).
manager
.create_table(
Table::create()
.table(PostReactions::Table)
.if_not_exists()
.col(ColumnDef::new(PostReactions::Id).uuid().not_null().primary_key())
.col(ColumnDef::new(PostReactions::PostId).uuid().not_null())
.col(ColumnDef::new(PostReactions::UserId).uuid().not_null())
.col(ColumnDef::new(PostReactions::ReactionId).uuid().not_null())
.col(ColumnDef::new(PostReactions::CreatedAt).timestamp_with_time_zone().not_null().default(Expr::current_timestamp()))
.foreign_key(ForeignKey::create().name("fk_post_reactions_post").from(PostReactions::Table, PostReactions::PostId).to(Alias::new("posts"), Alias::new("id")).on_delete(ForeignKeyAction::Cascade).on_update(ForeignKeyAction::Cascade))
.foreign_key(ForeignKey::create().name("fk_post_reactions_user").from(PostReactions::Table, PostReactions::UserId).to(Alias::new("users"), Alias::new("id")).on_delete(ForeignKeyAction::Cascade).on_update(ForeignKeyAction::Cascade))
.foreign_key(ForeignKey::create().name("fk_post_reactions_type").from(PostReactions::Table, PostReactions::ReactionId).to(ReactionTypes::Table, ReactionTypes::Id).on_delete(ForeignKeyAction::Cascade).on_update(ForeignKeyAction::Cascade))
.to_owned(),
)
.await?;
manager
.create_index(Index::create().name("idx_post_reactions_post_user").table(PostReactions::Table).col(PostReactions::PostId).col(PostReactions::UserId).unique().to_owned())
.await?;
manager
.create_index(Index::create().name("idx_post_reactions_post").table(PostReactions::Table).col(PostReactions::PostId).to_owned())
.await?;
// Per-user opt-out of reaction notifications.
manager
.alter_table(
Table::alter()
.table(Users::Table)
.add_column(ColumnDef::new(Users::ReactionNotifications).boolean().not_null().default(true))
.to_owned(),
)
.await?;
// Seed defaults.
manager
.get_connection()
.execute_unprepared(
"INSERT INTO reaction_types (id, key, emoji, position, is_active, created_at) VALUES \
(gen_random_uuid(), 'heart', '\u{2764}\u{FE0F}', 0, true, now()), \
(gen_random_uuid(), 'star', '\u{2B50}', 1, true, now()), \
(gen_random_uuid(), 'thumbsup', '\u{1F44D}', 2, true, now()) \
ON CONFLICT (key) DO NOTHING;",
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager.alter_table(Table::alter().table(Users::Table).drop_column(Users::ReactionNotifications).to_owned()).await?;
manager.drop_table(Table::drop().table(PostReactions::Table).to_owned()).await?;
manager.drop_table(Table::drop().table(ReactionTypes::Table).to_owned()).await?;
Ok(())
}
}
#[derive(DeriveIden)]
enum ReactionTypes {
Table,
Id,
Key,
Emoji,
Position,
IsActive,
CreatedAt,
}
#[derive(DeriveIden)]
enum PostReactions {
Table,
Id,
PostId,
UserId,
ReactionId,
CreatedAt,
}
#[derive(DeriveIden)]
enum Users {
Table,
ReactionNotifications,
}
neiam
/recourse
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
4.6 KiB · text
History
263b40b