diff --git a/src/BaseMigration.php b/src/BaseMigration.php index c4171df7..23c4831a 100644 --- a/src/BaseMigration.php +++ b/src/BaseMigration.php @@ -18,6 +18,7 @@ use Migrations\Db\Adapter\AdapterInterface; use Migrations\Db\Table; use Migrations\Db\Table\ForeignKey; +use Migrations\Db\Table\Index; use RuntimeException; /** @@ -436,6 +437,17 @@ public function foreignKey(string|array $columns): ForeignKey return (new ForeignKey())->setColumns($columns); } + /** + * Create a new Index object. + * + * @params string|string[] $columns Columns + * @return \Migrations\Db\Table\Index + */ + public function index(string|array $columns): Index + { + return (new Index())->setColumns($columns); + } + /** * Perform checks on the migration, printing a warning * if there are potential problems. diff --git a/src/Db/Adapter/PostgresAdapter.php b/src/Db/Adapter/PostgresAdapter.php index 65408635..d59f957e 100644 --- a/src/Db/Adapter/PostgresAdapter.php +++ b/src/Db/Adapter/PostgresAdapter.php @@ -1288,6 +1288,7 @@ protected function getIndexSqlDefinition(Index $index, string $tableName): strin $include = $index->getInclude(); $includedColumns = $include ? sprintf('INCLUDE ("%s")', implode('","', $include)) : ''; + // TODO always concurrently $createIndexSentence = 'CREATE %s INDEX %s ON %s '; if ($index->getType() === self::GIN_INDEX_TYPE) { $createIndexSentence .= ' USING ' . $index->getType() . '(%s) %s;'; diff --git a/src/Db/Table.php b/src/Db/Table.php index eb46b5a0..150af177 100644 --- a/src/Db/Table.php +++ b/src/Db/Table.php @@ -32,6 +32,7 @@ use Migrations\Db\Table\Index; use Migrations\Db\Table\Table as TableValue; use RuntimeException; +use function Cake\Core\deprecationWarning; /** * This object is based loosely on: https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html. @@ -513,6 +514,8 @@ public function addForeignKey(string|array|ForeignKey $columns, string|TableValu * @param string|string[] $referencedColumns Referenced Columns * @param array $options Options * @return $this + * @deprecated 4.6.0 Use addForeignKey() instead. Use `BaseMigration::foreignKey()` to get + * a fluent interface for building foreign keys. */ public function addForeignKeyWithName( string|ForeignKey $name, @@ -521,6 +524,11 @@ public function addForeignKeyWithName( string|array $referencedColumns = ['id'], array $options = [] ) { + deprecationWarning( + '4.6.0', + 'Use addForeignKey() instead. Use `BaseMigration::foreignKey()` to get a fluent' . + ' interface for building foreign keys.' + ); if (is_string($name)) { if ($columns === null || $referencedTable === null) { throw new InvalidArgumentException( diff --git a/src/Db/Table/ForeignKey.php b/src/Db/Table/ForeignKey.php index db5072e0..0c3042c3 100644 --- a/src/Db/Table/ForeignKey.php +++ b/src/Db/Table/ForeignKey.php @@ -17,7 +17,7 @@ * * Used to define foreign keys that are added to tables as part of migrations. * - * @see \Migrations\Db\Table::foreignKey() + * @see \Migrations\BaseMigration::foreignKey() * @see \Migrations\Db\Table::addForeignKey() */ class ForeignKey diff --git a/src/Db/Table/Index.php b/src/Db/Table/Index.php index 2fb274ae..d7032896 100644 --- a/src/Db/Table/Index.php +++ b/src/Db/Table/Index.php @@ -10,6 +10,19 @@ use RuntimeException; +/** + * Index value object + * + * Used to define indexes that are added to tables as part of migrations. + * + * @see \Migrations\BaseMigration::index() + * @see \Migrations\Db\Table::addIndex() + * + * TODO expand functionality of Index: + * - Add ifnotexists + * - Add nullsfirst/nullslast + * - Add where for partial indexes + */ class Index { /** @@ -129,6 +142,9 @@ public function getName(): ?string /** * Sets the index limit. * + * In MySQL indexes can have limit clauses to control the number of + * characters indexed in text and char columns. + * * @param int|array $limit limit value or array of limit value * @return $this */ @@ -173,7 +189,12 @@ public function getOrder(): ?array } /** - * Sets the index included columns. + * Sets the index included columns for a 'covering index'. + * + * In postgres and sqlserver, indexes can define additional non-key + * columns to build 'covering indexes'. This feature allows you to + * further optimize well-crafted queries that leverage specific + * indexes by reading all data from the index. * * @param string[] $includedColumns Columns * @return $this diff --git a/templates/bake/config/diff.twig b/templates/bake/config/diff.twig index 2e373c5c..377f06e1 100644 --- a/templates/bake/config/diff.twig +++ b/templates/bake/config/diff.twig @@ -99,7 +99,7 @@ not empty %} {{ Migration.element('Migrations.add-columns', {'columns': tableDiff['columns']['add']}) | raw -}} {% endif -%} {% if tableDiff['indexes']['add'] is not empty %} -{{ Migration.element('Migrations.add-indexes', {'indexes': tableDiff['indexes']['add']}) | raw -}} +{{ Migration.element('Migrations.add-indexes', {'backend': backend, 'indexes': tableDiff['indexes']['add']}) | raw -}} {% endif -%} {% if Migration.wasTableStatementGeneratedFor(tableName) %} ->update(); diff --git a/templates/bake/element/add-indexes.twig b/templates/bake/element/add-indexes.twig index 18d7c3a9..42900b25 100644 --- a/templates/bake/element/add-indexes.twig +++ b/templates/bake/element/add-indexes.twig @@ -1,10 +1,28 @@ {% for indexName, index in indexes %} +{% set columnsList = '\'' ~ index['columns'][0] ~ '\'' %} +{% if index['columns']|length > 1 %} +{% set columnsList = '[' ~ Migration.stringifyList(index['columns'], {'indent': 6}) ~ ']' %} +{% endif %} ->addIndex( +{% if backend == 'builtin' %} + $this->index({{ columnsList | raw }}) + ->setName('{{ indexName }}') +{% if index['type'] == 'unique' %} + ->setType('unique') +{% elseif index['type'] == 'fulltext' %} + ->setType('fulltext') +{% endif %} +{% if index['options'] %} + ->setOptions([{{ Migration.stringifyList(index['options'], {'indent': 6}) | raw }}]) +{% endif %} + ) +{% else %} [{{ Migration.stringifyList(index['columns'], {'indent': 5}) | raw }}], -{% set params = {'name': indexName} %} -{% if index['type'] == 'unique' %} -{% set params = params|merge({'unique': true}) %} -{% endif %} +{% set params = {'name': indexName} %} +{% if index['type'] == 'unique' %} +{% set params = params|merge({'unique': true}) %} +{% endif %} [{{ Migration.stringifyList(params, {'indent': 5}) | raw }}] ) +{% endif %} {% endfor %} diff --git a/templates/bake/element/create-tables.twig b/templates/bake/element/create-tables.twig index d57559e6..fd7e6d73 100644 --- a/templates/bake/element/create-tables.twig +++ b/templates/bake/element/create-tables.twig @@ -39,26 +39,17 @@ {% if createData.tables[table].constraints is not empty %} {% for name, constraint in createData.tables[table].constraints %} {% if constraint['type'] == 'unique' %} - ->addIndex( - [{{ Migration.stringifyList(constraint['columns'], {'indent': 5}) | raw }}], -{% set params = {'name': name, 'unique': true} %} - [{{ Migration.stringifyList(params, {'indent': 5}) | raw }}] - ) +{{ element('Migrations.add-indexes', { + indexes: {(name): constraint}, + backend: backend, + }) -}} {% endif %} {% endfor %} {% endif %} -{% for indexName, index in createData.tables[table].indexes %} -{% set indexColumns = index['columns'] | sort %} - ->addIndex( - [{{ Migration.stringifyList(index['columns'], {'indent': 5}) | raw }}], -{% set params = {'name': indexName} %} -{% if index['type'] == 'fulltext' %} -{% set params = params|merge({'type': 'fulltext'}) %} -{% endif %} - [{{ Migration.stringifyList(params, {'indent': 5}) | raw }}] - ) -{% endfor %} - ->create(); +{{- element('Migrations.add-indexes', { + indexes: createData.tables[table].indexes, + backend: backend, +}) }} ->create(); {% endfor -%} {% if createData.constraints %} {% for table, tableConstraints in createData.constraints %} diff --git a/tests/comparisons/Diff/default/the_diff_default_mysql.php b/tests/comparisons/Diff/default/the_diff_default_mysql.php index f0ad425d..0cdf0976 100644 --- a/tests/comparisons/Diff/default/the_diff_default_mysql.php +++ b/tests/comparisons/Diff/default/the_diff_default_mysql.php @@ -78,20 +78,12 @@ public function up(): void 'signed' => true, ]) ->addIndex( - [ - 'user_id', - ], - [ - 'name' => 'categories_ibfk_1', - ] + $this->index('user_id') + ->setName('categories_ibfk_1') ) ->addIndex( - [ - 'name', - ], - [ - 'name' => 'name', - ] + $this->index('name') + ->setName('name') ) ->create(); @@ -123,28 +115,16 @@ public function up(): void 'signed' => true, ]) ->addIndex( - [ - 'slug', - ], - [ - 'name' => 'UNIQUE_SLUG', - ] + $this->index('slug') + ->setName('UNIQUE_SLUG') ) ->addIndex( - [ - 'category_id', - ], - [ - 'name' => 'category_id', - ] + $this->index('category_id') + ->setName('category_id') ) ->addIndex( - [ - 'name', - ], - [ - 'name' => 'rating_index', - ] + $this->index('name') + ->setName('rating_index') ) ->update(); @@ -233,29 +213,17 @@ public function down(): void ->removeColumn('category_id') ->removeColumn('average_note') ->addIndex( - [ - 'slug', - ], - [ - 'name' => 'UNIQUE_SLUG', - 'unique' => true, - ] + $this->index('slug') + ->setName('UNIQUE_SLUG') + ->setType('unique') ) ->addIndex( - [ - 'rating', - ], - [ - 'name' => 'rating_index', - ] + $this->index('rating') + ->setName('rating_index') ) ->addIndex( - [ - 'name', - ], - [ - 'name' => 'BY_NAME', - ] + $this->index('name') + ->setName('BY_NAME') ) ->update(); diff --git a/tests/comparisons/Diff/simple/the_diff_simple_mysql.php b/tests/comparisons/Diff/simple/the_diff_simple_mysql.php index d7b5917c..9e9c50a5 100644 --- a/tests/comparisons/Diff/simple/the_diff_simple_mysql.php +++ b/tests/comparisons/Diff/simple/the_diff_simple_mysql.php @@ -52,12 +52,8 @@ public function up(): void 'signed' => false, ]) ->addIndex( - [ - 'user_id', - ], - [ - 'name' => 'user_id', - ] + $this->index('user_id') + ->setName('user_id') ) ->update(); diff --git a/tests/comparisons/Migration/pgsql/test_snapshot_auto_id_disabled_pgsql.php b/tests/comparisons/Migration/pgsql/test_snapshot_auto_id_disabled_pgsql.php index 683115f6..9a27dcaf 100644 --- a/tests/comparisons/Migration/pgsql/test_snapshot_auto_id_disabled_pgsql.php +++ b/tests/comparisons/Migration/pgsql/test_snapshot_auto_id_disabled_pgsql.php @@ -70,12 +70,8 @@ public function up(): void 'scale' => 6, ]) ->addIndex( - [ - 'title', - ], - [ - 'name' => 'articles_title_idx', - ] + $this->index('title') + ->setName('articles_title_idx') ) ->create(); @@ -117,13 +113,9 @@ public function up(): void 'scale' => 6, ]) ->addIndex( - [ - 'slug', - ], - [ - 'name' => 'categories_slug_unique', - 'unique' => true, - ] + $this->index('slug') + ->setName('categories_slug_unique') + ->setType('unique') ) ->create(); @@ -185,13 +177,11 @@ public function up(): void 'null' => false, ]) ->addIndex( - [ - 'product_category', - 'product_id', - ], - [ - 'name' => 'orders_product_category_idx', - ] + $this->index([ + 'product_category', + 'product_id', + ]) + ->setName('orders_product_category_idx') ) ->create(); @@ -253,31 +243,21 @@ public function up(): void 'scale' => 6, ]) ->addIndex( - [ - 'id', - 'category_id', - ], - [ - 'name' => 'products_category_unique', - 'unique' => true, - ] + $this->index([ + 'id', + 'category_id', + ]) + ->setName('products_category_unique') + ->setType('unique') ) ->addIndex( - [ - 'slug', - ], - [ - 'name' => 'products_slug_unique', - 'unique' => true, - ] + $this->index('slug') + ->setName('products_slug_unique') + ->setType('unique') ) ->addIndex( - [ - 'title', - ], - [ - 'name' => 'products_title_idx', - ] + $this->index('title') + ->setName('products_title_idx') ) ->create(); @@ -331,13 +311,9 @@ public function up(): void 'scale' => 6, ]) ->addIndex( - [ - 'article_id', - ], - [ - 'name' => 'special_tags_article_unique', - 'unique' => true, - ] + $this->index('article_id') + ->setName('special_tags_article_unique') + ->setType('unique') ) ->create(); diff --git a/tests/comparisons/Migration/pgsql/test_snapshot_not_empty_pgsql.php b/tests/comparisons/Migration/pgsql/test_snapshot_not_empty_pgsql.php index 6a3ec27d..4264411c 100644 --- a/tests/comparisons/Migration/pgsql/test_snapshot_not_empty_pgsql.php +++ b/tests/comparisons/Migration/pgsql/test_snapshot_not_empty_pgsql.php @@ -61,12 +61,8 @@ public function up(): void 'scale' => 6, ]) ->addIndex( - [ - 'title', - ], - [ - 'name' => 'articles_title_idx', - ] + $this->index('title') + ->setName('articles_title_idx') ) ->create(); @@ -101,13 +97,9 @@ public function up(): void 'scale' => 6, ]) ->addIndex( - [ - 'slug', - ], - [ - 'name' => 'categories_slug_unique', - 'unique' => true, - ] + $this->index('slug') + ->setName('categories_slug_unique') + ->setType('unique') ) ->create(); @@ -154,13 +146,11 @@ public function up(): void 'null' => false, ]) ->addIndex( - [ - 'product_category', - 'product_id', - ], - [ - 'name' => 'orders_product_category_idx', - ] + $this->index([ + 'product_category', + 'product_id', + ]) + ->setName('orders_product_category_idx') ) ->create(); @@ -208,31 +198,21 @@ public function up(): void 'scale' => 6, ]) ->addIndex( - [ - 'id', - 'category_id', - ], - [ - 'name' => 'products_category_unique', - 'unique' => true, - ] + $this->index([ + 'id', + 'category_id', + ]) + ->setName('products_category_unique') + ->setType('unique') ) ->addIndex( - [ - 'slug', - ], - [ - 'name' => 'products_slug_unique', - 'unique' => true, - ] + $this->index('slug') + ->setName('products_slug_unique') + ->setType('unique') ) ->addIndex( - [ - 'title', - ], - [ - 'name' => 'products_title_idx', - ] + $this->index('title') + ->setName('products_title_idx') ) ->create(); @@ -278,13 +258,9 @@ public function up(): void 'scale' => 6, ]) ->addIndex( - [ - 'article_id', - ], - [ - 'name' => 'special_tags_article_unique', - 'unique' => true, - ] + $this->index('article_id') + ->setName('special_tags_article_unique') + ->setType('unique') ) ->create(); diff --git a/tests/comparisons/Migration/pgsql/test_snapshot_plugin_blog_pgsql.php b/tests/comparisons/Migration/pgsql/test_snapshot_plugin_blog_pgsql.php index fb1496ca..e2fe20f9 100644 --- a/tests/comparisons/Migration/pgsql/test_snapshot_plugin_blog_pgsql.php +++ b/tests/comparisons/Migration/pgsql/test_snapshot_plugin_blog_pgsql.php @@ -61,12 +61,8 @@ public function up(): void 'scale' => 6, ]) ->addIndex( - [ - 'title', - ], - [ - 'name' => 'articles_title_idx', - ] + $this->index('title') + ->setName('articles_title_idx') ) ->create(); @@ -101,13 +97,9 @@ public function up(): void 'scale' => 6, ]) ->addIndex( - [ - 'slug', - ], - [ - 'name' => 'categories_slug_unique', - 'unique' => true, - ] + $this->index('slug') + ->setName('categories_slug_unique') + ->setType('unique') ) ->create(); diff --git a/tests/comparisons/Migration/sqlite/test_snapshot_auto_id_disabled_sqlite.php b/tests/comparisons/Migration/sqlite/test_snapshot_auto_id_disabled_sqlite.php index ebf782cc..34f372f0 100644 --- a/tests/comparisons/Migration/sqlite/test_snapshot_auto_id_disabled_sqlite.php +++ b/tests/comparisons/Migration/sqlite/test_snapshot_auto_id_disabled_sqlite.php @@ -65,12 +65,8 @@ public function up(): void 'null' => true, ]) ->addIndex( - [ - 'title', - ], - [ - 'name' => 'articles_title_idx', - ] + $this->index('title') + ->setName('articles_title_idx') ) ->create(); @@ -108,13 +104,9 @@ public function up(): void 'null' => true, ]) ->addIndex( - [ - 'slug', - ], - [ - 'name' => 'categories_slug_unique', - 'unique' => true, - ] + $this->index('slug') + ->setName('categories_slug_unique') + ->setType('unique') ) ->create(); @@ -176,13 +168,11 @@ public function up(): void 'null' => false, ]) ->addIndex( - [ - 'product_category', - 'product_id', - ], - [ - 'name' => 'orders_product_category_idx', - ] + $this->index([ + 'product_category', + 'product_id', + ]) + ->setName('orders_product_category_idx') ) ->create(); @@ -240,31 +230,21 @@ public function up(): void 'null' => true, ]) ->addIndex( - [ - 'category_id', - 'id', - ], - [ - 'name' => 'products_category_unique', - 'unique' => true, - ] + $this->index([ + 'category_id', + 'id', + ]) + ->setName('products_category_unique') + ->setType('unique') ) ->addIndex( - [ - 'slug', - ], - [ - 'name' => 'products_slug_unique', - 'unique' => true, - ] + $this->index('slug') + ->setName('products_slug_unique') + ->setType('unique') ) ->addIndex( - [ - 'title', - ], - [ - 'name' => 'products_title_idx', - ] + $this->index('title') + ->setName('products_title_idx') ) ->create(); @@ -316,13 +296,9 @@ public function up(): void 'null' => true, ]) ->addIndex( - [ - 'article_id', - ], - [ - 'name' => 'special_tags_article_unique', - 'unique' => true, - ] + $this->index('article_id') + ->setName('special_tags_article_unique') + ->setType('unique') ) ->create(); diff --git a/tests/comparisons/Migration/sqlite/test_snapshot_not_empty_sqlite.php b/tests/comparisons/Migration/sqlite/test_snapshot_not_empty_sqlite.php index b1066505..5905106c 100644 --- a/tests/comparisons/Migration/sqlite/test_snapshot_not_empty_sqlite.php +++ b/tests/comparisons/Migration/sqlite/test_snapshot_not_empty_sqlite.php @@ -56,12 +56,8 @@ public function up(): void 'null' => true, ]) ->addIndex( - [ - 'title', - ], - [ - 'name' => 'articles_title_idx', - ] + $this->index('title') + ->setName('articles_title_idx') ) ->create(); @@ -92,13 +88,9 @@ public function up(): void 'null' => true, ]) ->addIndex( - [ - 'slug', - ], - [ - 'name' => 'categories_slug_unique', - 'unique' => true, - ] + $this->index('slug') + ->setName('categories_slug_unique') + ->setType('unique') ) ->create(); @@ -145,13 +137,11 @@ public function up(): void 'null' => false, ]) ->addIndex( - [ - 'product_category', - 'product_id', - ], - [ - 'name' => 'orders_product_category_idx', - ] + $this->index([ + 'product_category', + 'product_id', + ]) + ->setName('orders_product_category_idx') ) ->create(); @@ -195,31 +185,21 @@ public function up(): void 'null' => true, ]) ->addIndex( - [ - 'category_id', - 'id', - ], - [ - 'name' => 'products_category_unique', - 'unique' => true, - ] + $this->index([ + 'category_id', + 'id', + ]) + ->setName('products_category_unique') + ->setType('unique') ) ->addIndex( - [ - 'slug', - ], - [ - 'name' => 'products_slug_unique', - 'unique' => true, - ] + $this->index('slug') + ->setName('products_slug_unique') + ->setType('unique') ) ->addIndex( - [ - 'title', - ], - [ - 'name' => 'products_title_idx', - ] + $this->index('title') + ->setName('products_title_idx') ) ->create(); @@ -263,13 +243,9 @@ public function up(): void 'null' => true, ]) ->addIndex( - [ - 'article_id', - ], - [ - 'name' => 'special_tags_article_unique', - 'unique' => true, - ] + $this->index('article_id') + ->setName('special_tags_article_unique') + ->setType('unique') ) ->create(); diff --git a/tests/comparisons/Migration/sqlite/test_snapshot_plugin_blog_sqlite.php b/tests/comparisons/Migration/sqlite/test_snapshot_plugin_blog_sqlite.php index 1873c4a1..93f996b9 100644 --- a/tests/comparisons/Migration/sqlite/test_snapshot_plugin_blog_sqlite.php +++ b/tests/comparisons/Migration/sqlite/test_snapshot_plugin_blog_sqlite.php @@ -56,12 +56,8 @@ public function up(): void 'null' => true, ]) ->addIndex( - [ - 'title', - ], - [ - 'name' => 'articles_title_idx', - ] + $this->index('title') + ->setName('articles_title_idx') ) ->create(); @@ -92,13 +88,9 @@ public function up(): void 'null' => true, ]) ->addIndex( - [ - 'slug', - ], - [ - 'name' => 'categories_slug_unique', - 'unique' => true, - ] + $this->index('slug') + ->setName('categories_slug_unique') + ->setType('unique') ) ->create(); diff --git a/tests/comparisons/Migration/sqlserver/test_snapshot_auto_id_disabled_sqlserver.php b/tests/comparisons/Migration/sqlserver/test_snapshot_auto_id_disabled_sqlserver.php index 512acd94..89b55688 100644 --- a/tests/comparisons/Migration/sqlserver/test_snapshot_auto_id_disabled_sqlserver.php +++ b/tests/comparisons/Migration/sqlserver/test_snapshot_auto_id_disabled_sqlserver.php @@ -71,12 +71,8 @@ public function up(): void 'scale' => 7, ]) ->addIndex( - [ - 'title', - ], - [ - 'name' => 'articles_title_idx', - ] + $this->index('title') + ->setName('articles_title_idx') ) ->create(); @@ -120,13 +116,9 @@ public function up(): void 'scale' => 7, ]) ->addIndex( - [ - 'slug', - ], - [ - 'name' => 'categories_slug_unique', - 'unique' => true, - ] + $this->index('slug') + ->setName('categories_slug_unique') + ->setType('unique') ) ->create(); @@ -192,13 +184,11 @@ public function up(): void 'null' => false, ]) ->addIndex( - [ - 'product_category', - 'product_id', - ], - [ - 'name' => 'orders_product_category_idx', - ] + $this->index([ + 'product_category', + 'product_id', + ]) + ->setName('orders_product_category_idx') ) ->create(); @@ -263,31 +253,21 @@ public function up(): void 'scale' => 7, ]) ->addIndex( - [ - 'category_id', - 'id', - ], - [ - 'name' => 'products_category_unique', - 'unique' => true, - ] + $this->index([ + 'category_id', + 'id', + ]) + ->setName('products_category_unique') + ->setType('unique') ) ->addIndex( - [ - 'slug', - ], - [ - 'name' => 'products_slug_unique', - 'unique' => true, - ] + $this->index('slug') + ->setName('products_slug_unique') + ->setType('unique') ) ->addIndex( - [ - 'title', - ], - [ - 'name' => 'products_title_idx', - ] + $this->index('title') + ->setName('products_title_idx') ) ->create(); @@ -342,13 +322,9 @@ public function up(): void 'scale' => 7, ]) ->addIndex( - [ - 'article_id', - ], - [ - 'name' => 'special_tags_article_unique', - 'unique' => true, - ] + $this->index('article_id') + ->setName('special_tags_article_unique') + ->setType('unique') ) ->create(); diff --git a/tests/comparisons/Migration/sqlserver/test_snapshot_not_empty_sqlserver.php b/tests/comparisons/Migration/sqlserver/test_snapshot_not_empty_sqlserver.php index 182811d9..0b0823ef 100644 --- a/tests/comparisons/Migration/sqlserver/test_snapshot_not_empty_sqlserver.php +++ b/tests/comparisons/Migration/sqlserver/test_snapshot_not_empty_sqlserver.php @@ -62,12 +62,8 @@ public function up(): void 'scale' => 7, ]) ->addIndex( - [ - 'title', - ], - [ - 'name' => 'articles_title_idx', - ] + $this->index('title') + ->setName('articles_title_idx') ) ->create(); @@ -104,13 +100,9 @@ public function up(): void 'scale' => 7, ]) ->addIndex( - [ - 'slug', - ], - [ - 'name' => 'categories_slug_unique', - 'unique' => true, - ] + $this->index('slug') + ->setName('categories_slug_unique') + ->setType('unique') ) ->create(); @@ -161,13 +153,11 @@ public function up(): void 'null' => false, ]) ->addIndex( - [ - 'product_category', - 'product_id', - ], - [ - 'name' => 'orders_product_category_idx', - ] + $this->index([ + 'product_category', + 'product_id', + ]) + ->setName('orders_product_category_idx') ) ->create(); @@ -218,31 +208,21 @@ public function up(): void 'scale' => 7, ]) ->addIndex( - [ - 'category_id', - 'id', - ], - [ - 'name' => 'products_category_unique', - 'unique' => true, - ] + $this->index([ + 'category_id', + 'id', + ]) + ->setName('products_category_unique') + ->setType('unique') ) ->addIndex( - [ - 'slug', - ], - [ - 'name' => 'products_slug_unique', - 'unique' => true, - ] + $this->index('slug') + ->setName('products_slug_unique') + ->setType('unique') ) ->addIndex( - [ - 'title', - ], - [ - 'name' => 'products_title_idx', - ] + $this->index('title') + ->setName('products_title_idx') ) ->create(); @@ -289,13 +269,9 @@ public function up(): void 'scale' => 7, ]) ->addIndex( - [ - 'article_id', - ], - [ - 'name' => 'special_tags_article_unique', - 'unique' => true, - ] + $this->index('article_id') + ->setName('special_tags_article_unique') + ->setType('unique') ) ->create(); diff --git a/tests/comparisons/Migration/sqlserver/test_snapshot_plugin_blog_sqlserver.php b/tests/comparisons/Migration/sqlserver/test_snapshot_plugin_blog_sqlserver.php index 366b46ec..fbbadeb9 100644 --- a/tests/comparisons/Migration/sqlserver/test_snapshot_plugin_blog_sqlserver.php +++ b/tests/comparisons/Migration/sqlserver/test_snapshot_plugin_blog_sqlserver.php @@ -62,12 +62,8 @@ public function up(): void 'scale' => 7, ]) ->addIndex( - [ - 'title', - ], - [ - 'name' => 'articles_title_idx', - ] + $this->index('title') + ->setName('articles_title_idx') ) ->create(); @@ -104,13 +100,9 @@ public function up(): void 'scale' => 7, ]) ->addIndex( - [ - 'slug', - ], - [ - 'name' => 'categories_slug_unique', - 'unique' => true, - ] + $this->index('slug') + ->setName('categories_slug_unique') + ->setType('unique') ) ->create(); diff --git a/tests/comparisons/Migration/test_snapshot_auto_id_disabled.php b/tests/comparisons/Migration/test_snapshot_auto_id_disabled.php index ce4158aa..6f472c27 100644 --- a/tests/comparisons/Migration/test_snapshot_auto_id_disabled.php +++ b/tests/comparisons/Migration/test_snapshot_auto_id_disabled.php @@ -70,20 +70,12 @@ public function up(): void 'null' => true, ]) ->addIndex( - [ - 'category_id', - ], - [ - 'name' => 'articles_category_fk', - ] + $this->index('category_id') + ->setName('articles_category_fk') ) ->addIndex( - [ - 'title', - ], - [ - 'name' => 'articles_title_idx', - ] + $this->index('title') + ->setName('articles_title_idx') ) ->create(); @@ -123,13 +115,9 @@ public function up(): void 'null' => true, ]) ->addIndex( - [ - 'slug', - ], - [ - 'name' => 'categories_slug_unique', - 'unique' => true, - ] + $this->index('slug') + ->setName('categories_slug_unique') + ->setType('unique') ) ->create(); @@ -195,13 +183,11 @@ public function up(): void 'signed' => false, ]) ->addIndex( - [ - 'product_category', - 'product_id', - ], - [ - 'name' => 'orders_product_category_idx', - ] + $this->index([ + 'product_category', + 'product_id', + ]) + ->setName('orders_product_category_idx') ) ->create(); @@ -263,32 +249,22 @@ public function up(): void 'null' => true, ]) ->addIndex( - [ - 'slug', - ], - [ - 'name' => 'products_slug_unique', - 'unique' => true, - ] + $this->index('slug') + ->setName('products_slug_unique') + ->setType('unique') ) ->addIndex( - [ - 'category_id', - 'id', - ], - [ - 'name' => 'products_category_unique', - 'unique' => true, - ] + $this->index([ + 'category_id', + 'id', + ]) + ->setName('products_category_unique') + ->setType('unique') ) ->addIndex( - [ - 'title', - ], - [ - 'name' => 'products_title_idx', - 'type' => 'fulltext', - ] + $this->index('title') + ->setName('products_title_idx') + ->setType('fulltext') ) ->create(); @@ -344,13 +320,9 @@ public function up(): void 'null' => true, ]) ->addIndex( - [ - 'article_id', - ], - [ - 'name' => 'special_tags_article_unique', - 'unique' => true, - ] + $this->index('article_id') + ->setName('special_tags_article_unique') + ->setType('unique') ) ->create(); diff --git a/tests/comparisons/Migration/test_snapshot_not_empty.php b/tests/comparisons/Migration/test_snapshot_not_empty.php index 8435b8a8..9ddbe426 100644 --- a/tests/comparisons/Migration/test_snapshot_not_empty.php +++ b/tests/comparisons/Migration/test_snapshot_not_empty.php @@ -60,20 +60,12 @@ public function up(): void 'null' => true, ]) ->addIndex( - [ - 'category_id', - ], - [ - 'name' => 'articles_category_fk', - ] + $this->index('category_id') + ->setName('articles_category_fk') ) ->addIndex( - [ - 'title', - ], - [ - 'name' => 'articles_title_idx', - ] + $this->index('title') + ->setName('articles_title_idx') ) ->create(); @@ -105,13 +97,9 @@ public function up(): void 'null' => true, ]) ->addIndex( - [ - 'slug', - ], - [ - 'name' => 'categories_slug_unique', - 'unique' => true, - ] + $this->index('slug') + ->setName('categories_slug_unique') + ->setType('unique') ) ->create(); @@ -160,13 +148,11 @@ public function up(): void 'signed' => false, ]) ->addIndex( - [ - 'product_category', - 'product_id', - ], - [ - 'name' => 'orders_product_category_idx', - ] + $this->index([ + 'product_category', + 'product_id', + ]) + ->setName('orders_product_category_idx') ) ->create(); @@ -212,32 +198,22 @@ public function up(): void 'null' => true, ]) ->addIndex( - [ - 'slug', - ], - [ - 'name' => 'products_slug_unique', - 'unique' => true, - ] + $this->index('slug') + ->setName('products_slug_unique') + ->setType('unique') ) ->addIndex( - [ - 'category_id', - 'id', - ], - [ - 'name' => 'products_category_unique', - 'unique' => true, - ] + $this->index([ + 'category_id', + 'id', + ]) + ->setName('products_category_unique') + ->setType('unique') ) ->addIndex( - [ - 'title', - ], - [ - 'name' => 'products_title_idx', - 'type' => 'fulltext', - ] + $this->index('title') + ->setName('products_title_idx') + ->setType('fulltext') ) ->create(); @@ -284,13 +260,9 @@ public function up(): void 'null' => true, ]) ->addIndex( - [ - 'article_id', - ], - [ - 'name' => 'special_tags_article_unique', - 'unique' => true, - ] + $this->index('article_id') + ->setName('special_tags_article_unique') + ->setType('unique') ) ->create(); diff --git a/tests/comparisons/Migration/test_snapshot_plugin_blog.php b/tests/comparisons/Migration/test_snapshot_plugin_blog.php index 09d65a36..db313050 100644 --- a/tests/comparisons/Migration/test_snapshot_plugin_blog.php +++ b/tests/comparisons/Migration/test_snapshot_plugin_blog.php @@ -60,20 +60,12 @@ public function up(): void 'null' => true, ]) ->addIndex( - [ - 'category_id', - ], - [ - 'name' => 'articles_category_fk', - ] + $this->index('category_id') + ->setName('articles_category_fk') ) ->addIndex( - [ - 'title', - ], - [ - 'name' => 'articles_title_idx', - ] + $this->index('title') + ->setName('articles_title_idx') ) ->create(); @@ -105,13 +97,9 @@ public function up(): void 'null' => true, ]) ->addIndex( - [ - 'slug', - ], - [ - 'name' => 'categories_slug_unique', - 'unique' => true, - ] + $this->index('slug') + ->setName('categories_slug_unique') + ->setType('unique') ) ->create(); diff --git a/tests/test_app/config/BaseMigrations/20230628181900_base_migration_tables.php b/tests/test_app/config/BaseMigrations/20230628181900_base_migration_tables.php index c57cc0ff..87910772 100644 --- a/tests/test_app/config/BaseMigrations/20230628181900_base_migration_tables.php +++ b/tests/test_app/config/BaseMigrations/20230628181900_base_migration_tables.php @@ -11,7 +11,12 @@ public function change(): void ->addColumn('name', 'string') ->addTimestamps() ->addPrimaryKey('id') + ->addIndex( + $this->index('name') + ->setName('base_stores_name_idx') + ) ->create(); + $io = $this->getIo(); $res = $this->query('SELECT 121 as val');