-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(migration): make compatible with initial sql file
- Loading branch information
Showing
5 changed files
with
109 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,8 @@ pub enum Group { | |
CreateTime, | ||
UpdateTime, | ||
} | ||
|
||
#[derive(Iden)] | ||
pub enum GroupUser { | ||
Table, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,8 @@ pub enum Organization { | |
CreateTime, | ||
UpdateTime, | ||
} | ||
|
||
#[derive(Iden)] | ||
pub enum OrganizationUser { | ||
Table, | ||
} |