]> BookStack Code Mirror - bookstack/blob - database/migrations/2017_03_19_091553_create_search_index_table.php
56281741e3c9f220e9108802c244eb2dc10141b8
[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\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::create('search_terms', function (Blueprint $table) {
16             $table->increments('id');
17             $table->string('term', 180);
18             $table->string('entity_type', 100);
19             $table->integer('entity_id');
20             $table->integer('score');
21
22             $table->index('term');
23             $table->index('entity_type');
24             $table->index(['entity_type', 'entity_id']);
25             $table->index('score');
26         });
27
28         $sm = Schema::getConnection()->getDoctrineSchemaManager();
29         $prefix = DB::getTablePrefix();
30         $pages = $sm->introspectTable($prefix . 'pages');
31         $books = $sm->introspectTable($prefix . 'books');
32         $chapters = $sm->introspectTable($prefix . '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     public function down(): void
60     {
61         // This was removed for v0.24 since these indexes are removed anyway
62         // and will cause issues for db engines that don't support such indexes.
63
64 //        $prefix = DB::getTablePrefix();
65 //        DB::statement("ALTER TABLE {$prefix}pages ADD FULLTEXT search(name, text)");
66 //        DB::statement("ALTER TABLE {$prefix}books ADD FULLTEXT search(name, description)");
67 //        DB::statement("ALTER TABLE {$prefix}chapters ADD FULLTEXT search(name, description)");
68 //        DB::statement("ALTER TABLE {$prefix}pages ADD FULLTEXT name_search(name)");
69 //        DB::statement("ALTER TABLE {$prefix}books ADD FULLTEXT name_search(name)");
70 //        DB::statement("ALTER TABLE {$prefix}chapters ADD FULLTEXT name_search(name)");
71
72         Schema::dropIfExists('search_terms');
73     }
74 };