Skip to content

Commit 5401ee8

Browse files
author
kishan
committed
add support to run scripts on usage DB
1 parent 9a56017 commit 5401ee8

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

utils/src/com/cloud/utils/db/Transaction.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ public static Connection getStandaloneConnection() {
161161
}
162162
}
163163

164+
public static Connection getStandaloneUsageConnection() {
165+
try {
166+
return s_usageDS.getConnection();
167+
} catch (SQLException e) {
168+
s_logger.warn("Unexpected exception: ", e);
169+
return null;
170+
}
171+
}
172+
164173
protected static boolean checkAnnotation(int stack, Transaction txn) {
165174
final StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
166175
StackElement se = txn.peekInStack(CURRENT_TXN);

utils/test/com/cloud/utils/db/DbTestUtils.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,34 @@ public static void executeScript(String file, boolean autoCommit, boolean stopOn
5858
}
5959
}
6060

61+
public static void executeUsageScript(String file, boolean autoCommit, boolean stopOnError) {
62+
File cleanScript = PropertiesUtil.findConfigFile(file);
63+
if (cleanScript == null) {
64+
throw new RuntimeException("Unable to clean the database because I can't find " + file);
65+
}
66+
67+
Connection conn = Transaction.getStandaloneUsageConnection();
68+
69+
ScriptRunner runner = new ScriptRunner(conn, autoCommit, stopOnError);
70+
FileReader reader;
71+
try {
72+
reader = new FileReader(cleanScript);
73+
} catch (FileNotFoundException e) {
74+
throw new RuntimeException("Unable to read " + file, e);
75+
}
76+
try {
77+
runner.runScript(reader);
78+
} catch (IOException e) {
79+
throw new RuntimeException("Unable to read " + file, e);
80+
} catch (SQLException e) {
81+
throw new RuntimeException("Unable to execute " + file, e);
82+
}
83+
84+
try {
85+
conn.close();
86+
} catch (SQLException e) {
87+
throw new RuntimeException("Unable to close DB connection", e);
88+
}
89+
}
90+
6191
}

0 commit comments

Comments
 (0)