Created migration to add workspaces, projects, and documents

This commit is contained in:
Nicole Tietz-Sokolskaya 2024-04-13 21:28:08 -04:00
parent 5b117e9a8c
commit 14f9196358
3 changed files with 142 additions and 3 deletions

View file

@ -1,6 +1,7 @@
pub use sea_orm_migration::prelude::*; pub use sea_orm_migration::prelude::*;
mod m20240316_155147_create_users_table; mod m20240316_155147_create_users_table;
mod m20240325_053712_create_projects;
pub struct Migrator; pub struct Migrator;
@ -9,6 +10,7 @@ impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> { fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![ vec![
Box::new(m20240316_155147_create_users_table::Migration), Box::new(m20240316_155147_create_users_table::Migration),
Box::new(m20240325_053712_create_projects::Migration),
] ]
} }
} }

View file

@ -22,13 +22,13 @@ impl MigrationTrait for Migration {
.col( .col(
ColumnDef::new(User::Email) ColumnDef::new(User::Email)
.string_len(100) .string_len(100)
.unique() .unique_key()
.not_null(), .not_null(),
) )
.col( .col(
ColumnDef::new(User::Username) ColumnDef::new(User::Username)
.string_len(32) .string_len(32)
.unique() .unique_key()
.not_null(), .not_null(),
) )
.col(ColumnDef::new(User::PasswordHash).string().not_null()) .col(ColumnDef::new(User::PasswordHash).string().not_null())
@ -57,7 +57,7 @@ impl MigrationTrait for Migration {
} }
#[derive(DeriveIden)] #[derive(DeriveIden)]
enum User { pub enum User {
Table, Table,
Id, Id,
FullName, FullName,

View file

@ -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,
}