Skip to content

Rolling back columns and indices added in separate migrations clashes with index recreation #2126

Closed
@ndm2

Description

@ndm2

When a column with an index is being added in a separate migration, this will, when being rolled back, cause an error when the column is being dropped, as the fix from #2040 will try to re-add the index for which the referenced column(s) doesn't exist anymore.

I mistakenly assumed that reversed plans would drop indices before removing columns, however while the inverted commands do indeed generate index drops for the to be removed columns, these index drops are being filtered out by the plans conflict resolution:

// Columns that are dropped will automatically cause the indexes to be dropped as well
foreach ($this->columnRemoves as $columnRemove) {
foreach ($columnRemove->getActions() as $action) {
if ($action instanceof RemoveColumn) {
[$this->indexes] = $this->forgetDropIndex(
$action->getTable(),
[$action->getColumn()->getName()],
$this->indexes
);
}
}
}


The error can be reproduced using these two migrations, where table creation comes first, and then table alteration. Rolling back the alteration migration will trigger the error.

== 20221014120113 RollbackFailureAlter: reverting

In PdoAdapter.php line 192:

SQLSTATE[HY000]: General error: 1 no such column: foo_id

<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class RollbackFailureCreate extends AbstractMigration
{
    public function change()
    {
        $this
            ->table('test')
            ->create();
    }
}
<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class RollbackFailureAlter extends AbstractMigration
{
    public function change()
    {
        $this
            ->table('test')
            ->addColumn('foo_id', 'integer')
            ->addIndex(['foo_id'], ['name' => 'FOO_ID_IDX'])
            ->update();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions