From b882f78d068008e6caf57c06e8e42b91fd6505ec Mon Sep 17 00:00:00 2001 From: Antonio de la Vega Date: Thu, 27 Jan 2022 16:49:38 +0100 Subject: [PATCH 1/6] feat(Postgres): Add full text search index types --- src/Phinx/Db/Adapter/PostgresAdapter.php | 11 ++++++++++- tests/Phinx/Db/Adapter/PostgresAdapterTest.php | 13 +++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Phinx/Db/Adapter/PostgresAdapter.php b/src/Phinx/Db/Adapter/PostgresAdapter.php index b9b637d94..38fa50969 100644 --- a/src/Phinx/Db/Adapter/PostgresAdapter.php +++ b/src/Phinx/Db/Adapter/PostgresAdapter.php @@ -35,6 +35,8 @@ class PostgresAdapter extends PdoAdapter self::PHINX_TYPE_BINARYUUID, ]; + const FULLTEXT_INDEX_TYPES = ['gin', 'gist']; + /** * Columns with comments * @@ -1264,8 +1266,15 @@ protected function getIndexSqlDefinition(Index $index, $tableName) $includedColumns = $index->getInclude() ? sprintf('INCLUDE ("%s")', implode('","', $index->getInclude())) : ''; + $createIndexSentence = 'CREATE %s INDEX %s ON %s '; + if(in_array($index->getType(), self::FULLTEXT_INDEX_TYPES, true)) { + $createIndexSentence .= ' USING ' . $index->getType() .'(%s) %s;'; + } else { + $createIndexSentence .= '(%s) %s;'; + } + return sprintf( - 'CREATE %s INDEX %s ON %s (%s) %s;', + $createIndexSentence, ($index->getType() === Index::UNIQUE ? 'UNIQUE' : ''), $this->quoteColumnName($indexName), $this->quoteTableName($tableName), diff --git a/tests/Phinx/Db/Adapter/PostgresAdapterTest.php b/tests/Phinx/Db/Adapter/PostgresAdapterTest.php index 5c89ff589..8a5cfcda5 100644 --- a/tests/Phinx/Db/Adapter/PostgresAdapterTest.php +++ b/tests/Phinx/Db/Adapter/PostgresAdapterTest.php @@ -352,6 +352,19 @@ public function testCreateTableWithUniqueIndexes() $this->assertFalse($this->adapter->hasIndex('table1', ['email', 'user_email'])); } + public function testCreateTableWithFullTextSearchIndexes() + { + $table = new \Phinx\Db\Table('table1', [], $this->adapter); + $table->addColumn('names', 'jsonb') + ->addColumn('texts', 'jsonb') + ->addIndex('names', ['gin' => true]) + ->addIndex('texts', ['gist' => true]) + ->save(); + + $this->assertTrue($this->adapter->hasIndex('table1', ['names'])); + $this->assertTrue($this->adapter->hasIndex('table1', ['texts'])); + } + public function testCreateTableWithNamedIndexes() { $table = new \Phinx\Db\Table('table1', [], $this->adapter); From bff508c3167c3721d87ef7edef902d3c16fba93f Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Thu, 27 Jan 2022 15:54:28 +0000 Subject: [PATCH 2/6] Fixing style errors. --- src/Phinx/Db/Adapter/PostgresAdapter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Phinx/Db/Adapter/PostgresAdapter.php b/src/Phinx/Db/Adapter/PostgresAdapter.php index 38fa50969..06690c53e 100644 --- a/src/Phinx/Db/Adapter/PostgresAdapter.php +++ b/src/Phinx/Db/Adapter/PostgresAdapter.php @@ -1267,8 +1267,8 @@ protected function getIndexSqlDefinition(Index $index, $tableName) $includedColumns = $index->getInclude() ? sprintf('INCLUDE ("%s")', implode('","', $index->getInclude())) : ''; $createIndexSentence = 'CREATE %s INDEX %s ON %s '; - if(in_array($index->getType(), self::FULLTEXT_INDEX_TYPES, true)) { - $createIndexSentence .= ' USING ' . $index->getType() .'(%s) %s;'; + if (in_array($index->getType(), self::FULLTEXT_INDEX_TYPES, true)) { + $createIndexSentence .= ' USING ' . $index->getType() . '(%s) %s;'; } else { $createIndexSentence .= '(%s) %s;'; } From 498adcd99fe873ab380e19fd598ec190fc14d97f Mon Sep 17 00:00:00 2001 From: Antonio de la Vega Date: Thu, 27 Jan 2022 17:22:56 +0100 Subject: [PATCH 3/6] fix(Postgres): Update test. removes gist support --- src/Phinx/Db/Adapter/PostgresAdapter.php | 4 ++-- tests/Phinx/Db/Adapter/PostgresAdapterTest.php | 5 +---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Phinx/Db/Adapter/PostgresAdapter.php b/src/Phinx/Db/Adapter/PostgresAdapter.php index 38fa50969..73014628a 100644 --- a/src/Phinx/Db/Adapter/PostgresAdapter.php +++ b/src/Phinx/Db/Adapter/PostgresAdapter.php @@ -35,7 +35,7 @@ class PostgresAdapter extends PdoAdapter self::PHINX_TYPE_BINARYUUID, ]; - const FULLTEXT_INDEX_TYPES = ['gin', 'gist']; + const GIN_INDEX_TYPE = 'gin'; /** * Columns with comments @@ -1267,7 +1267,7 @@ protected function getIndexSqlDefinition(Index $index, $tableName) $includedColumns = $index->getInclude() ? sprintf('INCLUDE ("%s")', implode('","', $index->getInclude())) : ''; $createIndexSentence = 'CREATE %s INDEX %s ON %s '; - if(in_array($index->getType(), self::FULLTEXT_INDEX_TYPES, true)) { + if($index->getType() === self::GIN_INDEX_TYPE) { $createIndexSentence .= ' USING ' . $index->getType() .'(%s) %s;'; } else { $createIndexSentence .= '(%s) %s;'; diff --git a/tests/Phinx/Db/Adapter/PostgresAdapterTest.php b/tests/Phinx/Db/Adapter/PostgresAdapterTest.php index 8a5cfcda5..47eded3cb 100644 --- a/tests/Phinx/Db/Adapter/PostgresAdapterTest.php +++ b/tests/Phinx/Db/Adapter/PostgresAdapterTest.php @@ -356,13 +356,10 @@ public function testCreateTableWithFullTextSearchIndexes() { $table = new \Phinx\Db\Table('table1', [], $this->adapter); $table->addColumn('names', 'jsonb') - ->addColumn('texts', 'jsonb') - ->addIndex('names', ['gin' => true]) - ->addIndex('texts', ['gist' => true]) + ->addIndex('names', ['type' => 'gin']) ->save(); $this->assertTrue($this->adapter->hasIndex('table1', ['names'])); - $this->assertTrue($this->adapter->hasIndex('table1', ['texts'])); } public function testCreateTableWithNamedIndexes() From 35ee97dab08fab673fc0073f549040317441c94f Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Thu, 27 Jan 2022 16:24:43 +0000 Subject: [PATCH 4/6] Fixing style errors. --- src/Phinx/Db/Adapter/PostgresAdapter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Phinx/Db/Adapter/PostgresAdapter.php b/src/Phinx/Db/Adapter/PostgresAdapter.php index 73014628a..fe0c88027 100644 --- a/src/Phinx/Db/Adapter/PostgresAdapter.php +++ b/src/Phinx/Db/Adapter/PostgresAdapter.php @@ -1267,8 +1267,8 @@ protected function getIndexSqlDefinition(Index $index, $tableName) $includedColumns = $index->getInclude() ? sprintf('INCLUDE ("%s")', implode('","', $index->getInclude())) : ''; $createIndexSentence = 'CREATE %s INDEX %s ON %s '; - if($index->getType() === self::GIN_INDEX_TYPE) { - $createIndexSentence .= ' USING ' . $index->getType() .'(%s) %s;'; + if ($index->getType() === self::GIN_INDEX_TYPE) { + $createIndexSentence .= ' USING ' . $index->getType() . '(%s) %s;'; } else { $createIndexSentence .= '(%s) %s;'; } From 12179a84456784058a4d104d601d0c188a2b3a54 Mon Sep 17 00:00:00 2001 From: Antonio de la Vega Date: Thu, 27 Jan 2022 17:27:58 +0100 Subject: [PATCH 5/6] fix(Postgres): Add visibility to constant --- src/Phinx/Db/Adapter/PostgresAdapter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Phinx/Db/Adapter/PostgresAdapter.php b/src/Phinx/Db/Adapter/PostgresAdapter.php index fe0c88027..f510ba97d 100644 --- a/src/Phinx/Db/Adapter/PostgresAdapter.php +++ b/src/Phinx/Db/Adapter/PostgresAdapter.php @@ -35,7 +35,7 @@ class PostgresAdapter extends PdoAdapter self::PHINX_TYPE_BINARYUUID, ]; - const GIN_INDEX_TYPE = 'gin'; + private const GIN_INDEX_TYPE = 'gin'; /** * Columns with comments From 31d79dc3d4309edb03f616f551bcab3901761273 Mon Sep 17 00:00:00 2001 From: Antonio de la Vega Date: Thu, 27 Jan 2022 18:38:55 +0100 Subject: [PATCH 6/6] doc(postgres): Add GIN index documentation --- docs/en/migrations.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/docs/en/migrations.rst b/docs/en/migrations.rst index 4dc814063..37e3e6b53 100644 --- a/docs/en/migrations.rst +++ b/docs/en/migrations.rst @@ -1388,6 +1388,24 @@ The SQL Server and PostgreSQL adapters also supports ``include`` (non-key) colum } } +In addition PostgreSQL adapters also supports Generalized Inverted Index ``gin`` indexes. + +.. code-block:: php + + table('users'); + $table->addColumn('address', 'string') + ->addIndex('address', ['type' => 'gin']) + ->create(); + } + } Removing indexes is as easy as calling the ``removeIndex()`` method. You must call this method for each index.