yii2: Migration error handling
Hi, I noticed troublesome behavior of migrations. As en example, let’s say I have following code:
public function up() {
...
$this->createTable($this->profiles, [
'user_id' => Schema::TYPE_INTEGER,
'firstname' => 'VARCHAR(64) DEFAULT NULL',
'lastname' => 'VARCHAR(64) DEFAULT NULL',
], $tableOptions);
$this->addForeignKey('profile_user', $this->profiles, 'user_id' , '{{%users}}', 'id', 'CASCADE', 'CASCADE');
}
My user table is named user not users so the key won’t be added, but profiles table will. Obviously - as the migration is not complete - it is not in the migrations table and it cannot be reverted using yii migrate/down command. It has to be done manually.
The code above is easy to revert manually, but it could be much more complicated. Wouldn’t it be good if migrating system (when migration up fails at some point) would autmatically call down()?
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 24 (17 by maintainers)
@qiangxue I see, that’s solid argument against.I was trying to use the
tobut it says that nothing needs to be done. I checked theactionToand as far as I can see only migrating up inmigrateToVersionmethod gets the list of migrations from list of files (which is obvious as they cannot be present in migrations db table). In the end - migrating down failed migration cannot be done as in bothmigrateToTimeandmigrateToVersionmethods down-migrating is based on list of migrations in database but failed migration are not present there. Unless I’m wrong about this.I agree with not using simple names with
downbut using full name should be fine, right?