diff --git a/migrations/src/lib.rs b/migrations/src/lib.rs index 3d190ce1b..2a8bc9fe1 100644 --- a/migrations/src/lib.rs +++ b/migrations/src/lib.rs @@ -14,6 +14,7 @@ mod m20240718_000007_create_file; mod m20240718_000008_create_task; mod m20240718_000009_create_grouppermission; mod m20240718_000010_create_userpermission; +mod m20240726_000001_normalize_schema; #[async_trait::async_trait] impl MigratorTrait for Migrator { @@ -29,6 +30,7 @@ impl MigratorTrait for Migrator { Box::new(m20240718_000008_create_task::Migration), Box::new(m20240718_000009_create_grouppermission::Migration), Box::new(m20240718_000010_create_userpermission::Migration), + Box::new(m20240726_000001_normalize_schema::Migration), ] } } diff --git a/migrations/src/m20240718_000007_create_file.rs b/migrations/src/m20240718_000007_create_file.rs index 83ab4cbf2..9105e7272 100644 --- a/migrations/src/m20240718_000007_create_file.rs +++ b/migrations/src/m20240718_000007_create_file.rs @@ -65,12 +65,6 @@ impl Migration { .not_null(), ) .col(ColumnDef::new(File::ParentId).text()) - .foreign_key( - ForeignKey::create() - .from(File::Table, File::ParentId) - .to(File::Table, File::Id) - .on_delete(ForeignKeyAction::SetNull), - ) .col(ColumnDef::new(File::WorkspaceId).text()) .foreign_key( ForeignKey::create() @@ -79,11 +73,6 @@ impl Migration { .on_delete(ForeignKeyAction::Cascade), ) .col(ColumnDef::new(File::SnapshotId).text()) - .foreign_key( - ForeignKey::create() - .from(File::Table, File::SnapshotId) - .to(Snapshot::Table, Snapshot::Id), - ) .col( ColumnDef::new(File::CreateTime) .text() @@ -122,7 +111,8 @@ impl Migration { .foreign_key( ForeignKey::create() .from(SnapshotFile::Table, SnapshotFile::SnapshotId) - .to(Snapshot::Table, Snapshot::Id), + .to(Snapshot::Table, Snapshot::Id) + .on_delete(ForeignKeyAction::Cascade), ) .col(ColumnDef::new(SnapshotFile::FileId).text()) .foreign_key( diff --git a/migrations/src/m20240726_000001_normalize_schema.rs b/migrations/src/m20240726_000001_normalize_schema.rs new file mode 100644 index 000000000..31eb71ac7 --- /dev/null +++ b/migrations/src/m20240726_000001_normalize_schema.rs @@ -0,0 +1,95 @@ +use sea_orm_migration::prelude::*; + +use crate::models::v1::{File, GroupUser, OrganizationUser, Snapshot, SnapshotFile, Workspace}; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up( + &self, + manager: &SchemaManager, + ) -> Result<(), DbErr> { + manager + .drop_table( + // Remove group_user table if it still exists + Table::drop() + .table(GroupUser::Table) + .if_exists() + .to_owned(), + ) + .await?; + + manager + .drop_table( + // Remove organization_user if it still exists + Table::drop() + .table(OrganizationUser::Table) + .if_exists() + .to_owned(), + ) + .await?; + + manager + .alter_table( + Table::alter() + .table(Workspace::Table) + // Lock root folder if a workspace still actively uses it + .add_foreign_key( + TableForeignKey::new() + .from_tbl(Workspace::Table) + .from_col(Workspace::RootId) + .to_tbl(File::Table) + .to_col(File::Id), + ) + .to_owned(), + ) + .await?; + + manager + .alter_table( + Table::alter() + .table(File::Table) + // Orphan children if a parent is deleted + .add_foreign_key( + TableForeignKey::new() + .from_tbl(File::Table) + .from_col(File::ParentId) + .to_tbl(File::Table) + .to_col(File::Id), + ) + // Create stricter file -> snapshot foreign key constraint to prevent in-use snapshots + // from being deleted + .add_foreign_key( + TableForeignKey::new() + .from_tbl(File::Table) + .from_col(File::SnapshotId) + .to_tbl(Snapshot::Table) + .to_col(Snapshot::Id), + ) + .to_owned(), + ) + .await?; + + // Recreate stricter snapshot_file -> snapshot foreign key constraint to prevent in-use + // snapshots from being deleted. + manager + .alter_table( + Table::alter() + .table(SnapshotFile::Table) + .drop_foreign_key(Alias::new("snapshot_file_snapshot_id_fkey")) + .add_foreign_key( + TableForeignKey::new() + .from_tbl(SnapshotFile::Table) + .from_col(SnapshotFile::SnapshotId) + .to_tbl(Snapshot::Table) + .to_col(Snapshot::Id), + ) + .to_owned(), + ) + .await?; + + Ok(()) + } +} diff --git a/migrations/src/models/v1/group.rs b/migrations/src/models/v1/group.rs index 73fe1d126..eae05c0de 100644 --- a/migrations/src/models/v1/group.rs +++ b/migrations/src/models/v1/group.rs @@ -9,3 +9,8 @@ pub enum Group { CreateTime, UpdateTime, } + +#[derive(Iden)] +pub enum GroupUser { + Table, +} \ No newline at end of file diff --git a/migrations/src/models/v1/organization.rs b/migrations/src/models/v1/organization.rs index ab4cf48d0..db1d08a50 100644 --- a/migrations/src/models/v1/organization.rs +++ b/migrations/src/models/v1/organization.rs @@ -8,3 +8,8 @@ pub enum Organization { CreateTime, UpdateTime, } + +#[derive(Iden)] +pub enum OrganizationUser { + Table, +} \ No newline at end of file