protected function run1($stage)
{
$this->console->out("Creating managed_tmp table...");
$this->db->Execute("CREATE TABLE managed_tmp LIKE managed");
$this->console->out("Applying changes to managed_tmp table...");
$this->db->Execute("\n ALTER TABLE managed_tmp\n ADD CONSTRAINT `fk_managed_poller_sessions` FOREIGN KEY (`sid`)\n REFERENCES `poller_sessions` (`sid`) \n ON DELETE CASCADE\n ");
$this->console->out("Swapping table names...");
$this->db->Execute("RENAME TABLE managed TO managed_backup, managed_tmp TO managed");
$this->console->out("Copying actual data...");
$this->db->Execute("\n INSERT IGNORE INTO managed\n SELECT mb.* FROM managed_backup mb\n JOIN poller_sessions ps USING(sid)\n ");
$this->console->out("Dropping managed_tmp table...");
$this->db->Execute("DROP TABLE IF EXISTS managed_backup");
}