Nah, it doesn't work like this.
It is very close to magento upgrade system so each loop is one migration that upgrades module to specific version. Let me give you an example what migrationClass
can be:
<?php
namespace ABC\Task\Migrations;
use Phalcon\Db\Column;
use Phalcon\Db\Index;
use Phalcon\Db\Reference;
use ABC\Core\Migrations\Migration;
use ABC\Task\Models\TaskStatus;
/**
* Class Migration_109
*/
class Migration_109 extends Migration
{
const VERSION = '1.0.9';
/**
* Define the table structure
*
* @return void
*/
public function up()
{
$this->db->createTable('task_statuses', null, [
'columns' => [
new Column('id', [
'type' => Column::TYPE_INTEGER,
'unsigned' => true,
'notNull' => true,
'autoIncrement' => true,
'size' => 11,
'first' => true
]),
new Column('label', [
'type' => Column::TYPE_VARCHAR,
'notNull' => true,
'size' => 255,
'after' => 'id'
]),
new Column('icon', [
'type' => Column::TYPE_VARCHAR,
'notNull' => true,
'size' => 255,
'after' => 'label'
]),
new Column('color', [
'type' => Column::TYPE_VARCHAR,
'notNull' => true,
'size' => 20,
'after' => 'icon'
]),
new Column('isDefault', [
'type' => Column::TYPE_BOOLEAN,
'notNull' => true,
'default' => 0,
])
],
'indexes' => [
new Index('PRIMARY', ['id'], 'PRIMARY')
],
'options' => [
'TABLE_TYPE' => 'BASE TABLE',
'ENGINE' => 'InnoDB',
'TABLE_COLLATION' => 'utf8_general_ci'
],
]);
$newStatus = new TaskStatus();
$newStatus->color = '#00FF00';
$newStatus->isDefault = true;
$newStatus->icon = 'fa fa-glass';
$newStatus->label = 'Nowy';
$newStatus->save();
$defaultStatusId = $newStatus->id;
$this->db->execute("ALTER TABLE `tasks` CHANGE COLUMN `type` `typeId` INT(11) UNSIGNED NOT NULL;");
$this->db->execute("ALTER TABLE `tasks` CHANGE COLUMN `priority` `priorityId` INT(11) UNSIGNED NOT NULL;");
$this->db->addColumn('tasks', null, new Column('statusId', [
'type' => Column::TYPE_INTEGER,
'unsigned' => true,
'size' => 11,
'after' => 'priorityId',
]));
$this->db->execute("UPDATE `tasks` SET statusId = {$defaultStatusId}");
$this->db->modifyColumn('tasks', null, new Column('statusId', [
'type' => Column::TYPE_INTEGER,
'unsigned' => true,
'notNull' => true,
'size' => 11,
'after' => 'priorityId',
]));
$this->db->addIndex('tasks', null, new Index('FK_TASKS_TYPE', ['typeId']));
$this->db->addIndex('tasks', null, new Index('FK_TASKS_PRIORITY', ['priorityId']));
$this->db->addIndex('tasks', null, new Index('FK_TASKS_STATUS', ['statusId']));
$this->db->addForeignKey('tasks', null, new Reference('FK_TASKS_TYPE', [
'referencedTable' => 'task_types',
'columns' => ['typeId'],
'referencedColumns' => ['id'],
'onUpdate' => 'NO ACTION',
'onDelete' => 'RESTRICT'
]));
$this->db->addForeignKey('tasks', null, new Reference('FK_TASKS_PRIORITY', [
'referencedTable' => 'task_priorities',
'columns' => ['priorityId'],
'referencedColumns' => ['id'],
'onUpdate' => 'NO ACTION',
'onDelete' => 'RESTRICT'
]));
$this->db->addForeignKey('tasks', null, new Reference('FK_TASKS_STATUS', [
'referencedTable' => 'task_statuses',
'columns' => ['statusId'],
'referencedColumns' => ['id'],
'onUpdate' => 'NO ACTION',
'onDelete' => 'RESTRICT'
]));
}
}
Michal is right, Mysql can't rollback table creation. And I was pretty sure magento could rollback this somewhow but I was probably mistaken.