From 4c2abf54166b6193ade497e624abca04efa1bbde Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Thu, 23 Apr 2020 18:54:54 -0400 Subject: [PATCH] Add migrations for collection improvements --- ...0018_reorganize_anime_collection_media.php | 5 +- ...3646_anime_collection_refactor_cleanup.php | 54 +++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 migrations/20200423163646_anime_collection_refactor_cleanup.php diff --git a/migrations/20200422170018_reorganize_anime_collection_media.php b/migrations/20200422170018_reorganize_anime_collection_media.php index 03b32212..d09949c1 100644 --- a/migrations/20200422170018_reorganize_anime_collection_media.php +++ b/migrations/20200422170018_reorganize_anime_collection_media.php @@ -23,7 +23,9 @@ class ReorganizeAnimeCollectionMedia extends AbstractMigration // Get the old link entries $insertRows = []; - $rows = $this->fetchAll('SELECT hummingbird_id, media_id from anime_set'); + $rows = ($this->table('anime_set')->hasColumn('media_id')) + ? $this->fetchAll('SELECT hummingbird_id, media_id from anime_set') + : []; // Filter the numeric keys out of the row results foreach ($rows as $row) @@ -39,7 +41,6 @@ class ReorganizeAnimeCollectionMedia extends AbstractMigration $insertRows[] = $row; } - // And put them in the new table $linkTable = $this->table('anime_set_media_link'); $linkTable->insert($insertRows)->save(); diff --git a/migrations/20200423163646_anime_collection_refactor_cleanup.php b/migrations/20200423163646_anime_collection_refactor_cleanup.php new file mode 100644 index 00000000..f8b24609 --- /dev/null +++ b/migrations/20200423163646_anime_collection_refactor_cleanup.php @@ -0,0 +1,54 @@ +newMediaTypes as $id => $medium) + { + $moreMediaTypes[] = [ + 'id' => $id + 5, + 'type' => $medium, + ]; + } + $this->table('media')->insert($moreMediaTypes)->save(); + + // Cleanup existing media types a bit + $this->execute("UPDATE media SET type='Bootleg' WHERE id=4"); + $this->execute('DELETE FROM media WHERE id=1'); + + // Remove foreign key and media_id column from anime_set + $animeSet = $this->table('anime_set'); + if ($animeSet->hasColumn('media_id')) + { + $animeSet->dropForeignKey('media_id')->save(); + $animeSet->removeColumn('media_id')->save(); + } + } + + public function down() + { + // Restore the original values for existing media + $this->execute("INSERT INTO media (id, type) VALUES (1, 'DVD & Blu-ray')"); + $this->execute("UPDATE media SET type='Bootleg DVD' WHERE id=4"); + + // Remove the new media types + $values = array_map(fn ($medium) => "'{$medium}'", $this->newMediaTypes); + $valueList = implode(',', $values); + $this->execute("DELETE FROM media WHERE type IN ({$valueList})"); + } +}