]> WPIA git - gigi.git/commitdiff
add: allow restricting sample data with version information
authorFelix Dörre <felix@dogcraft.de>
Mon, 18 Jul 2016 12:33:52 +0000 (14:33 +0200)
committerFelix Dörre <felix@dogcraft.de>
Tue, 19 Jul 2016 05:22:10 +0000 (07:22 +0200)
Change-Id: I809e51f3c965a971594e9c9babed351a89855bb5

src/org/cacert/gigi/database/SQLFileManager.java
util/org/cacert/gigi/util/DatabaseManager.java

index 9f563dbb476062c175928243c16b5bdb5a8326a4..61732182015ddcd007b4b3892750415ab949e8e1 100644 (file)
@@ -22,11 +22,26 @@ public class SQLFileManager {
         /**
          * Execute INSERT statements as-is, and TRUNCATE instead of DROPPING
          */
-        TRUNCATE
+        TRUNCATE,
+        /**
+         * Execute Script as-is if db version is >= specified version in
+         * optional header
+         */
+        SAMPLE_DATA,
     }
 
     public static void addFile(Statement stmt, InputStream f, ImportType type) throws IOException, SQLException {
         String sql = readFile(f);
+        if (type == ImportType.SAMPLE_DATA) {
+            String fl = sql.split("\n")[0];
+            if (fl.matches("--Version: ([0-9]+)")) {
+                int v0 = Integer.parseInt(fl.substring(11));
+                if (DatabaseConnection.CURRENT_SCHEMA_VERSION < v0) {
+                    System.out.println("skipping sample data (data has version " + v0 + ", db has version " + DatabaseConnection.CURRENT_SCHEMA_VERSION + ")");
+                    return;
+                }
+            }
+        }
         sql = sql.replaceAll("--[^\n]*\n", "\n");
         sql = sql.replaceAll("#[^\n]*\n", "\n");
         String[] stmts = sql.split(";");
@@ -46,7 +61,7 @@ public class SQLFileManager {
                 stmt.addBatch(sql2);
                 continue;
             }
-            if (type == ImportType.PRODUCTION || string.startsWith("INSERT")) {
+            if (type == ImportType.PRODUCTION || type == ImportType.SAMPLE_DATA || string.startsWith("INSERT")) {
                 stmt.addBatch(string);
             } else if (type == ImportType.TEST) {
                 stmt.addBatch(string.replace("ENGINE=InnoDB", "ENGINE=Memory"));
index 20df139ac9f8b6f13aa9fc528dd2371d6ab0d177..a6626b746dbc9462c3bc708e96921e8fff73c3e5 100644 (file)
@@ -55,7 +55,7 @@ public class DatabaseManager {
                 File localData = new File("doc/sampleData.sql");
                 if (localData.exists()) {
                     try (FileInputStream f = new FileInputStream(localData)) {
-                        SQLFileManager.addFile(stmt, f, ImportType.PRODUCTION);
+                        SQLFileManager.addFile(stmt, f, ImportType.SAMPLE_DATA);
                     }
                 }
                 stmt.executeBatch();