2
2
3
3
declare (strict_types=1 );
4
4
5
- use Illuminate \Database \Migrations \Migration ;
5
+ namespace App \Http \Controllers ;
6
+
6
7
use Illuminate \Support \Facades \Schema ;
8
+ use MongoDB \Collection ;
7
9
use MongoDB \Laravel \Schema \Blueprint ;
10
+ use MongoDB \Laravel \Tests \TestCase ;
8
11
9
- return new class extends Migration
10
- {
11
- protected $ connection = 'mongodb ' ;
12
+ use function assert ;
12
13
13
- public function up (): void
14
+ class AtlasIdxSchemaBuilderTest extends TestCase
15
+ {
16
+ /**
17
+ * @runInSeparateProcess
18
+ * @preserveGlobalState disabled
19
+ */
20
+ public function testAtlasSearchIdx (): void
14
21
{
15
22
// begin-create-search-indexes
16
23
Schema::create ('galaxies ' , function (Blueprint $ collection ) {
@@ -33,6 +40,22 @@ public function up(): void
33
40
});
34
41
// end-create-search-indexes
35
42
43
+ $ index = $ this ->getSearchIndex ('galaxies ' , 'dynamic_index ' );
44
+ self ::assertNotNull ($ index );
45
+
46
+ self ::assertSame ('dynamic_index ' , $ index ['name ' ]);
47
+ self ::assertSame ('search ' , $ index ['type ' ]);
48
+ self ::assertTrue ($ index ['latestDefinition ' ]['mappings ' ]['dynamic ' ]);
49
+
50
+ $ index = $ this ->getSearchIndex ('galaxies ' , 'auto_index ' );
51
+ self ::assertNotNull ($ index );
52
+
53
+ self ::assertSame ('auto_index ' , $ index ['name ' ]);
54
+ self ::assertSame ('search ' , $ index ['type ' ]);
55
+ }
56
+
57
+ public function testVectorSearchIdx (): void
58
+ {
36
59
// begin-create-vs-index
37
60
Schema::create ('galaxies ' , function (Blueprint $ collection ) {
38
61
$ collection ->vectorSearchIndex ([
@@ -47,14 +70,50 @@ public function up(): void
47
70
], 'vs_index ' );
48
71
});
49
72
// end-create-vs-index
73
+
74
+ $ index = $ this ->getSearchIndex ('galaxies ' , 'vs_index ' );
75
+ self ::assertNotNull ($ index );
76
+
77
+ self ::assertSame ('vs_index ' , $ index ['name ' ]);
78
+ self ::assertSame ('vectorSearch ' , $ index ['type ' ]);
79
+ self ::assertSame ('vector ' , $ index ['latestDefinition ' ]['fields ' ][0 ]['type ' ]);
50
80
}
51
81
52
- public function down (): void
82
+ public function testDropIndexes (): void
53
83
{
54
84
// begin-drop-search-index
55
85
Schema::table ('galaxies ' , function (Blueprint $ collection ) {
56
86
$ collection ->dropSearchIndex ('auto_index ' );
57
87
});
58
88
// end-drop-search-index
89
+
90
+ Schema::table ('galaxies ' , function (Blueprint $ collection ) {
91
+ $ collection ->dropSearchIndex ('dynamic_index ' );
92
+ });
93
+
94
+ Schema::table ('galaxies ' , function (Blueprint $ collection ) {
95
+ $ collection ->dropSearchIndex ('vs_index ' );
96
+ });
97
+
98
+ $ index = $ this ->getSearchIndex ('galaxies ' , 'auto_index ' );
99
+ self ::assertNull ($ index );
100
+
101
+ $ index = $ this ->getSearchIndex ('galaxies ' , 'dynamic_index ' );
102
+ self ::assertNull ($ index );
103
+
104
+ $ index = $ this ->getSearchIndex ('galaxies ' , 'vs_index ' );
105
+ self ::assertNull ($ index );
106
+ }
107
+
108
+ protected function getSearchIndex (string $ collection , string $ name ): ?array
109
+ {
110
+ $ collection = $ this ->getConnection ('mongodb ' )->getCollection ($ collection );
111
+ assert ($ collection instanceof Collection);
112
+
113
+ foreach ($ collection ->listSearchIndexes (['name ' => $ name , 'typeMap ' => ['root ' => 'array ' , 'array ' => 'array ' , 'document ' => 'array ' ]]) as $ index ) {
114
+ return $ index ;
115
+ }
116
+
117
+ return null ;
59
118
}
60
- };
119
+ }
0 commit comments