]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/database/DatabaseConnection.java
FIX: coverity resource leaks.
[gigi.git] / src / org / cacert / gigi / database / DatabaseConnection.java
index 661565d843a0a3f1b428a8f114d6c92cfc34129f..ef9b19cc71cbab89c6bbb5193479036eb90d1dd9 100644 (file)
@@ -1,5 +1,7 @@
 package org.cacert.gigi.database;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.PreparedStatement;
@@ -9,8 +11,12 @@ import java.sql.Statement;
 import java.util.HashMap;
 import java.util.Properties;
 
+import org.cacert.gigi.database.SQLFileManager.ImportType;
+
 public class DatabaseConnection {
 
+    public static final int CURRENT_SCHEMA_VERSION = 1;
+
     public static final int CONNECTION_TIMEOUT = 24 * 60 * 60;
 
     private Connection c;
@@ -96,6 +102,42 @@ public class DatabaseConnection {
             throw new Error("Re-initiaizing is forbidden.");
         }
         credentials = conf;
+        GigiResultSet rs = getInstance().prepare("SELECT version FROM schemeVersion ORDER BY version DESC LIMIT 1").executeQuery();
+        int version = 0;
+        if (rs.next()) {
+            version = rs.getInt(1);
+        }
+        if (version == CURRENT_SCHEMA_VERSION) {
+            return; // Good to go
+        }
+        if (version > CURRENT_SCHEMA_VERSION) {
+            throw new Error("Invalid database version. Please fix this.");
+        }
+        upgrade(version);
+    }
+
+    private static void upgrade(int version) {
+        try {
+            Statement s = getInstance().c.createStatement();
+            while (version < CURRENT_SCHEMA_VERSION) {
+                try (InputStream resourceAsStream = DatabaseConnection.class.getResourceAsStream("upgrade/from_" + version + ".sql")) {
+                    if (resourceAsStream == null) {
+                        throw new Error("Upgrade script from version " + version + " was not found.");
+                    }
+                    SQLFileManager.addFile(s, resourceAsStream, ImportType.PRODUCTION);
+                }
+                version++;
+            }
+            s.addBatch("INSERT INTO schemeVersion SET version='" + version + "'");
+            System.out.println("UPGRADING Database to version " + version);
+            s.executeBatch();
+            System.out.println("done.");
+            s.close();
+        } catch (SQLException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
     }
 
     public void beginTransaction() throws SQLException {