protected function run1($stage)
{
$this->console->out("Creating farm_teams table...");
$this->db->Execute("\n CREATE TABLE `farm_teams` (\n `farm_id` INT(11) NOT NULL COMMENT 'farms.id ref',\n `team_id` INT(11) NOT NULL COMMENT 'account_teams.id ref',\n PRIMARY KEY (`farm_id`,`team_id`),\n KEY `idx_team_id` (`team_id`),\n CONSTRAINT `fk_8e10336984d4` FOREIGN KEY (`farm_id`) REFERENCES `farms` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION,\n CONSTRAINT `fk_91800ad81855` FOREIGN KEY (`team_id`) REFERENCES `account_teams` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION\n ) ENGINE = InnoDB DEFAULT CHARSET = utf8\n ");
$this->db->BeginTrans();
try {
$this->console->out("Copying existing associations between Farms and Teams to newly created table...");
$this->db->Execute("\n INSERT INTO `farm_teams` (`farm_id`, `team_id`)\n SELECT `id`, `team_id` \n FROM `farms` \n WHERE team_id IS NOT NULL\n ");
$this->console->out("Removing obsolete 'team_id' column and 'farms_account_teams_id' key...");
$this->db->Execute("ALTER TABLE `farms` DROP FOREIGN KEY `farms_account_teams_id`");
$this->db->Execute("ALTER TABLE `farms` DROP COLUMN `team_id`");
$this->db->CommitTrans();
} catch (Exception $e) {
$this->console->error("Something went wrong!");
$this->db->RollbackTrans();
throw $e;
}
}