From 19e9657ed8e310529a2f70042c98716dcec16537 Mon Sep 17 00:00:00 2001 From: alemi Date: Fri, 21 Jun 2024 20:51:14 +0200 Subject: [PATCH] feat: add database migrations --- migrations/Cargo.toml | 9 ++ migrations/src/lib.rs | 17 ++ ...0001_create_messages_users_chats_tables.rs | 153 ++++++++++++++++++ 3 files changed, 179 insertions(+) create mode 100644 migrations/Cargo.toml create mode 100644 migrations/src/lib.rs create mode 100644 migrations/src/m20240621_000001_create_messages_users_chats_tables.rs diff --git a/migrations/Cargo.toml b/migrations/Cargo.toml new file mode 100644 index 0000000..ec59bc0 --- /dev/null +++ b/migrations/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "scct-migrations" +version = "0.1.0" +edition = "2021" + +[lib] + +[dependencies] +sea-orm-migration = "0.12" diff --git a/migrations/src/lib.rs b/migrations/src/lib.rs new file mode 100644 index 0000000..9a31e69 --- /dev/null +++ b/migrations/src/lib.rs @@ -0,0 +1,17 @@ +use sea_orm_migration::prelude::*; + +mod m20240621_000001_create_messages_users_chats_tables; + + +pub struct Migrator; + +#[async_trait::async_trait] +impl MigratorTrait for Migrator { + fn migrations() -> Vec> { + vec![ + Box::new(m20240621_000001_create_messages_users_chats_tables::Migration) + ] + } +} + +pub use sea_orm_migration::MigratorTrait; diff --git a/migrations/src/m20240621_000001_create_messages_users_chats_tables.rs b/migrations/src/m20240621_000001_create_messages_users_chats_tables.rs new file mode 100644 index 0000000..a67e7d2 --- /dev/null +++ b/migrations/src/m20240621_000001_create_messages_users_chats_tables.rs @@ -0,0 +1,153 @@ +use sea_orm_migration::prelude::*; + +#[derive(DeriveIden)] +pub enum Users { + Table, + Id, + Username, + Domain, + DisplayName, + Description, + Avatar, + Created, + Updated, +} + +#[derive(DeriveIden)] +pub enum Chats { + Table, + Id, + Name, +} + +#[derive(DeriveIden)] +pub enum Messages { + Table, + Id, + UserId, + ChatId, + Content, + Created, + Updated, +} + + +#[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) + .comment("registered users on this service") + .col( + ColumnDef::new(Users::Id) + .uuid() + .not_null() + .primary_key() + ) + .col(ColumnDef::new(Users::Username).string().not_null()) + .col(ColumnDef::new(Users::Domain).string().not_null()) + .col(ColumnDef::new(Users::DisplayName).string().null()) + .col(ColumnDef::new(Users::Description).string().null()) + .col(ColumnDef::new(Users::Avatar).string().null()) + .col(ColumnDef::new(Users::Created).timestamp_with_time_zone().not_null().default(Expr::current_timestamp())) + .col(ColumnDef::new(Users::Updated).timestamp_with_time_zone().null()) + .to_owned() + ) + .await?; + + manager + .create_index( + Index::create() + .unique() + .name("index-users-username-domain") + .table(Users::Table) + .col(Users::Username) + .col(Users::Domain) + .to_owned() + ) + .await?; + + + manager + .create_table( + Table::create() + .table(Chats::Table) + .comment("active chat rooms") + .col( + ColumnDef::new(Chats::Id) + .uuid() + .not_null() + .primary_key() + ) + .col(ColumnDef::new(Chats::Name).string().not_null()) + .to_owned() + ) + .await?; + + manager + .create_table( + Table::create() + .table(Messages::Table) + .comment("all individual sent messages") + .col( + ColumnDef::new(Messages::Id) + .uuid() + .not_null() + .primary_key() + ) + .col(ColumnDef::new(Messages::UserId).uuid().not_null()) + .foreign_key( + ForeignKey::create() + .name("fkey-messages-user") + .from(Messages::Table, Messages::UserId) + .to(Users::Table, Users::Id) + .on_update(ForeignKeyAction::Cascade) + ) + .col(ColumnDef::new(Messages::ChatId).uuid().not_null()) + .foreign_key( + ForeignKey::create() + .name("fkey-messages-chat") + .from(Messages::Table, Messages::ChatId) + .to(Chats::Table, Chats::Id) + .on_update(ForeignKeyAction::Cascade) + ) + .col(ColumnDef::new(Messages::Content).string().null()) + .col(ColumnDef::new(Messages::Created).timestamp_with_time_zone().not_null().default(Expr::current_timestamp())) + .col(ColumnDef::new(Messages::Updated).timestamp_with_time_zone().null()) + .to_owned() + ).await?; + + manager + .create_index(Index::create().name("index-messages-user").table(Messages::Table).col(Messages::UserId).to_owned()) + .await?; + + manager + .create_index(Index::create().name("index-messages-chat").table(Messages::Table).col(Messages::ChatId).to_owned()) + .await?; + + + Ok(()) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(Users::Table).to_owned()) + .await?; + + manager + .drop_table(Table::drop().table(Chats::Table).to_owned()) + .await?; + + manager + .drop_table(Table::drop().table(Messages::Table).to_owned()) + .await?; + + Ok(()) + } +}