89 lines
2.7 KiB
Rust
89 lines
2.7 KiB
Rust
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> {
|
|
manager
|
|
.create_table(
|
|
Table::create()
|
|
.table(Workspaces::Table)
|
|
.if_not_exists()
|
|
.col(
|
|
ColumnDef::new(Workspaces::Id)
|
|
.uuid()
|
|
.not_null()
|
|
.primary_key(),
|
|
)
|
|
.col(
|
|
ColumnDef::new(Workspaces::OrganizationId)
|
|
.uuid()
|
|
.not_null(),
|
|
)
|
|
.col(ColumnDef::new(Workspaces::Name).string().not_null())
|
|
.col(
|
|
ColumnDef::new(Workspaces::Slug)
|
|
.string()
|
|
.not_null(),
|
|
)
|
|
.col(
|
|
ColumnDef::new(Workspaces::CreatedAt)
|
|
.timestamp_with_time_zone()
|
|
.not_null(),
|
|
)
|
|
.col(
|
|
ColumnDef::new(Workspaces::UpdatedAt)
|
|
.timestamp_with_time_zone()
|
|
.not_null(),
|
|
)
|
|
.foreign_key(
|
|
ForeignKey::create()
|
|
.name("fk_workspace_organization")
|
|
.from(Workspaces::Table, Workspaces::OrganizationId)
|
|
.to(Organizations::Table, Organizations::Id)
|
|
.on_delete(ForeignKeyAction::Cascade),
|
|
)
|
|
.to_owned(),
|
|
)
|
|
.await?;
|
|
|
|
// Create unique index on organization_id + slug
|
|
manager
|
|
.create_index(
|
|
Index::create()
|
|
.unique()
|
|
.name("idx_workspaces_org_slug")
|
|
.table(Workspaces::Table)
|
|
.col(Workspaces::OrganizationId)
|
|
.col(Workspaces::Slug)
|
|
.to_owned(),
|
|
)
|
|
.await
|
|
}
|
|
|
|
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
|
manager
|
|
.drop_table(Table::drop().table(Workspaces::Table).to_owned())
|
|
.await
|
|
}
|
|
}
|
|
|
|
#[derive(DeriveIden)]
|
|
enum Workspaces {
|
|
Table,
|
|
Id,
|
|
OrganizationId,
|
|
Name,
|
|
Slug,
|
|
CreatedAt,
|
|
UpdatedAt,
|
|
}
|
|
|
|
#[derive(DeriveIden)]
|
|
enum Organizations {
|
|
Table,
|
|
Id,
|
|
}
|