feat: Refactor user and thought models to use UUIDs instead of integers

- Updated user and thought models to utilize UUIDs for primary keys.
- Modified persistence functions to accommodate UUIDs for user and thought IDs.
- Implemented tag functionality with new Tag and ThoughtTag models.
- Added migration scripts to create new tables for tags and thought-tag relationships.
- Enhanced thought creation to parse hashtags and link them to thoughts.
- Updated tests to reflect changes in user and thought ID types.
This commit is contained in:
2025-09-06 15:29:38 +02:00
parent c9e99e6f23
commit b83b7acf1c
38 changed files with 638 additions and 107 deletions

View File

@@ -3,6 +3,7 @@ pub use sea_orm_migration::prelude::*;
mod m20240101_000001_init;
mod m20250905_000001_init;
mod m20250906_100000_add_profile_fields;
mod m20250906_130237_add_tags;
pub struct Migrator;
@@ -13,6 +14,7 @@ impl MigratorTrait for Migrator {
Box::new(m20240101_000001_init::Migration),
Box::new(m20250905_000001_init::Migration),
Box::new(m20250906_100000_add_profile_fields::Migration),
Box::new(m20250906_130237_add_tags::Migration),
]
}
}

View File

@@ -13,10 +13,10 @@ impl MigrationTrait for Migration {
.if_not_exists()
.col(
ColumnDef::new(User::Id)
.integer()
.uuid()
.not_null()
.auto_increment()
.primary_key(),
.primary_key()
.default(Expr::cust("gen_random_uuid()")),
)
.col(
ColumnDef::new(User::Username)

View File

@@ -13,8 +13,14 @@ impl MigrationTrait for Migration {
Table::create()
.table(Thought::Table)
.if_not_exists()
.col(pk_auto(Thought::Id))
.col(integer(Thought::AuthorId).not_null())
.col(
ColumnDef::new(Thought::Id)
.uuid()
.not_null()
.primary_key()
.default(Expr::cust("gen_random_uuid()")),
)
.col(uuid(Thought::AuthorId).not_null())
.foreign_key(
ForeignKey::create()
.name("fk_thought_author_id")
@@ -39,8 +45,8 @@ impl MigrationTrait for Migration {
Table::create()
.table(Follow::Table)
.if_not_exists()
.col(integer(Follow::FollowerId).not_null())
.col(integer(Follow::FollowedId).not_null())
.col(uuid(Follow::FollowerId).not_null())
.col(uuid(Follow::FollowedId).not_null())
// Composite Primary Key to ensure a user can only follow another once
.primary_key(
Index::create()
@@ -77,7 +83,7 @@ impl MigrationTrait for Migration {
}
#[derive(DeriveIden)]
enum Thought {
pub enum Thought {
Table,
Id,
AuthorId,
@@ -86,7 +92,7 @@ enum Thought {
}
#[derive(DeriveIden)]
enum Follow {
pub enum Follow {
Table,
// The user who is initiating the follow
FollowerId,

View File

@@ -36,8 +36,8 @@ impl MigrationTrait for Migration {
Table::create()
.table(TopFriends::Table)
.if_not_exists()
.col(integer(TopFriends::UserId).not_null())
.col(integer(TopFriends::FriendId).not_null())
.col(uuid(TopFriends::UserId).not_null())
.col(uuid(TopFriends::FriendId).not_null())
.col(small_integer(TopFriends::Position).not_null())
.primary_key(
Index::create()

View File

@@ -0,0 +1,74 @@
use super::m20250905_000001_init::Thought;
use sea_orm_migration::{prelude::*, schema::*};
#[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(Tag::Table)
.if_not_exists()
.col(pk_auto(Tag::Id))
.col(string(Tag::Name).not_null().unique_key())
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(ThoughtTag::Table)
.if_not_exists()
.col(uuid(ThoughtTag::ThoughtId).not_null())
.col(integer(ThoughtTag::TagId).not_null())
.primary_key(
Index::create()
.col(ThoughtTag::ThoughtId)
.col(ThoughtTag::TagId),
)
.foreign_key(
ForeignKey::create()
.name("fk_thought_tag_thought_id")
.from(ThoughtTag::Table, ThoughtTag::ThoughtId)
.to(Thought::Table, Thought::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("fk_thought_tag_tag_id")
.from(ThoughtTag::Table, ThoughtTag::TagId)
.to(Tag::Table, Tag::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(ThoughtTag::Table).to_owned())
.await?;
manager
.drop_table(Table::drop().table(Tag::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum Tag {
Table,
Id,
Name,
}
#[derive(DeriveIden)]
enum ThoughtTag {
Table,
ThoughtId,
TagId,
}