From 468371d43df5f5006a12d4a210a4cf3510d7d55e Mon Sep 17 00:00:00 2001 From: aprilthepink Date: Sun, 5 May 2024 16:26:48 +0200 Subject: [PATCH] Add user follow relation --- migration/src/lib.rs | 2 + .../m20240505_002524_user_follow_relation.rs | 66 +++++++++++++++++++ src/entities/follow_relation.rs | 38 +++++++++++ src/entities/mod.rs | 1 + src/entities/prelude.rs | 1 + 5 files changed, 108 insertions(+) create mode 100644 migration/src/m20240505_002524_user_follow_relation.rs create mode 100644 src/entities/follow_relation.rs diff --git a/migration/src/lib.rs b/migration/src/lib.rs index 4b2b34d..7e4572a 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -3,6 +3,7 @@ pub use sea_orm_migration::prelude::*; mod m20220101_000001_post_table; mod m20240417_230111_user_table; mod m20240417_233430_post_user_keys; +mod m20240505_002524_user_follow_relation; pub struct Migrator; @@ -13,6 +14,7 @@ impl MigratorTrait for Migrator { Box::new(m20220101_000001_post_table::Migration), Box::new(m20240417_230111_user_table::Migration), Box::new(m20240417_233430_post_user_keys::Migration), + Box::new(m20240505_002524_user_follow_relation::Migration), ] } } diff --git a/migration/src/m20240505_002524_user_follow_relation.rs b/migration/src/m20240505_002524_user_follow_relation.rs new file mode 100644 index 0000000..f10dd8c --- /dev/null +++ b/migration/src/m20240505_002524_user_follow_relation.rs @@ -0,0 +1,66 @@ +use sea_orm_migration::prelude::*; + +use crate::m20240417_230111_user_table::User; + +#[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(FollowRelation::Table) + .if_not_exists() + .col( + ColumnDef::new(FollowRelation::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col(ColumnDef::new(FollowRelation::FolloweeId).string().not_null()) + .col(ColumnDef::new(FollowRelation::FollowerId).string().not_null()) + .col(ColumnDef::new(FollowRelation::FolloweeHost).string()) + .col(ColumnDef::new(FollowRelation::FollowerHost).string()) + .col(ColumnDef::new(FollowRelation::FolloweeInbox).string()) + .col(ColumnDef::new(FollowRelation::FollowerInbox).string()) + .foreign_key( + ForeignKey::create() + .name("fk_follow_relation_followee_id") + .from(FollowRelation::Table, FollowRelation::FolloweeId) + .to(User::Table, User::Id) + .on_delete(ForeignKeyAction::Cascade), + ) + .foreign_key( + ForeignKey::create() + .name("fk_follow_relation_follower_id") + .from(FollowRelation::Table, FollowRelation::FollowerId) + .to(User::Table, User::Id) + .on_delete(ForeignKeyAction::Cascade), + ) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(FollowRelation::Table).to_owned()) + .await + } +} + +#[derive(DeriveIden)] +enum FollowRelation { + Table, + Id, + FolloweeId, + FollowerId, + FolloweeHost, + FollowerHost, + FolloweeInbox, + FollowerInbox +} diff --git a/src/entities/follow_relation.rs b/src/entities/follow_relation.rs new file mode 100644 index 0000000..ea2074a --- /dev/null +++ b/src/entities/follow_relation.rs @@ -0,0 +1,38 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.10 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "follow_relation")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i32, + pub followee_id: String, + pub follower_id: String, + pub followee_host: Option, + pub follower_host: Option, + pub followee_inbox: Option, + pub follower_inbox: Option, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::user::Entity", + from = "Column::FollowerId", + to = "super::user::Column::Id", + on_update = "NoAction", + on_delete = "Cascade" + )] + User2, + #[sea_orm( + belongs_to = "super::user::Entity", + from = "Column::FolloweeId", + to = "super::user::Column::Id", + on_update = "NoAction", + on_delete = "Cascade" + )] + User1, +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src/entities/mod.rs b/src/entities/mod.rs index 2217ca2..ecd232e 100644 --- a/src/entities/mod.rs +++ b/src/entities/mod.rs @@ -4,3 +4,4 @@ pub mod prelude; pub mod post; pub mod user; +pub mod follow_relation; diff --git a/src/entities/prelude.rs b/src/entities/prelude.rs index 1456e3e..f0d643b 100644 --- a/src/entities/prelude.rs +++ b/src/entities/prelude.rs @@ -1,4 +1,5 @@ //! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.10 +pub use super::follow_relation::Entity as FollowRelation; pub use super::post::Entity as Post; pub use super::user::Entity as User;