Skip to content

Commit

Permalink
fix(migration): make compatible with initial sql file
Browse files Browse the repository at this point in the history
  • Loading branch information
dsonck92 committed Jul 26, 2024
1 parent 07308f7 commit eaf1544
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 12 deletions.
2 changes: 2 additions & 0 deletions migrations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
]
}
}
14 changes: 2 additions & 12 deletions migrations/src/m20240718_000007_create_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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(
Expand Down
95 changes: 95 additions & 0 deletions migrations/src/m20240726_000001_normalize_schema.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}
5 changes: 5 additions & 0 deletions migrations/src/models/v1/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ pub enum Group {
CreateTime,
UpdateTime,
}

#[derive(Iden)]
pub enum GroupUser {
Table,
}
5 changes: 5 additions & 0 deletions migrations/src/models/v1/organization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ pub enum Organization {
CreateTime,
UpdateTime,
}

#[derive(Iden)]
pub enum OrganizationUser {
Table,
}

0 comments on commit eaf1544

Please sign in to comment.