|
1 | 1 | <?php |
2 | 2 |
|
3 | 3 | use Illuminate\Database\Migrations\Migration; |
4 | | -use Illuminate\Database\Schema\Blueprint; |
5 | | -use Illuminate\Support\Facades\Schema; |
6 | 4 |
|
7 | 5 | return new class extends Migration |
8 | 6 | { |
|
11 | 9 | */ |
12 | 10 | public function up(): void |
13 | 11 | { |
14 | | - // Migrate main data |
| 12 | + // Start a transaction to avoid leaving a message DB on error |
| 13 | + DB::beginTransaction(); |
15 | 14 |
|
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(); |
17 | 76 | } |
18 | 77 |
|
19 | 78 | /** |
20 | 79 | * Reverse the migrations. |
21 | 80 | */ |
22 | 81 | public function down(): void |
23 | 82 | { |
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. |
25 | 85 | } |
26 | 86 | }; |
0 commit comments