Fixed db upgrade for 2.1-3.0

This commit is contained in:
Alena Prokharchyk 2012-04-20 11:54:53 -07:00
parent 102bc1f39f
commit e4e0fff2eb
2 changed files with 31 additions and 1 deletions

View File

@ -74,6 +74,7 @@ public class Upgrade301to302 implements DbUpgrade {
public void performDataMigration(Connection conn) {
dropKeysIfExists(conn);
updateSharedNetworks(conn);
fixLastHostIdKey(conn);
}
@Override
@ -157,4 +158,34 @@ public class Upgrade301to302 implements DbUpgrade {
}
}
}
private void fixLastHostIdKey(Connection conn) {
//Drop i_usage_event__created key (if exists) and re-add it again
List<String> keys = new ArrayList<String>();
//Drop vmInstance keys (if exists) and insert one with correct name
keys = new ArrayList<String>();
keys.add("fk_vm_instance__last_host_id");
keys.add("i_vm_instance__last_host_id");
DbUpgradeUtils.dropKeysIfExist(conn, "cloud.vm_instance", keys, true);
DbUpgradeUtils.dropKeysIfExist(conn, "cloud.vm_instance", keys, false);
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`vm_instance` ADD CONSTRAINT `fk_vm_instance__last_host_id` FOREIGN KEY (`last_host_id`) REFERENCES `host` (`id`)");
pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
throw new CloudRuntimeException("Unable to insert foreign key in vm_instance table ", e);
}finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
}
}
}
}

View File

@ -71,7 +71,6 @@ DELETE FROM op_ha_work WHERE taken IS NOT NULL;
DELETE FROM op_ha_work WHERE host_id NOT IN (SELECT DISTINCT id FROM host);
UPDATE `cloud`.`vm_instance` SET last_host_id=NULL WHERE last_host_id NOT IN (SELECT DISTINCT id FROM host);
ALTER TABLE `cloud`.`vm_instance` ADD CONSTRAINT `fk_vm_instance__last_host_id` FOREIGN KEY `fk_vm_instance__last_host_id` (`last_host_id`) REFERENCES `host`(`id`);
UPDATE `cloud`.`vm_instance` SET domain_id=1, account_id=1 where account_id not in (select distinct id from account) or domain_id not in (select distinct id from domain);
ALTER TABLE `cloud`.`vm_instance` ADD CONSTRAINT `fk_vm_instance__account_id` FOREIGN KEY `fk_vm_instance__account_id` (`account_id`) REFERENCES `account` (`id`);