0

For proper migration rollback (either through errors or manual operation), does one need to implement specific things in the migration to ensure this can be done?

For instance, if I only have change(), which has updates, how will it know how to do the rollback? Should I implement up() and down() for consistency and to ensure completion of rollback capabilities?

Edit #1

I think I should have specified the following in my previous submission. My change() migration has the following code:

public function change()
{
    //rename values
    $this->query("update table_name_here set name_en='new value en' name_fr='new value fr' where id = 7");

    ...
}
TechFanDan
  • 3,329
  • 6
  • 46
  • 89

1 Answers1

1

This is mentioned in the docs here:

Phinx 0.2.0 introduced a new feature called reversible migrations. This feature has now become the default migration method. With reversible migrations, you only need to define the up logic, and Phinx can figure out how to migrate down automatically for you. For example:

[...]

The following actions are reversible when done through the Table API in Phinx, and will be automatically reversed:

  • Creating a table
  • Renaming a table
  • Adding a column
  • Renaming a column
  • Adding an index
  • Adding a foreign key

[...]

If a command cannot be reversed then Phinx will throw an IrreversibleMigrationException when it’s migrating down. If you wish to use a command that cannot be reversed in the change function, you can use an if statement with $this->isMigratingUp() to only run things in the up or down direction.

https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method

So, if your migration only uses actions in the change() method that are reversible, then you don't need to do anything else, Phinx can figure the required inverted commands out on its own.

If however you're using actions that are not automatically reversible, either because they're not covered, or because your not using Phinx's Table API, but say for example custom raw SQL, then you have to make sure that you implement the UP/DOWN logic yourself, either using the up()/down() methods instead of the change() method (you cannot use both), or by checking $this->isMigratingUp() in the change() method to conditionally run either UP or DOWN logic.

ndm
  • 59,784
  • 9
  • 71
  • 110
  • I did have to update and add Edit #1 after your answer. In my case, I'm using a query to update the database. From the documentation you specified, it seems that I need to alter my statement to use the appropriate update process. Thanks for this! – TechFanDan May 24 '22 at 14:38
  • @TechFanDan The query builder is not part of the table API (`$this->table()`), so you'd have to write custom DOWN migration logic for that change. That is of course assuming that you'd actually want to change it back when migrating down, depending on the circumstances it can be perfectly valid to not change things back. – ndm May 24 '22 at 14:42
  • noted and will take this into consideration. – TechFanDan May 24 '22 at 14:47