From d921ae9a5312364471269ff0ddb3752f1894a943 Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 6 Dec 2023 10:36:49 +0000 Subject: [PATCH 1/6] Builder methods --- src/Illuminate/Database/Schema/Builder.php | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/Illuminate/Database/Schema/Builder.php b/src/Illuminate/Database/Schema/Builder.php index 07698e7fc206..3dc37be93112 100755 --- a/src/Illuminate/Database/Schema/Builder.php +++ b/src/Illuminate/Database/Schema/Builder.php @@ -289,6 +289,40 @@ public function whenTableDoesntHaveColumn(string $table, string $column, Closure } } + /** + * Determine if the given table has a given column. + * + * @param string $table + * @param string $index + * @return bool + */ + public function hasIndex($table, $index) + { + return in_array( + strtolower($index), array_map('strtolower', $this->getIndexes($table)) + ); + } + + /** + * Determine if the given table has given columns. + * + * @param string $table + * @param array $indexes + * @return bool + */ + public function hasIndexes($table, array $indexes) + { + $tableIndexes = array_map('strtolower', $this->getIndexes($table)); + + foreach ($tableIndexes as $tableIndex) { + if (! in_array(strtolower($tableIndex), $indexes)) { + return false; + } + } + + return true; + } + /** * Get the data type for the given column name. * From d3633d2fe875bfb8dd54968d40a31dbb8f67496a Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 6 Dec 2023 10:36:57 +0000 Subject: [PATCH 2/6] Schema facade --- src/Illuminate/Support/Facades/Schema.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Illuminate/Support/Facades/Schema.php b/src/Illuminate/Support/Facades/Schema.php index 1bf87ba9a151..7ecad4df8048 100755 --- a/src/Illuminate/Support/Facades/Schema.php +++ b/src/Illuminate/Support/Facades/Schema.php @@ -16,6 +16,8 @@ * @method static array getViews() * @method static bool hasColumn(string $table, string $column) * @method static bool hasColumns(string $table, array $columns) + * @method static bool hasIndex(string $table, string $column) + * @method static bool hasIndexes(string $table, array $columns) * @method static void whenTableHasColumn(string $table, string $column, \Closure $callback) * @method static void whenTableDoesntHaveColumn(string $table, string $column, \Closure $callback) * @method static string getColumnType(string $table, string $column, bool $fullDefinition = false) From 0679fe815a2f70f7bc2a843c550b5bbf75b544f5 Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 6 Dec 2023 10:37:07 +0000 Subject: [PATCH 3/6] Builder tests --- tests/Database/DatabaseSchemaBuilderTest.php | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/Database/DatabaseSchemaBuilderTest.php b/tests/Database/DatabaseSchemaBuilderTest.php index e2619c667447..bb1a3a4d3453 100755 --- a/tests/Database/DatabaseSchemaBuilderTest.php +++ b/tests/Database/DatabaseSchemaBuilderTest.php @@ -71,6 +71,30 @@ public function testTableHasColumns() $this->assertFalse($builder->hasColumns('users', ['id', 'address'])); } + public function testTableHasIndex() + { + $connection = m::mock(Connection::class); + $grammar = m::mock(stdClass::class); + $connection->shouldReceive('getSchemaGrammar')->andReturn($grammar); + $builder = m::mock(Builder::class.'[getIndexes]', [$connection]); + $builder->shouldReceive('getIndexes')->with('users')->twice()->andReturn(['id', 'firstname']); + + $this->assertTrue($builder->hasIndex('users', 'id')); + $this->assertFalse($builder->hasIndex('users', 'address')); + } + + public function testTableHasIndexes() + { + $connection = m::mock(Connection::class); + $grammar = m::mock(stdClass::class); + $connection->shouldReceive('getSchemaGrammar')->andReturn($grammar); + $builder = m::mock(Builder::class.'[getIndexes]', [$connection]); + $builder->shouldReceive('getIndexes')->with('users')->twice()->andReturn(['id', 'firstname']); + + $this->assertTrue($builder->hasIndexes('users', ['id', 'firstname'])); + $this->assertFalse($builder->hasIndexes('users', ['id', 'address'])); + } + public function testGetColumnTypeAddsPrefix() { $connection = m::mock(Connection::class); From fc9d261264d4d432be4ccb3e231f594030fe14b3 Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 6 Dec 2023 11:40:12 +0000 Subject: [PATCH 4/6] Test Tweak --- tests/Database/DatabaseSchemaBuilderTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Database/DatabaseSchemaBuilderTest.php b/tests/Database/DatabaseSchemaBuilderTest.php index bb1a3a4d3453..507fa9b734ec 100755 --- a/tests/Database/DatabaseSchemaBuilderTest.php +++ b/tests/Database/DatabaseSchemaBuilderTest.php @@ -77,9 +77,10 @@ public function testTableHasIndex() $grammar = m::mock(stdClass::class); $connection->shouldReceive('getSchemaGrammar')->andReturn($grammar); $builder = m::mock(Builder::class.'[getIndexes]', [$connection]); - $builder->shouldReceive('getIndexes')->with('users')->twice()->andReturn(['id', 'firstname']); + $builder->shouldReceive('getIndexes')->with('users')->times(3)->andReturn(['id', 'firstname']); $this->assertTrue($builder->hasIndex('users', 'id')); + $this->assertTrue($builder->hasIndex('users', 'firstname')); $this->assertFalse($builder->hasIndex('users', 'address')); } From ed4b40ef792aa06139b698355d8114dda1d83026 Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 6 Dec 2023 11:52:32 +0000 Subject: [PATCH 5/6] Fixes --- src/Illuminate/Database/Schema/Builder.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Database/Schema/Builder.php b/src/Illuminate/Database/Schema/Builder.php index 3dc37be93112..38ce4a6c7f66 100755 --- a/src/Illuminate/Database/Schema/Builder.php +++ b/src/Illuminate/Database/Schema/Builder.php @@ -299,7 +299,7 @@ public function whenTableDoesntHaveColumn(string $table, string $column, Closure public function hasIndex($table, $index) { return in_array( - strtolower($index), array_map('strtolower', $this->getIndexes($table)) + strtolower($index), array_map('strtolower', $this->getIndexListing($table)) ); } @@ -312,10 +312,10 @@ public function hasIndex($table, $index) */ public function hasIndexes($table, array $indexes) { - $tableIndexes = array_map('strtolower', $this->getIndexes($table)); + $tableIndexes = array_map('strtolower', $this->getIndexListing($table)); - foreach ($tableIndexes as $tableIndex) { - if (! in_array(strtolower($tableIndex), $indexes)) { + foreach ($indexes as $index) { + if (! in_array(strtolower($index), $tableIndexes)) { return false; } } @@ -361,6 +361,17 @@ public function getColumnListing($table) return array_column($this->getColumns($table), 'name'); } + /** + * Get the index listing for a given table. + * + * @param string $table + * @return array + */ + public function getIndexListing($table) + { + return array_column($this->getIndexes($table), 'name'); + } + /** * Get the columns for a given table. * From a9babc82979d98c36e156aab8219b518ad8d9503 Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 6 Dec 2023 11:53:22 +0000 Subject: [PATCH 6/6] Fix tests --- tests/Database/DatabaseSchemaBuilderTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Database/DatabaseSchemaBuilderTest.php b/tests/Database/DatabaseSchemaBuilderTest.php index 507fa9b734ec..4a748456bd38 100755 --- a/tests/Database/DatabaseSchemaBuilderTest.php +++ b/tests/Database/DatabaseSchemaBuilderTest.php @@ -76,8 +76,8 @@ public function testTableHasIndex() $connection = m::mock(Connection::class); $grammar = m::mock(stdClass::class); $connection->shouldReceive('getSchemaGrammar')->andReturn($grammar); - $builder = m::mock(Builder::class.'[getIndexes]', [$connection]); - $builder->shouldReceive('getIndexes')->with('users')->times(3)->andReturn(['id', 'firstname']); + $builder = m::mock(Builder::class.'[getIndexListing]', [$connection]); + $builder->shouldReceive('getIndexListing')->with('users')->times(3)->andReturn(['id', 'firstname']); $this->assertTrue($builder->hasIndex('users', 'id')); $this->assertTrue($builder->hasIndex('users', 'firstname')); @@ -89,8 +89,8 @@ public function testTableHasIndexes() $connection = m::mock(Connection::class); $grammar = m::mock(stdClass::class); $connection->shouldReceive('getSchemaGrammar')->andReturn($grammar); - $builder = m::mock(Builder::class.'[getIndexes]', [$connection]); - $builder->shouldReceive('getIndexes')->with('users')->twice()->andReturn(['id', 'firstname']); + $builder = m::mock(Builder::class.'[getIndexListing]', [$connection]); + $builder->shouldReceive('getIndexListing')->with('users')->twice()->andReturn(['id', 'firstname']); $this->assertTrue($builder->hasIndexes('users', ['id', 'firstname'])); $this->assertFalse($builder->hasIndexes('users', ['id', 'address']));