From 14f919635806a1a26a4f6d9c587d8c56d6d7a556 Mon Sep 17 00:00:00 2001 From: Nicole Tietz-Sokolskaya Date: Sat, 13 Apr 2024 21:28:08 -0400 Subject: [PATCH] Created migration to add workspaces, projects, and documents --- migration/src/lib.rs | 2 + .../m20240316_155147_create_users_table.rs | 6 +- .../src/m20240325_053712_create_projects.rs | 137 ++++++++++++++++++ 3 files changed, 142 insertions(+), 3 deletions(-) create mode 100644 migration/src/m20240325_053712_create_projects.rs diff --git a/migration/src/lib.rs b/migration/src/lib.rs index c4ad367..35fce53 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -1,6 +1,7 @@ pub use sea_orm_migration::prelude::*; mod m20240316_155147_create_users_table; +mod m20240325_053712_create_projects; pub struct Migrator; @@ -9,6 +10,7 @@ impl MigratorTrait for Migrator { fn migrations() -> Vec> { vec![ Box::new(m20240316_155147_create_users_table::Migration), + Box::new(m20240325_053712_create_projects::Migration), ] } } diff --git a/migration/src/m20240316_155147_create_users_table.rs b/migration/src/m20240316_155147_create_users_table.rs index 7f3f7a2..d011c4d 100644 --- a/migration/src/m20240316_155147_create_users_table.rs +++ b/migration/src/m20240316_155147_create_users_table.rs @@ -22,13 +22,13 @@ impl MigrationTrait for Migration { .col( ColumnDef::new(User::Email) .string_len(100) - .unique() + .unique_key() .not_null(), ) .col( ColumnDef::new(User::Username) .string_len(32) - .unique() + .unique_key() .not_null(), ) .col(ColumnDef::new(User::PasswordHash).string().not_null()) @@ -57,7 +57,7 @@ impl MigrationTrait for Migration { } #[derive(DeriveIden)] -enum User { +pub enum User { Table, Id, FullName, diff --git a/migration/src/m20240325_053712_create_projects.rs b/migration/src/m20240325_053712_create_projects.rs new file mode 100644 index 0000000..f11db81 --- /dev/null +++ b/migration/src/m20240325_053712_create_projects.rs @@ -0,0 +1,137 @@ +use super::m20240316_155147_create_users_table::User; +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> { + let mut owner_fk = ForeignKey::create() + .name("fk_workspace_owner") + .from(Workspace::Table, Workspace::OwnerId) + .to(User::Table, User::Id) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade) + .to_owned(); + + let workspace_table = Table::create() + .table(Workspace::Table) + .if_not_exists() + .col( + ColumnDef::new(Workspace::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col(ColumnDef::new(Workspace::Name).string().not_null()) + .col(ColumnDef::new(Workspace::OwnerId).integer().not_null()) + .foreign_key(&mut owner_fk) + .to_owned(); + + let mut project_workspace_fk = ForeignKey::create() + .name("fk_project_workspace") + .from(Project::Table, Project::WorkspaceId) + .to(Workspace::Table, Workspace::Id) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade) + .to_owned(); + + let project_table = Table::create() + .table(Project::Table) + .if_not_exists() + .col( + ColumnDef::new(Project::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col(ColumnDef::new(Project::Name).string().not_null()) + .col(ColumnDef::new(Project::WorkspaceId).integer().not_null()) + .foreign_key(&mut project_workspace_fk) + .to_owned(); + + let mut document_project_fk = ForeignKey::create() + .name("fk_document_project") + .from(Document::Table, Document::ProjectId) + .to(Project::Table, Project::Id) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade) + .to_owned(); + + let mut document_workspace_fk = ForeignKey::create() + .name("fk_document_workspace") + .from(Document::Table, Document::WorkspaceId) + .to(Workspace::Table, Workspace::Id) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade) + .to_owned(); + + let document_table = Table::create() + .table(Document::Table) + .if_not_exists() + .col( + ColumnDef::new(Document::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col(ColumnDef::new(Document::Title).string().not_null()) + .col(ColumnDef::new(Document::Content).string().not_null()) + .col(ColumnDef::new(Document::ProjectId).integer().not_null()) + .col(ColumnDef::new(Document::WorkspaceId).integer().not_null()) + .foreign_key(&mut document_project_fk) + .foreign_key(&mut document_workspace_fk) + .to_owned(); + + + manager.create_table(workspace_table).await?; + manager.create_table(project_table).await?; + manager.create_table(document_table).await?; + + + + Ok(()) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + let document_table = Table::drop().table(Document::Table).to_owned(); + let project_table = Table::drop().table(Project::Table).to_owned(); + let workspace_table = Table::drop().table(Workspace::Table).to_owned(); + + manager.drop_table(document_table).await?; + manager.drop_table(project_table).await?; + manager.drop_table(workspace_table).await?; + + Ok(()) + } +} + +#[derive(DeriveIden)] +pub enum Workspace { + Table, + Id, + Name, + OwnerId, +} + +#[derive(DeriveIden)] +pub enum Project { + Table, + Id, + Name, + WorkspaceId, +} + +#[derive(DeriveIden)] +pub enum Document { + Table, + Id, + Title, + Content, + WorkspaceId, + ProjectId, +}