@Test
  public void testDropPrimaryKey() throws Exception {
    Connection conn = connectionMock;
    String tableName = "tableName";

    DbUpgradeUtils.dropPrimaryKeyIfExists(conn, tableName);

    verify(daoMock, times(1)).dropPrimaryKey(conn, tableName);
  }
Ejemplo n.º 2
0
  private void fixForeignKeys(Connection conn) {
    HashMap<String, List<String>> foreignKeys = new HashMap<String, List<String>>();
    List<String> keys = new ArrayList<String>();
    keys.add("fk_networks__data_center_id");
    foreignKeys.put("networks", keys);

    // drop all foreign keys
    s_logger.debug("Dropping old key fk_networks__data_center_id...");
    for (String tableName : foreignKeys.keySet()) {
      DbUpgradeUtils.dropKeysIfExist(conn, tableName, foreignKeys.get(tableName), true);
    }

    try {
      PreparedStatement pstmt =
          conn.prepareStatement(
              "ALTER TABLE `cloud`.`networks` ADD CONSTRAINT `fk_networks__data_center_id` FOREIGN KEY (`data_center_id`) REFERENCES `data_center`(`id`) ON DELETE CASCADE");
      pstmt.executeUpdate();
    } catch (SQLException e) {
      throw new CloudRuntimeException("Unable to reinsert data center key for the network", e);
    }

    // drop primary keys
    DbUpgradeUtils.dropPrimaryKeyIfExists(conn, "cloud_usage.usage_load_balancer_policy");
    DbUpgradeUtils.dropPrimaryKeyIfExists(conn, "cloud_usage.usage_port_forwarding");

    // Drop usage_network_offering unique key
    try {
      PreparedStatement pstmt =
          conn.prepareStatement(
              "drop index network_offering_id on cloud_usage.usage_network_offering");
      pstmt.executeUpdate();
      s_logger.debug("Dropped usage_network_offering unique key");
    } catch (Exception e) {
      // Ignore error if the usage_network_offering table or the unique key doesn't exist
    }
  }