|
19 | 19 | use PHPUnit\Framework\Attributes\RequiresPhpExtension;
|
20 | 20 | use PHPUnit\Framework\TestCase;
|
21 | 21 |
|
| 22 | +use function Hyperf\Collection\collect; |
| 23 | + |
22 | 24 | /**
|
23 | 25 | * @internal
|
24 | 26 | * @coversNothing
|
@@ -101,4 +103,101 @@ public function testColumn(): void
|
101 | 103 | $this->assertSame(['id', 'name', 'ranking'], array_column($columns, 'column_name'));
|
102 | 104 | Schema::drop('column_1');
|
103 | 105 | }
|
| 106 | + |
| 107 | + public function index(): void |
| 108 | + { |
| 109 | + Schema::create('users', function (Blueprint $table) { |
| 110 | + $table->string('name'); |
| 111 | + $table->string('email'); |
| 112 | + }); |
| 113 | + |
| 114 | + Schema::table('users', function (Blueprint $table) { |
| 115 | + $table->index(['name', 'email'], 'index1'); |
| 116 | + }); |
| 117 | + |
| 118 | + $indexes = Schema::getIndexListing('users'); |
| 119 | + |
| 120 | + $this->assertContains('index1', $indexes); |
| 121 | + $this->assertNotContains('index2', $indexes); |
| 122 | + |
| 123 | + Schema::table('users', function (Blueprint $table) { |
| 124 | + $table->renameIndex('index1', 'index2'); |
| 125 | + }); |
| 126 | + |
| 127 | + $this->assertFalse(Schema::hasIndex('users', 'index1')); |
| 128 | + $this->assertTrue(collect(Schema::getIndexes('users'))->contains( |
| 129 | + fn ($index) => $index['name'] === 'index2' && $index['columns'] === ['name', 'email'] |
| 130 | + )); |
| 131 | + Schema::create('foo', function (Blueprint $table) { |
| 132 | + $table->id(); |
| 133 | + $table->string('bar'); |
| 134 | + $table->integer('baz'); |
| 135 | + |
| 136 | + $table->unique(['baz', 'bar']); |
| 137 | + }); |
| 138 | + |
| 139 | + $indexes = Schema::getIndexes('foo'); |
| 140 | + |
| 141 | + $this->assertCount(2, $indexes); |
| 142 | + $this->assertTrue(collect($indexes)->contains( |
| 143 | + fn ($index) => $index['columns'] === ['id'] && $index['primary'] |
| 144 | + )); |
| 145 | + $this->assertTrue(collect($indexes)->contains( |
| 146 | + fn ($index) => $index['name'] === 'foo_baz_bar_unique' && $index['columns'] === ['baz', 'bar'] && $index['unique'] |
| 147 | + )); |
| 148 | + $this->assertTrue(Schema::hasIndex('foo', 'foo_baz_bar_unique')); |
| 149 | + $this->assertTrue(Schema::hasIndex('foo', 'foo_baz_bar_unique', 'unique')); |
| 150 | + $this->assertTrue(Schema::hasIndex('foo', ['baz', 'bar'])); |
| 151 | + $this->assertTrue(Schema::hasIndex('foo', ['baz', 'bar'], 'unique')); |
| 152 | + $this->assertFalse(Schema::hasIndex('foo', ['baz', 'bar'], 'primary')); |
| 153 | + Schema::drop('foo'); |
| 154 | + Schema::create('foo', function (Blueprint $table) { |
| 155 | + $table->string('bar')->index('my_index'); |
| 156 | + }); |
| 157 | + |
| 158 | + $indexes = Schema::getIndexes('foo'); |
| 159 | + |
| 160 | + $this->assertCount(1, $indexes); |
| 161 | + $this->assertTrue( |
| 162 | + $indexes[0]['name'] === 'my_index' |
| 163 | + && $indexes[0]['columns'] === ['bar'] |
| 164 | + && ! $indexes[0]['unique'] |
| 165 | + && ! $indexes[0]['primary'] |
| 166 | + ); |
| 167 | + $this->assertTrue(Schema::hasIndex('foo', 'my_index')); |
| 168 | + $this->assertTrue(Schema::hasIndex('foo', ['bar'])); |
| 169 | + $this->assertFalse(Schema::hasIndex('foo', 'my_index', 'primary')); |
| 170 | + $this->assertFalse(Schema::hasIndex('foo', ['bar'], 'unique')); |
| 171 | + Schema::drop('foo'); |
| 172 | + Schema::create('foo', function (Blueprint $table) { |
| 173 | + $table->unsignedBigInteger('key'); |
| 174 | + $table->string('bar')->unique(); |
| 175 | + $table->integer('baz'); |
| 176 | + |
| 177 | + $table->primary(['baz', 'key']); |
| 178 | + }); |
| 179 | + |
| 180 | + $indexes = Schema::getIndexes('foo'); |
| 181 | + |
| 182 | + $this->assertCount(2, $indexes); |
| 183 | + $this->assertTrue(collect($indexes)->contains( |
| 184 | + fn ($index) => $index['columns'] === ['baz', 'key'] && $index['primary'] |
| 185 | + )); |
| 186 | + $this->assertTrue(collect($indexes)->contains( |
| 187 | + fn ($index) => $index['name'] === 'foo_bar_unique' && $index['columns'] === ['bar'] && $index['unique'] |
| 188 | + )); |
| 189 | + Schema::create('articles', function (Blueprint $table) { |
| 190 | + $table->id(); |
| 191 | + $table->string('title', 200); |
| 192 | + $table->text('body'); |
| 193 | + |
| 194 | + $table->fulltext(['body', 'title']); |
| 195 | + }); |
| 196 | + |
| 197 | + $indexes = Schema::getIndexes('articles'); |
| 198 | + |
| 199 | + $this->assertCount(2, $indexes); |
| 200 | + $this->assertTrue(collect($indexes)->contains(fn ($index) => $index['columns'] === ['id'] && $index['primary'])); |
| 201 | + $this->assertTrue(collect($indexes)->contains('name', 'articles_body_title_fulltext')); |
| 202 | + } |
104 | 203 | }
|
0 commit comments