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");
...
}
CodePudding user response:
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.