feat: add database migrations
This commit is contained in:
parent
f94afcd8b8
commit
19e9657ed8
3 changed files with 179 additions and 0 deletions
9
migrations/Cargo.toml
Normal file
9
migrations/Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "scct-migrations"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
|
||||
[dependencies]
|
||||
sea-orm-migration = "0.12"
|
17
migrations/src/lib.rs
Normal file
17
migrations/src/lib.rs
Normal file
|
@ -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<Box<dyn MigrationTrait>> {
|
||||
vec![
|
||||
Box::new(m20240621_000001_create_messages_users_chats_tables::Migration)
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
pub use sea_orm_migration::MigratorTrait;
|
|
@ -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(())
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue