]> BookStack Code Mirror - bookstack/blob - database/migrations/2017_03_19_091553_create_search_index_table.php
03a63392afd6817d5b1a0ea8b60c8a29b636fea0
[bookstack] / database / migrations / 2017_03_19_091553_create_search_index_table.php
1 <?php
2
3 use Illuminate\Database\Migrations\Migration;
4 use Illuminate\Database\Schema\Blueprint;
5 use Illuminate\Support\Facades\Schema;
6
7 return new class extends Migration
8 {
9     /**
10      * Run the migrations.
11      *
12      * @return void
13      */
14     public function up()
15     {
16         Schema::create('search_terms', function (Blueprint $table) {
17             $table->increments('id');
18             $table->string('term', 180);
19             $table->string('entity_type', 100);
20             $table->integer('entity_id');
21             $table->integer('score');
22
23             $table->index('term');
24             $table->index('entity_type');
25             $table->index(['entity_type', 'entity_id']);
26             $table->index('score');
27         });
28
29         $sm = Schema::getConnection()->getDoctrineSchemaManager();
30         $pages = $sm->listTableDetails('pages');
31         $books = $sm->listTableDetails('books');
32         $chapters = $sm->listTableDetails('chapters');
33
34         if ($pages->hasIndex('search')) {
35             Schema::table('pages', function (Blueprint $table) {
36                 $table->dropIndex('search');
37                 $table->dropIndex('name_search');
38             });
39         }
40
41         if ($books->hasIndex('search')) {
42             Schema::table('books', function (Blueprint $table) {
43                 $table->dropIndex('search');
44                 $table->dropIndex('name_search');
45             });
46         }
47
48         if ($chapters->hasIndex('search')) {
49             Schema::table('chapters', function (Blueprint $table) {
50                 $table->dropIndex('search');
51                 $table->dropIndex('name_search');
52             });
53         }
54     }
55
56     /**
57      * Reverse the migrations.
58      *
59      * @return void
60      */
61     public function down()
62     {
63         // This was removed for v0.24 since these indexes are removed anyway
64         // and will cause issues for db engines that don't support such indexes.
65
66 //        $prefix = DB::getTablePrefix();
67 //        DB::statement("ALTER TABLE {$prefix}pages ADD FULLTEXT search(name, text)");
68 //        DB::statement("ALTER TABLE {$prefix}books ADD FULLTEXT search(name, description)");
69 //        DB::statement("ALTER TABLE {$prefix}chapters ADD FULLTEXT search(name, description)");
70 //        DB::statement("ALTER TABLE {$prefix}pages ADD FULLTEXT name_search(name)");
71 //        DB::statement("ALTER TABLE {$prefix}books ADD FULLTEXT name_search(name)");
72 //        DB::statement("ALTER TABLE {$prefix}chapters ADD FULLTEXT name_search(name)");
73
74         Schema::dropIfExists('search_terms');
75     }
76 };