]> BookStack Code Mirror - bookstack/blob - database/migrations/2017_04_20_185112_add_revision_counts.php
Guest create page: name field autofocus
[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\Schema;
6
7 class AddRevisionCounts extends Migration
8 {
9     /**
10      * Run the migrations.
11      *
12      * @return void
13      */
14     public function up()
15     {
16         Schema::table('pages', function (Blueprint $table) {
17             $table->integer('revision_count');
18         });
19         Schema::table('page_revisions', function (Blueprint $table) {
20             $table->integer('revision_number');
21             $table->index('revision_number');
22         });
23
24         // Update revision count
25         $pTable = DB::getTablePrefix() . 'pages';
26         $rTable = DB::getTablePrefix() . 'page_revisions';
27         DB::statement("UPDATE ${pTable} SET ${pTable}.revision_count=(SELECT count(*) FROM ${rTable} WHERE ${rTable}.page_id=${pTable}.id)");
28     }
29
30     /**
31      * Reverse the migrations.
32      *
33      * @return void
34      */
35     public function down()
36     {
37         Schema::table('pages', function (Blueprint $table) {
38             $table->dropColumn('revision_count');
39         });
40         Schema::table('page_revisions', function (Blueprint $table) {
41             $table->dropColumn('revision_number');
42         });
43     }
44 }