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. diff --git a/src/Phinx/Db/Adapter/PostgresAdapter.php b/src/Phinx/Db/Adapter/PostgresAdapter.php index b9b637d94..f510ba97d 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, ]; + private const GIN_INDEX_TYPE = 'gin'; + /** * 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 ($index->getType() === self::GIN_INDEX_TYPE) { + $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..47eded3cb 100644 --- a/tests/Phinx/Db/Adapter/PostgresAdapterTest.php +++ b/tests/Phinx/Db/Adapter/PostgresAdapterTest.php @@ -352,6 +352,16 @@ 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') + ->addIndex('names', ['type' => 'gin']) + ->save(); + + $this->assertTrue($this->adapter->hasIndex('table1', ['names'])); + } + public function testCreateTableWithNamedIndexes() { $table = new \Phinx\Db\Table('table1', [], $this->adapter);