]> BookStack Code Mirror - bookstack/commitdiff
Fixed shelf covers being stored as 'cover_book'
authorDan Brown <redacted>
Fri, 2 Sep 2022 11:54:54 +0000 (12:54 +0100)
committerDan Brown <redacted>
Fri, 2 Sep 2022 11:54:54 +0000 (12:54 +0100)
Are now stored as 'cover_bookshelf' as expected.
Added a migrate to alter existing shelf cover image types.

app/Entities/Models/Bookshelf.php
app/Entities/Repos/BaseRepo.php
database/migrations/2022_09_02_082910_fix_shelf_cover_image_types.php [new file with mode: 0644]
tests/Entity/BookShelfTest.php

index b2dab252a05decab60f407b0fe68541b92f9063e..cdc6648f96cddb43109eb0eade96dd91726bc0a7 100644 (file)
@@ -86,7 +86,7 @@ class Bookshelf extends Entity implements HasCoverImage
      */
     public function coverImageTypeKey(): string
     {
-        return 'cover_shelf';
+        return 'cover_bookshelf';
     }
 
     /**
index 747d1b17644cbf0846f8176b622c62338bd79fc5..da939e10281792ffbd78a9e4fa030470f4e2b0f8 100644 (file)
@@ -86,8 +86,9 @@ class BaseRepo
     public function updateCoverImage($entity, ?UploadedFile $coverImage, bool $removeImage = false)
     {
         if ($coverImage) {
+            $imageType = $entity->coverImageTypeKey();
             $this->imageRepo->destroyImage($entity->cover);
-            $image = $this->imageRepo->saveNew($coverImage, 'cover_book', $entity->id, 512, 512, true);
+            $image = $this->imageRepo->saveNew($coverImage, $imageType, $entity->id, 512, 512, true);
             $entity->cover()->associate($image);
             $entity->save();
         }
diff --git a/database/migrations/2022_09_02_082910_fix_shelf_cover_image_types.php b/database/migrations/2022_09_02_082910_fix_shelf_cover_image_types.php
new file mode 100644 (file)
index 0000000..837bdfe
--- /dev/null
@@ -0,0 +1,42 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Support\Facades\DB;
+
+class FixShelfCoverImageTypes extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        // This updates the 'type' field for images, uploaded as shelf cover images,
+        // to be cover_bookshelf instead of cover_book.
+        // This does not fix their paths, since fixing that requires a more complicated operation,
+        // but their path does not affect functionality at time of this fix.
+
+        $shelfImageIds = DB::table('bookshelves')
+            ->whereNotNull('image_id')
+            ->pluck('image_id')
+            ->values()->all();
+
+        DB::table('images')
+            ->where('type', '=', 'cover_book')
+            ->whereIn('id', $shelfImageIds)
+            ->update(['type' => 'cover_bookshelf']);
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        DB::table('images')
+            ->where('type', '=', 'cover_bookshelf')
+            ->update(['type' => 'cover_book']);
+    }
+}
index 5a7107ff0967166e3addd2c8a5cd7e7741a74609..44d30a548a062ac9559aef2e552761d34fa61c47 100644 (file)
@@ -125,6 +125,7 @@ class BookShelfTest extends TestCase
             'image_id' => $lastImage->id,
         ]);
         $this->assertEquals($lastImage->id, $shelf->cover->id);
+        $this->assertEquals('cover_bookshelf', $lastImage->type);
     }
 
     public function test_shelf_view()