Skip to content

Commit 4e935f7

Browse files
committed
DB: Created entity migration logic
Made some other tweaks/fixes while testing.
1 parent 50b147c commit 4e935f7

File tree

4 files changed

+128
-12
lines changed

4 files changed

+128
-12
lines changed

database/migrations/2025_09_15_132850_create_entities_table.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public function up(): void
1414
Schema::create('entities', function (Blueprint $table) {
1515
$table->bigIncrements('id');
1616
$table->string('type', 10);
17-
$table->string('slug', 191);
17+
$table->string('name');
18+
$table->string('slug')->index();
1819

1920
$table->unsignedBigInteger('book_id')->nullable()->index();
2021
$table->unsignedInteger('priority')->nullable();
@@ -32,20 +33,20 @@ public function up(): void
3233

3334
Schema::create('entity_container_data', function (Blueprint $table) {
3435
$table->unsignedBigInteger('entity_id');
35-
$table->string('type', 10);
36+
$table->string('entity_type', 10);
3637
$table->text('description');
3738
$table->text('description_html');
3839

3940
$table->unsignedBigInteger('default_template_id')->nullable();
4041
$table->unsignedInteger('image_id')->nullable();
4142
$table->unsignedInteger('sort_rule_id')->nullable();
4243

43-
$table->primary(['entity_id', 'type'], 'entity_container_data_pk');
44+
$table->primary(['entity_id', 'entity_type'], 'entity_container_data_pk');
4445
});
4546

46-
Schema::table('entity_page_data', function (Blueprint $table) {
47+
Schema::create('entity_page_data', function (Blueprint $table) {
4748
$table->unsignedBigInteger('page_id')->primary();
48-
$table->unsignedBigInteger('chapter_id')->index();
49+
$table->unsignedBigInteger('chapter_id')->nullable()->index();
4950

5051
$table->boolean('draft')->index();
5152
$table->boolean('template')->index();
Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22

33
use Illuminate\Database\Migrations\Migration;
4-
use Illuminate\Database\Schema\Blueprint;
5-
use Illuminate\Support\Facades\Schema;
64

75
return new class extends Migration
86
{
@@ -11,16 +9,78 @@
119
*/
1210
public function up(): void
1311
{
14-
// Migrate main data
12+
// Start a transaction to avoid leaving a message DB on error
13+
DB::beginTransaction();
1514

16-
// Fix up data (zeros to nulls, missing relations to nulls, etc)
15+
// Migrate book/shelf data to entities
16+
foreach (['books' => 'book', 'bookshelves' => 'bookshelf'] as $table => $type) {
17+
DB::table('entities')->insertUsing([
18+
'id', 'type', 'name', 'slug', 'created_at', 'updated_at', 'deleted_at', 'created_by', 'updated_by', 'owned_by',
19+
], DB::table($table)->select([
20+
'id', DB::raw("'{$type}'"), 'name', 'slug', 'created_at', 'updated_at', 'deleted_at', 'created_by', 'updated_by', 'owned_by',
21+
]));
22+
}
23+
24+
// Migrate chapter/page data to entities
25+
foreach (['chapters' => 'chapter', 'pages' => 'page'] as $table => $type) {
26+
DB::table('entities')->insertUsing([
27+
'id', 'type', 'name', 'slug', 'book_id', 'priority', 'created_at', 'updated_at', 'deleted_at', 'created_by', 'updated_by', 'owned_by',
28+
], DB::table($table)->select([
29+
'id', DB::raw("'{$type}'"), 'name', 'slug', 'book_id', 'priority', 'created_at', 'updated_at', 'deleted_at', 'created_by', 'updated_by', 'owned_by',
30+
]));
31+
}
32+
33+
// Migrate shelf data to entity_container_data
34+
DB::table('entity_container_data')->insertUsing([
35+
'entity_id', 'entity_type', 'description', 'description_html', 'image_id',
36+
], DB::table('bookshelves')->select([
37+
'id', DB::raw("'bookshelf'"), 'description', 'description_html', 'image_id',
38+
]));
39+
40+
// Migrate book data to entity_container_data
41+
DB::table('entity_container_data')->insertUsing([
42+
'entity_id', 'entity_type', 'description', 'description_html', 'default_template_id', 'image_id', 'sort_rule_id'
43+
], DB::table('books')->select([
44+
'id', DB::raw("'book'"), 'description', 'description_html', 'default_template_id', 'image_id', 'sort_rule_id'
45+
]));
46+
47+
// Migrate chapter data to entity_container_data
48+
DB::table('entity_container_data')->insertUsing([
49+
'entity_id', 'entity_type', 'description', 'description_html', 'default_template_id',
50+
], DB::table('chapters')->select([
51+
'id', DB::raw("'chapter'"), 'description', 'description_html', 'default_template_id',
52+
]));
53+
54+
// Migrate page data to entity_page_data
55+
DB::table('entity_page_data')->insertUsing([
56+
'page_id', 'chapter_id', 'draft', 'template', 'revision_count', 'editor', 'html', 'text', 'markdown',
57+
], DB::table('pages')->select([
58+
'id', 'chapter_id', 'draft', 'template', 'revision_count', 'editor', 'html', 'text', 'markdown',
59+
]));
60+
61+
// Fix up data - Convert 0 id references to null
62+
DB::table('entities')->where('created_by', '=', 0)->update(['created_by' => null]);
63+
DB::table('entities')->where('updated_by', '=', 0)->update(['updated_by' => null]);
64+
DB::table('entities')->where('owned_by', '=', 0)->update(['owned_by' => null]);
65+
DB::table('entity_page_data')->where('chapter_id', '=', 0)->update(['chapter_id' => null]);
66+
67+
// Fix up data - Convert any missing id-based references to null
68+
$userIdQuery = DB::table('users')->select('id');
69+
DB::table('entities')->whereNotIn('created_by', $userIdQuery)->update(['created_by' => null]);
70+
DB::table('entities')->whereNotIn('updated_by', $userIdQuery)->update(['updated_by' => null]);
71+
DB::table('entities')->whereNotIn('owned_by', $userIdQuery)->update(['owned_by' => null]);
72+
DB::table('entity_page_data')->whereNotIn('chapter_id', DB::table('chapters')->select('id'))->update(['chapter_id' => null]);
73+
74+
// Commit our changes within our transaction
75+
DB::commit();
1776
}
1877

1978
/**
2079
* Reverse the migrations.
2180
*/
2281
public function down(): void
2382
{
24-
//
83+
// No action here since the actual data remains in the database for the old tables,
84+
// so data reversion actions are done in a later migration when the old tables are dropped.
2585
}
2686
};

database/migrations/2025_09_15_134751_update_entity_relation_columns.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
*/
1212
public function up(): void
1313
{
14-
//
14+
// TODO
1515
}
1616

1717
/**
1818
* Reverse the migrations.
1919
*/
2020
public function down(): void
2121
{
22-
//
22+
// TODO
2323
}
2424
};

database/migrations/2025_09_15_134813_drop_old_entity_tables.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Query\JoinClause;
45
use Illuminate\Database\Schema\Blueprint;
56
use Illuminate\Support\Facades\Schema;
67

@@ -103,5 +104,59 @@ public function down(): void
103104
$table->unsignedInteger('owned_by')->index();
104105
$table->text('description_html');
105106
});
107+
108+
DB::beginTransaction();
109+
110+
// Revert nulls back to zeros
111+
DB::table('entities')->whereNull('created_by')->update(['created_by' => 0]);
112+
DB::table('entities')->whereNull('updated_by')->update(['updated_by' => 0]);
113+
DB::table('entities')->whereNull('owned_by')->update(['owned_by' => 0]);
114+
DB::table('entity_page_data')->whereNull('chapter_id')->update(['chapter_id' => 0]);
115+
116+
// Restore data back into pages table
117+
$pageFields = [
118+
'id', 'book_id', 'chapter_id', 'name', 'slug', 'html', 'text', 'priority', 'created_at', 'updated_at',
119+
'created_by', 'updated_by', 'draft', 'markdown', 'revision_count', 'template', 'deleted_at', 'owned_by', 'editor'
120+
];
121+
$pageQuery = DB::table('entities')->select($pageFields)
122+
->leftJoin('entity_page_data', 'entities.id', '=', 'entity_page_data.page_id')
123+
->where('type', '=', 'page');
124+
DB::table('pages')->insertUsing($pageFields, $pageQuery);
125+
126+
// Restore data back into chapters table
127+
$containerJoinClause = function (JoinClause $join) {
128+
return $join->on('entities.id', '=', 'entity_container_data.entity_id')
129+
->on('entities.type', '=', 'entity_container_data.entity_type');
130+
};
131+
$chapterFields = [
132+
'id', 'book_id', 'slug', 'name', 'description', 'priority', 'created_at', 'updated_at', 'created_by', 'updated_by',
133+
'deleted_at', 'owned_by', 'description_html', 'default_template_id'
134+
];
135+
$chapterQuery = DB::table('entities')->select($chapterFields)
136+
->leftJoin('entity_container_data', $containerJoinClause)
137+
->where('type', '=', 'chapter');
138+
DB::table('chapters')->insertUsing($chapterFields, $chapterQuery);
139+
140+
// Restore data back into books table
141+
$bookFields = [
142+
'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'image_id',
143+
'deleted_at', 'owned_by', 'default_template_id', 'description_html', 'sort_rule_id'
144+
];
145+
$bookQuery = DB::table('entities')->select($bookFields)
146+
->leftJoin('entity_container_data', $containerJoinClause)
147+
->where('type', '=', 'book');
148+
DB::table('books')->insertUsing($bookFields, $bookQuery);
149+
150+
// Restore data back into bookshelves table
151+
$shelfFields = [
152+
'id', 'name', 'slug', 'description', 'created_by', 'updated_by', 'image_id', 'created_at', 'updated_at',
153+
'deleted_at', 'owned_by', 'description_html',
154+
];
155+
$shelfQuery = DB::table('entities')->select($shelfFields)
156+
->leftJoin('entity_container_data', $containerJoinClause)
157+
->where('type', '=', 'bookshelf');
158+
DB::table('bookshelves')->insertUsing($shelfFields, $shelfQuery);
159+
160+
DB::commit();
106161
}
107162
};

0 commit comments

Comments
 (0)