Commit 61bb3b3c authored by Matt Tucker's avatar Matt Tucker Committed by matt

Added automatic database upgrading.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@840 b35dd754-fafc-0310-a699-88a17e54d16e
parent 5190e1d8
...@@ -30,22 +30,6 @@ import java.sql.*; ...@@ -30,22 +30,6 @@ import java.sql.*;
*/ */
public class EmbeddedConnectionProvider implements ConnectionProvider { public class EmbeddedConnectionProvider implements ConnectionProvider {
private static final String CHECK_VERSION =
"SELECT majorVersion, minorVersion FROM jiveVersion";
/**
* Database schema major version. The schema version corresponds to the
* product release version, but may not exactly match in the case that
* the product version has advanced without schema changes.
*/
private static final int CURRENT_MAJOR_VERSION = 2;
/**
* Database schema minor version.
*/
private static final int CURRENT_MINOR_VERSION = 1;
private ConnectionPool connectionPool = null; private ConnectionPool connectionPool = null;
private Object initLock = new Object(); private Object initLock = new Object();
...@@ -96,15 +80,6 @@ public class EmbeddedConnectionProvider implements ConnectionProvider { ...@@ -96,15 +80,6 @@ public class EmbeddedConnectionProvider implements ConnectionProvider {
if (initData) { if (initData) {
initializeDatabase(); initializeDatabase();
} }
// Check to see if the database schema needs to be upgraded.
else {
try {
upgradeDatabase();
}
catch (Exception e) {
Log.error(e);
}
}
} }
catch (IOException ioe) { catch (IOException ioe) {
Log.error("Error starting connection pool.", ioe); Log.error("Error starting connection pool.", ioe);
...@@ -170,7 +145,7 @@ public class EmbeddedConnectionProvider implements ConnectionProvider { ...@@ -170,7 +145,7 @@ public class EmbeddedConnectionProvider implements ConnectionProvider {
break; break;
} }
// Ignore comments and blank lines. // Ignore comments and blank lines.
if (isCommandPart(line)) { if (DbConnectionManager.isSQLCommandPart(line)) {
command.append(line); command.append(line);
} }
if (line.endsWith(";")) { if (line.endsWith(";")) {
...@@ -192,107 +167,8 @@ public class EmbeddedConnectionProvider implements ConnectionProvider { ...@@ -192,107 +167,8 @@ public class EmbeddedConnectionProvider implements ConnectionProvider {
try { in.close(); } try { in.close(); }
catch (Exception e) { } catch (Exception e) { }
} }
if (con != null) {
try { con.close(); }
catch (Exception e) { }
}
}
}
private void upgradeDatabase() {
int majorVersion;
int minorVersion;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = getConnection();
pstmt = con.prepareStatement(CHECK_VERSION);
ResultSet rs = pstmt.executeQuery();
// If no results, assume the version is 2.0.
if (!rs.next()) {
majorVersion = 2;
minorVersion = 0;
}
majorVersion = rs.getInt(1);
minorVersion = rs.getInt(2);
rs.close();
}
catch (SQLException sqle) {
// If the table doesn't exist, an error will be thrown. Therefore
// assume the version is 2.0.
majorVersion = 2;
minorVersion = 0;
}
finally {
try { if (pstmt != null) { pstmt.close(); } }
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } } try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); } catch (Exception e) { Log.error(e); }
} }
if (minorVersion == CURRENT_MINOR_VERSION) {
return;
}
// The database is an old version that needs to be upgraded.
Log.info("Found old database schema (" + majorVersion + "." + minorVersion + "). " +
"Upgrading to latest schema.");
// Run all upgrade scripts until we're up to the latest schema.
for (int i=minorVersion; i<CURRENT_MINOR_VERSION; i++) {
BufferedReader in = null;
try {
// Resource will be like "/database/upgrade/2.0_to_2.1/messenger_hsqldb.sql"
String resourceName = "/database/upgrade/" + CURRENT_MAJOR_VERSION + "." + i +
"_to_" + CURRENT_MAJOR_VERSION + "." + (i+1) + "/messenger_hsqldb.sql";
in = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(
resourceName)));
con = getConnection();
boolean done = false;
while (!done) {
StringBuffer command = new StringBuffer();
while (true) {
String line = in.readLine();
if (line == null) {
done = true;
break;
}
// Ignore comments and blank lines.
if (isCommandPart(line)) {
command.append(line);
}
if (line.endsWith(";")) {
break;
}
}
// Send command to database.
Statement stmt = con.createStatement();
stmt.execute(command.toString());
stmt.close();
}
}
catch (Exception e) {
Log.error(e);
e.printStackTrace();
}
finally {
if (in != null) {
try { in.close(); }
catch (Exception e) { }
}
if (con != null) {
try { con.close(); }
catch (Exception e) { }
}
}
}
}
private static boolean isCommandPart(String line) {
line = line.trim();
if (line.equals("")) {
return false;
}
if (line.startsWith("//")) {
return false;
}
return true;
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment