Skip to content

Commit ea0dec7

Browse files
miguelaferreiraDaanHoogland
authored andcommitted
Refactor DbUpgradeUtils
- Move database access code to new class DatabaseAccessObject. This was done to ease the effort of testing, since DbUpgradeUtils has a static API and it is harder to mock static things with Mockito. - Log exceptions even if ignored - Add unit tests for both DbUpgradeUtils and DatabaseAccessObject - DbUpgradeUtils.dropTableColumnsIfExist(...) no longer throws CloudRuntimeException to make it consistent with the other methods in the class Signed-off-by: Daan Hoogland <daan@onecht.net>
1 parent 370554e commit ea0dec7

4 files changed

Lines changed: 732 additions & 68 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.upgrade.dao;
18+
19+
import java.sql.Connection;
20+
import java.sql.PreparedStatement;
21+
import java.sql.SQLException;
22+
23+
import org.apache.log4j.Logger;
24+
25+
public class DatabaseAccessObject {
26+
27+
private static Logger s_logger = Logger.getLogger(DatabaseAccessObject.class);
28+
29+
public void dropKey(Connection conn, String tableName, String key, boolean isForeignKey) {
30+
PreparedStatement pstmt = null;
31+
try {
32+
if (isForeignKey) {
33+
pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP FOREIGN KEY " + key);
34+
} else {
35+
pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP KEY " + key);
36+
}
37+
pstmt.executeUpdate();
38+
s_logger.debug("Key " + key + " is dropped successfully from the table " + tableName);
39+
} catch (SQLException e) {
40+
s_logger.warn("Ignored SQL Exception when trying to drop " + (isForeignKey ? "foreign " : "") + "key " + key + " on table " + tableName, e);
41+
} finally {
42+
closePreparedStatement(pstmt, "Ignored SQL Exception when trying to close PreparedStatement atfer dropping " + (isForeignKey ? "foreign " : "") + "key " + key
43+
+ " on table " + tableName);
44+
}
45+
}
46+
47+
public void dropPrimaryKey(Connection conn, String tableName) {
48+
PreparedStatement pstmt = null;
49+
try {
50+
pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP PRIMARY KEY ");
51+
pstmt.executeUpdate();
52+
s_logger.debug("Primary key is dropped successfully from the table " + tableName);
53+
} catch (SQLException e) {
54+
s_logger.warn("Ignored SQL Exception when trying to drop primary key on table " + tableName, e);
55+
} finally {
56+
closePreparedStatement(pstmt, "Ignored SQL Exception when trying to close PreparedStatement atfer dropping primary key on table " + tableName);
57+
}
58+
}
59+
60+
public void dropColumn(Connection conn, String tableName, String columnName) {
61+
PreparedStatement pstmt = null;
62+
try {
63+
pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP COLUMN " + columnName);
64+
pstmt.executeUpdate();
65+
s_logger.debug("Column " + columnName + " is dropped successfully from the table " + tableName);
66+
} catch (SQLException e) {
67+
s_logger.warn("Unable to drop columns using query " + pstmt + " due to exception", e);
68+
} finally {
69+
closePreparedStatement(pstmt, "Ignored SQL Exception when trying to close PreparedStatement after dropping column " + columnName + " on table " + tableName);
70+
}
71+
}
72+
73+
public boolean columnExists(Connection conn, String tableName, String columnName) {
74+
boolean columnExists = false;
75+
PreparedStatement pstmt = null;
76+
try {
77+
pstmt = conn.prepareStatement("SELECT " + columnName + " FROM " + tableName);
78+
pstmt.executeQuery();
79+
columnExists = true;
80+
} catch (SQLException e) {
81+
s_logger.warn("Field " + columnName + " doesn't exist in " + tableName, e);
82+
} finally {
83+
closePreparedStatement(pstmt, "Ignored SQL Exception when trying to close PreparedStatement atfer checking if column " + columnName + " existed on table " + tableName);
84+
}
85+
86+
return columnExists;
87+
}
88+
89+
protected static void closePreparedStatement(PreparedStatement pstmt, String errorMessage) {
90+
try {
91+
if (pstmt != null) {
92+
pstmt.close();
93+
}
94+
} catch (SQLException e) {
95+
s_logger.warn(errorMessage, e);
96+
}
97+
}
98+
99+
}

‎engine/schema/src/com/cloud/upgrade/dao/DbUpgradeUtils.java‎

Lines changed: 8 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -17,88 +17,28 @@
1717
package com.cloud.upgrade.dao;
1818

1919
import java.sql.Connection;
20-
import java.sql.PreparedStatement;
21-
import java.sql.SQLException;
2220
import java.util.List;
2321

24-
import org.apache.log4j.Logger;
25-
26-
import com.cloud.utils.exception.CloudRuntimeException;
27-
2822
public class DbUpgradeUtils {
29-
final static Logger s_logger = Logger.getLogger(DbUpgradeUtils.class);
23+
24+
private static DatabaseAccessObject dao = new DatabaseAccessObject();
3025

3126
public static void dropKeysIfExist(Connection conn, String tableName, List<String> keys, boolean isForeignKey) {
3227
for (String key : keys) {
33-
PreparedStatement pstmt = null;
34-
try {
35-
if (isForeignKey) {
36-
pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP FOREIGN KEY " + key);
37-
} else {
38-
pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP KEY " + key);
39-
}
40-
pstmt.executeUpdate();
41-
s_logger.debug("Key " + key + " is dropped successfully from the table " + tableName);
42-
} catch (SQLException e) {
43-
// do nothing here
44-
45-
continue;
46-
} finally {
47-
try {
48-
if (pstmt != null) {
49-
pstmt.close();
50-
}
51-
} catch (SQLException e) {
52-
}
53-
}
28+
dao.dropKey(conn, tableName, key, isForeignKey);
5429
}
5530
}
5631

5732
public static void dropPrimaryKeyIfExists(Connection conn, String tableName) {
58-
PreparedStatement pstmt = null;
59-
try {
60-
pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP PRIMARY KEY ");
61-
pstmt.executeUpdate();
62-
s_logger.debug("Primary key is dropped successfully from the table " + tableName);
63-
} catch (SQLException e) {
64-
// do nothing here
65-
} finally {
66-
try {
67-
if (pstmt != null) {
68-
pstmt.close();
69-
}
70-
} catch (SQLException e) {
71-
}
72-
}
33+
dao.dropPrimaryKey(conn, tableName);
7334
}
7435

7536
public static void dropTableColumnsIfExist(Connection conn, String tableName, List<String> columns) {
76-
PreparedStatement pstmt = null;
77-
try {
78-
for (String column : columns) {
79-
try {
80-
pstmt = conn.prepareStatement("SELECT " + column + " FROM " + tableName);
81-
pstmt.executeQuery();
82-
} catch (SQLException e) {
83-
// if there is an exception, it means that field doesn't exist, so do nothing here
84-
s_logger.trace("Field " + column + " doesn't exist in " + tableName);
85-
continue;
86-
}
87-
88-
pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP COLUMN " + column);
89-
pstmt.executeUpdate();
90-
s_logger.debug("Column " + column + " is dropped successfully from the table " + tableName);
91-
}
92-
} catch (SQLException e) {
93-
s_logger.warn("Unable to drop columns using query " + pstmt + " due to exception", e);
94-
throw new CloudRuntimeException("Unable to drop columns due to ", e);
95-
} finally {
96-
try {
97-
if (pstmt != null) {
98-
pstmt.close();
99-
}
100-
} catch (SQLException e) {
37+
for (String columnName : columns) {
38+
if (dao.columnExists(conn, tableName, columnName)) {
39+
dao.dropColumn(conn, tableName, columnName);
10140
}
10241
}
10342
}
43+
10444
}

0 commit comments

Comments
 (0)