|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Database\Migrations\Migration; |
| 4 | +use Illuminate\Database\Schema\Blueprint; |
| 5 | +use Illuminate\Support\Facades\DB; |
| 6 | +use Illuminate\Support\Facades\Schema; |
| 7 | + |
| 8 | +return new class extends Migration |
| 9 | +{ |
| 10 | + /** |
| 11 | + * Run the migrations. |
| 12 | + */ |
| 13 | + public function up(): void |
| 14 | + { |
| 15 | + // Create the table for storing slug history |
| 16 | + Schema::create('slug_history', function (Blueprint $table) { |
| 17 | + $table->increments('id'); |
| 18 | + $table->string('sluggable_type', 10)->index(); |
| 19 | + $table->unsignedBigInteger('sluggable_id')->index(); |
| 20 | + $table->string('slug')->index(); |
| 21 | + $table->string('parent_slug')->nullable()->index(); |
| 22 | + $table->timestamps(); |
| 23 | + }); |
| 24 | + |
| 25 | + // Migrate in slugs from page revisions |
| 26 | + $revisionSlugQuery = DB::table('page_revisions') |
| 27 | + ->select([ |
| 28 | + DB::raw('\'page\' as sluggable_type'), |
| 29 | + 'page_id as sluggable_id', |
| 30 | + 'slug', |
| 31 | + 'book_slug as parent_slug', |
| 32 | + DB::raw('min(created_at) as created_at'), |
| 33 | + DB::raw('min(updated_at) as updated_at'), |
| 34 | + ]) |
| 35 | + ->where('type', '=', 'version') |
| 36 | + ->groupBy(['sluggable_id', 'slug', 'parent_slug']); |
| 37 | + |
| 38 | + DB::table('slug_history')->insertUsing( |
| 39 | + ['sluggable_type', 'sluggable_id', 'slug', 'parent_slug', 'created_at', 'updated_at'], |
| 40 | + $revisionSlugQuery, |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Reverse the migrations. |
| 46 | + */ |
| 47 | + public function down(): void |
| 48 | + { |
| 49 | + Schema::dropIfExists('slug_history'); |
| 50 | + } |
| 51 | +}; |
0 commit comments