-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(database,backend): correct
lot
for import reports
Also bump version.
- Loading branch information
Showing
4 changed files
with
61 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
57 changes: 57 additions & 0 deletions
57
libs/database/src/migrations/m20240608_definitely_final_v5_migration.rs
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,57 @@ | ||
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> { | ||
let db = manager.get_connection(); | ||
db.execute_unprepared( | ||
r#" | ||
DO $$ | ||
DECLARE | ||
record RECORD; | ||
item JSONB; | ||
new_failed_items JSONB; | ||
new_lot TEXT; | ||
BEGIN | ||
FOR record IN SELECT id, details FROM import_report LOOP | ||
new_failed_items := '[]'::jsonb; | ||
FOR item IN SELECT jsonb_array_elements(record.details->'failed_items') LOOP | ||
new_lot := CASE item->>'lot' | ||
WHEN 'AudioBook' THEN 'audio_book' | ||
WHEN 'Anime' THEN 'anime' | ||
WHEN 'Book' THEN 'book' | ||
WHEN 'Podcast' THEN 'podcast' | ||
WHEN 'Manga' THEN 'manga' | ||
WHEN 'Movie' THEN 'movie' | ||
WHEN 'Show' THEN 'show' | ||
WHEN 'VideoGame' THEN 'video_game' | ||
WHEN 'VisualNovel' THEN 'visual_novel' | ||
ELSE item->>'lot' | ||
END; | ||
IF new_lot IS NOT NULL THEN | ||
item := jsonb_set(item, '{lot}', to_jsonb(new_lot)); | ||
END IF; | ||
new_failed_items := new_failed_items || item; | ||
END LOOP; | ||
UPDATE import_report | ||
SET details = jsonb_set(record.details, '{failed_items}', new_failed_items) | ||
WHERE id = record.id; | ||
END LOOP; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
"#, | ||
) | ||
.await?; | ||
Ok(()) | ||
} | ||
|
||
async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> { | ||
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