Skip to content

Commit bc49db7

Browse files
committed
DB: Added change of entity relation columns to suit new entities table
1 parent 4e935f7 commit bc49db7

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

database/migrations/2025_09_15_134751_update_entity_relation_columns.php

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,76 @@
66

77
return new class extends Migration
88
{
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+
930
/**
1031
* Run the migrations.
1132
*/
1233
public function up(): void
1334
{
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+
}
1553
}
1654

1755
/**
1856
* Reverse the migrations.
1957
*/
2058
public function down(): void
2159
{
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+
});
2380
}
2481
};

database/migrations/2025_09_15_134813_drop_old_entity_tables.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function down(): void
4848
$table->string('editor', 50)->default('');
4949
});
5050

51-
Schema::table('chapters', function (Blueprint $table) {
51+
Schema::create('chapters', function (Blueprint $table) {
5252
$table->unsignedInteger('id', true)->primary();
5353
$table->integer('book_id')->index();
5454
$table->string('slug')->index();

0 commit comments

Comments
 (0)