|
6 | 6 |
|
7 | 7 | return new class extends Migration |
8 | 8 | { |
| 9 | + /** |
| 10 | + * @var array<string, string|array<string>> $columnByTable |
| 11 | + */ |
| 12 | + protected static array $columnByTable = [ |
| 13 | + 'activities' => 'loggable_id', |
| 14 | + 'attachments' => 'uploaded_to', |
| 15 | + 'bookshelves_books' => ['bookshelf_id', 'book_id'], |
| 16 | + 'comments' => 'entity_id', |
| 17 | + 'deletions' => 'deletable_id', |
| 18 | + 'entity_permissions' => 'entity_id', |
| 19 | + 'favourites' => 'favouritable_id', |
| 20 | + 'images' => 'uploaded_to', |
| 21 | + 'joint_permissions' => 'entity_id', |
| 22 | + 'page_revisions' => 'page_id', |
| 23 | + 'references' => ['from_id', 'to_id'], |
| 24 | + 'search_terms' => 'entity_id', |
| 25 | + 'tags' => 'entity_id', |
| 26 | + 'views' => 'viewable_id', |
| 27 | + 'watches' => 'watchable_id', |
| 28 | + ]; |
| 29 | + |
9 | 30 | /** |
10 | 31 | * Run the migrations. |
11 | 32 | */ |
12 | 33 | public function up(): void |
13 | 34 | { |
14 | | - // TODO |
| 35 | + // Drop foreign key constraints |
| 36 | + Schema::table('bookshelves_books', function (Blueprint $table) { |
| 37 | + $table->dropForeign(['book_id']); |
| 38 | + $table->dropForeign(['bookshelf_id']); |
| 39 | + }); |
| 40 | + |
| 41 | + // Update column types to unsigned big integers |
| 42 | + foreach (static::$columnByTable as $table => $column) { |
| 43 | + Schema::table($table, function (Blueprint $table) use ($column) { |
| 44 | + if (is_string($column)) { |
| 45 | + $column = [$column]; |
| 46 | + } |
| 47 | + |
| 48 | + foreach ($column as $col) { |
| 49 | + $table->unsignedBigInteger($col)->change(); |
| 50 | + } |
| 51 | + }); |
| 52 | + } |
15 | 53 | } |
16 | 54 |
|
17 | 55 | /** |
18 | 56 | * Reverse the migrations. |
19 | 57 | */ |
20 | 58 | public function down(): void |
21 | 59 | { |
22 | | - // TODO |
| 60 | + // Revert columns to integers |
| 61 | + foreach (static::$columnByTable as $table => $column) { |
| 62 | + Schema::table($table, function (Blueprint $table) use ($column) { |
| 63 | + if (is_string($column)) { |
| 64 | + $column = [$column]; |
| 65 | + } |
| 66 | + |
| 67 | + foreach ($column as $col) { |
| 68 | + $table->unsignedInteger($col)->change(); |
| 69 | + } |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + // Re-add foreign key constraints |
| 74 | + Schema::table('bookshelves_books', function (Blueprint $table) { |
| 75 | + $table->foreign('bookshelf_id')->references('id')->on('bookshelves') |
| 76 | + ->onUpdate('cascade')->onDelete('cascade'); |
| 77 | + $table->foreign('book_id')->references('id')->on('books') |
| 78 | + ->onUpdate('cascade')->onDelete('cascade'); |
| 79 | + }); |
23 | 80 | } |
24 | 81 | }; |
0 commit comments