コード例 #1
0
  @Test
  public void testDropKeyIfExistWhenNoKeysAreSupplied() throws Exception {
    Connection conn = connectionMock;
    String tableName = "tableName";
    List<String> keys = new ArrayList<String>();
    boolean isForeignKey = false;

    DbUpgradeUtils.dropKeysIfExist(conn, tableName, keys, isForeignKey);

    verify(daoMock, times(0)).dropKey(eq(conn), eq(tableName), anyString(), eq(isForeignKey));
  }
コード例 #2
0
  @Test
  public void testDropKeyIfExistWhenOneKeysIsSupplied() throws Exception {
    Connection conn = connectionMock;
    String tableName = "tableName";
    String key = "key";
    List<String> keys = Arrays.asList(new String[] {key});
    boolean isForeignKey = false;

    DbUpgradeUtils.dropKeysIfExist(conn, tableName, keys, isForeignKey);

    verify(daoMock, times(1)).dropKey(conn, tableName, key, isForeignKey);
  }
コード例 #3
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
    }
  }