Skip to content

Commit

Permalink
feat(database): new stuff for database columns
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnisDa committed Aug 26, 2024
1 parent 2b5902a commit 5169618
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
17 changes: 15 additions & 2 deletions crates/migrations/src/m20230505_create_review.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@ use sea_orm_migration::prelude::*;
use super::{
m20230410_create_metadata::Metadata, m20230413_create_person::Person,
m20230417_create_user::User, m20230501_create_metadata_group::MetadataGroup,
m20230504_create_collection::Collection,
m20230504_create_collection::Collection, m20230822_create_exercise::Exercise,
};

#[derive(DeriveMigrationName)]
pub struct Migration;

pub static METADATA_TO_REVIEW_FOREIGN_KEY: &str = "review_to_metadata_foreign_key";
pub static PERSON_TO_REVIEW_FOREIGN_KEY: &str = "review_to_person_foreign_key";
pub static METADATA_GROUP_TO_REVIEW_FOREIGN_KEY: &str = "review_to_metadata_group_foreign_key";
pub static COLLECTION_TO_REVIEW_FOREIGN_KEY: &str = "review_to_collection_foreign_key";
pub static EXERCISE_TO_REVIEW_FOREIGN_KEY: &str = "review_to_exercise_foreign_key";
pub static ENTITY_LOT_SQL: &str = indoc! { r#"
GENERATED ALWAYS AS (
CASE
WHEN "metadata_id" IS NOT NULL THEN 'metadata'
WHEN "person_id" IS NOT NULL THEN 'person'
WHEN "metadata_group_id" IS NOT NULL THEN 'metadata_group'
WHEN "collection_id" IS NOT NULL THEN 'collection'
WHEN "exercise_id" IS NOT NULL THEN 'exercise'
END
) STORED
"# };
Expand All @@ -39,6 +42,7 @@ pub enum Review {
PersonId,
MetadataGroupId,
CollectionId,
ExerciseId,
IsSpoiler,
Comments,
ShowExtraInformation,
Expand Down Expand Up @@ -86,6 +90,7 @@ impl MigrationTrait for Migration {
.col(ColumnDef::new(Review::PersonId).text())
.col(ColumnDef::new(Review::MetadataId).text())
.col(ColumnDef::new(Review::UserId).text().not_null())
.col(ColumnDef::new(Review::ExerciseId).text())
.col(
ColumnDef::new(Review::EntityLot)
.text()
Expand All @@ -102,7 +107,7 @@ impl MigrationTrait for Migration {
)
.foreign_key(
ForeignKey::create()
.name("review_to_metadata_foreign_key")
.name(METADATA_TO_REVIEW_FOREIGN_KEY)
.from(Review::Table, Review::MetadataId)
.to(Metadata::Table, Metadata::Id)
.on_delete(ForeignKeyAction::Cascade)
Expand Down Expand Up @@ -132,6 +137,14 @@ impl MigrationTrait for Migration {
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name(EXERCISE_TO_REVIEW_FOREIGN_KEY)
.from(Review::Table, Review::ExerciseId)
.to(Exercise::Table, Exercise::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ BEGIN
WHERE id = rec.id;
END LOOP;
END $$;
"#,
)
.await?;
db.execute_unprepared(
r#"
ALTER TABLE "review" ADD COLUMN IF NOT EXISTS "exercise_id" TEXT;
ALTER TABLE "review" ADD CONSTRAINT IF NOT EXISTS "review_to_exercise_foreign_key" FOREIGN KEY ("exercise_id") REFERENCES "exercise"("id") ON DELETE CASCADE ON UPDATE CASCADE;
"#,
)
.await?;
db.execute_unprepared(
r#"
ALTER TABLE "review" DROP COLUMN IF EXISTS "entity_lot";
ALTER TABLE "review" ADD COLUMN IF NOT EXISTS "entity_lot" TEXT GENERATED ALWAYS AS (
CASE
WHEN "metadata_id" IS NOT NULL THEN 'metadata'
WHEN "person_id" IS NOT NULL THEN 'person'
WHEN "metadata_group_id" IS NOT NULL THEN 'metadata_group'
WHEN "collection_id" IS NOT NULL THEN 'collection'
WHEN "exercise_id" IS NOT NULL THEN 'exercise'
END
) STORED;
"#,
)
.await?;
Expand Down

0 comments on commit 5169618

Please sign in to comment.