]> BookStack Code Mirror - bookstack/blob - database/migrations/2017_04_20_185112_add_revision_counts.php
Deps: Updated PHP composer dependancy versions, fixed test namespaces
[bookstack] / database / migrations / 2017_04_20_185112_add_revision_counts.php
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         Schema::table('pages', function (Blueprint $table) {
16             $table->integer('revision_count');
17         });
18         Schema::table('page_revisions', function (Blueprint $table) {
19             $table->integer('revision_number');
20             $table->index('revision_number');
21         });
22
23         // Update revision count
24         $pTable = DB::getTablePrefix() . 'pages';
25         $rTable = DB::getTablePrefix() . 'page_revisions';
26         DB::statement("UPDATE {$pTable} SET {$pTable}.revision_count=(SELECT count(*) FROM {$rTable} WHERE {$rTable}.page_id={$pTable}.id)");
27     }
28
29     /**
30      * Reverse the migrations.
31      */
32     public function down(): void
33     {
34         Schema::table('pages', function (Blueprint $table) {
35             $table->dropColumn('revision_count');
36         });
37         Schema::table('page_revisions', function (Blueprint $table) {
38             $table->dropColumn('revision_number');
39         });
40     }
41 };