Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for partial indexes and concurrent indexes in postgres #793

Merged
merged 6 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
250 changes: 155 additions & 95 deletions docs/en/writing-migrations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1249,44 +1249,62 @@ table object.
}
}

By default Migrations instructs the database adapter to create a normal index. We
By default Migrations instructs the database adapter to create a simple index. We
can pass an additional parameter ``unique`` to the ``addIndex()`` method to
specify a unique index. We can also explicitly specify a name for the index
using the ``name`` parameter, the index columns sort order can also be specified using
the ``order`` parameter. The order parameter takes an array of column names and sort order key/value pairs.
the ``order`` parameter. The order parameter takes an array of column names and sort order key/value pairs::

.. code-block:: php

<?php
<?php

use Migrations\BaseMigration;
use Migrations\BaseMigration;

class MyNewMigration extends BaseMigration
class MyNewMigration extends BaseMigration
{
/**
* Migrate Up.
*/
public function up()
{
/**
* Migrate Up.
*/
public function up()
{
$table = $this->table('users');
$table->addColumn('email', 'string')
->addColumn('username','string')
->addIndex(['email', 'username'], [
'unique' => true,
'name' => 'idx_users_email',
'order' => ['email' => 'DESC', 'username' => 'ASC']]
)
->save();
}
$table = $this->table('users');
$table->addColumn('email', 'string')
->addColumn('username','string')
->addIndex(['email', 'username'], [
'unique' => true,
'name' => 'idx_users_email',
'order' => ['email' => 'DESC', 'username' => 'ASC']]
)
->save();
}
}

/**
* Migrate Down.
*/
public function down()
{
As of 4.6.0, you can use ``BaseMigration::index()`` to get a fluent builder to
define indexes::

}
<?php

use Migrations\BaseMigration;

class MyNewMigration extends BaseMigration
{
/**
* Migrate Up.
*/
public function up()
{
$table = $this->table('users');
$table->addColumn('email', 'string')
->addColumn('username','string')
->addIndex(
$this->index(['email', 'username'])
->setType('unique')
->setName('idx_users_email')
->setOrder(['email' => 'DESC', 'username' => 'ASC'])
)
->save();
}
}


The MySQL adapter also supports ``fulltext`` indexes. If you are using a version before 5.6 you must
ensure the table uses the ``MyISAM`` engine.
Expand All @@ -1308,103 +1326,145 @@ ensure the table uses the ``MyISAM`` engine.
}
}

In addition, MySQL adapter also supports setting the index length defined by limit option.
MySQL adapter supports setting the index length defined by limit option.
When you are using a multi-column index, you are able to define each column index length.
The single column index can define its index length with or without defining column name in limit option.

.. code-block:: php
The single column index can define its index length with or without defining column name in limit option::

<?php
<?php

use Migrations\BaseMigration;
use Migrations\BaseMigration;

class MyNewMigration extends BaseMigration
class MyNewMigration extends BaseMigration
{
public function change()
{
public function change()
{
$table = $this->table('users');
$table->addColumn('email', 'string')
->addColumn('username','string')
->addColumn('user_guid', 'string', ['limit' => 36])
->addIndex(['email','username'], ['limit' => ['email' => 5, 'username' => 2]])
->addIndex('user_guid', ['limit' => 6])
->create();
}
$table = $this->table('users');
$table->addColumn('email', 'string')
->addColumn('username','string')
->addColumn('user_guid', 'string', ['limit' => 36])
->addIndex(['email','username'], ['limit' => ['email' => 5, 'username' => 2]])
->addIndex('user_guid', ['limit' => 6])
->create();
}
}

The SQL Server and PostgreSQL adapters also supports ``include`` (non-key) columns on indexes.
The SQL Server and PostgreSQL adapters support ``include`` (non-key) columns on indexes::

.. code-block:: php
<?php

<?php
use Migrations\BaseMigration;

use Migrations\BaseMigration;
class MyNewMigration extends BaseMigration
{
public function change()
{
$table = $this->table('users');
$table->addColumn('email', 'string')
->addColumn('firstname','string')
->addColumn('lastname','string')
->addIndex(['email'], ['include' => ['firstname', 'lastname']])
->create();
}
}

class MyNewMigration extends BaseMigration
PostgreSQL, SQLServer, and SQLite support partial indexes by defining where
clauses for the index::

<?php

use Migrations\BaseMigration;

class MyNewMigration extends BaseMigration
{
public function change()
{
public function change()
{
$table = $this->table('users');
$table->addColumn('email', 'string')
->addColumn('firstname','string')
->addColumn('lastname','string')
->addIndex(['email'], ['include' => ['firstname', 'lastname']])
->create();
}
$table = $this->table('users');
$table->addColumn('email', 'string')
->addColumn('is_verified','boolean')
->addIndex(
$this->index('email')
->setName('user_email_verified_idx')
->setType('unique')
->setWhere('is_verified = true')
)
->create();
}
}

In addition PostgreSQL adapters also supports Generalized Inverted Index ``gin`` indexes.
PostgreSQL can create indexes concurrently which avoids taking disruptive locks
during index creation::

.. code-block:: php
<?php

<?php
use Migrations\BaseMigration;

use Migrations\BaseMigration;
class MyNewMigration extends BaseMigration
{
public function change()
{
$table = $this->table('users');
$table->addColumn('email', 'string')
->addIndex(
$this->index('email')
->setName('user_email_unique_idx')
->setType('unique')
->setConcurrently(true)
)
->create();
}
}

class MyNewMigration extends BaseMigration
PostgreSQL adapters also supports Generalized Inverted Index ``gin`` indexes::

<?php

use Migrations\BaseMigration;

class MyNewMigration extends BaseMigration
{
public function change()
{
public function change()
{
$table = $this->table('users');
$table->addColumn('address', 'string')
->addIndex('address', ['type' => 'gin'])
->create();
}
$table = $this->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.

.. code-block:: php
call this method for each index::

<?php
<?php

use Migrations\BaseMigration;
use Migrations\BaseMigration;

class MyNewMigration extends BaseMigration
class MyNewMigration extends BaseMigration
{
/**
* Migrate Up.
*/
public function up()
{
/**
* Migrate Up.
*/
public function up()
{
$table = $this->table('users');
$table->removeIndex(['email'])
->save();
$table = $this->table('users');
$table->removeIndex(['email'])
->save();

// alternatively, you can delete an index by its name, ie:
$table->removeIndexByName('idx_users_email')
->save();
}
// alternatively, you can delete an index by its name, ie:
$table->removeIndexByName('idx_users_email')
->save();
}

/**
* Migrate Down.
*/
public function down()
{
/**
* Migrate Down.
*/
public function down()
{

}
}
}

.. versionadded:: 4.6.0
``Index::setWhere()``, and ``Index::setConcurrently()`` were added.


Working With Foreign Keys
Expand Down
7 changes: 7 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.26.1@d747f6500b38ac4f7dfc5edbcae6e4b637d7add0">
<file src="src/AbstractMigration.php">
<DeprecatedClass>
<code><![CDATA[Table]]></code>
<code><![CDATA[\Migrations\Table]]></code>
<code><![CDATA[new Table($tableName, $options, $this->getAdapter())]]></code>
</DeprecatedClass>
</file>
<file src="src/AbstractSeed.php">
<DeprecatedClass>
<code><![CDATA[new Seed()]]></code>
Expand Down
1 change: 0 additions & 1 deletion src/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ class MysqlAdapter extends PdoAdapter
*/
public function connect(): void
{

$this->getConnection()->getDriver()->connect();
$this->setConnection($this->getConnection());
}
Expand Down
17 changes: 11 additions & 6 deletions src/Db/Adapter/PostgresAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1288,23 +1288,28 @@ protected function getIndexSqlDefinition(Index $index, string $tableName): strin
}, $columnNames);

$include = $index->getInclude();
$includedColumns = $include ? sprintf('INCLUDE ("%s")', implode('","', $include)) : '';
$includedColumns = $include ? sprintf(' INCLUDE ("%s")', implode('","', $include)) : '';

// TODO always concurrently
$createIndexSentence = 'CREATE %s INDEX %s ON %s ';
$createIndexSentence = 'CREATE %sINDEX%s %s ON %s ';
if ($index->getType() === self::GIN_INDEX_TYPE) {
$createIndexSentence .= ' USING ' . $index->getType() . '(%s) %s;';
} else {
$createIndexSentence .= '(%s) %s;';
$createIndexSentence .= '(%s)%s%s;';
}
$where = (string)$index->getWhere();
if ($where) {
$where = ' WHERE ' . $where;
}

return sprintf(
$createIndexSentence,
($index->getType() === Index::UNIQUE ? 'UNIQUE' : ''),
$index->getType() === Index::UNIQUE ? 'UNIQUE ' : '',
$index->getConcurrently() ? ' CONCURRENTLY' : '',
$this->quoteColumnName((string)$indexName),
$this->quoteTableName($tableName),
implode(',', $columnNames),
$includedColumns
$includedColumns,
$where,
);
}

Expand Down
9 changes: 7 additions & 2 deletions src/Db/Adapter/SqliteAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1378,11 +1378,16 @@ protected function getAddIndexInstructions(Table $table, Index $index): AlterIns
$indexColumnArray[] = sprintf('`%s` ASC', $column);
}
$indexColumns = implode(',', $indexColumnArray);
$where = (string)$index->getWhere();
if ($where) {
$where = ' WHERE ' . $where;
}
$sql = sprintf(
'CREATE %s ON %s (%s)',
'CREATE %s ON %s (%s)%s',
$this->getIndexSqlDefinition($table, $index),
$this->quoteTableName($table->getName()),
$indexColumns
$indexColumns,
$where
);

return new AlterInstructions([], [$sql]);
Expand Down
Loading
Loading