Browse Source

Add database migration functionality

master
Chris Smith 15 years ago
parent
commit
cb6051f228

+ 17
- 4
src/uk/co/md87/evetool/ConfigManager.java View File

@@ -26,6 +26,8 @@ import com.dmdirc.util.ConfigFile;
26 26
 import com.dmdirc.util.InvalidConfigFileException;
27 27
 import java.io.File;
28 28
 import java.io.IOException;
29
+import java.lang.reflect.InvocationTargetException;
30
+import java.sql.Connection;
29 31
 import java.util.logging.Level;
30 32
 import java.util.logging.Logger;
31 33
 
@@ -39,6 +41,8 @@ public class ConfigManager {
39 41
     /** Logger to use for this class. */
40 42
     private static final Logger LOGGER = Logger.getLogger(ConfigManager.class.getName());
41 43
 
44
+    private static final int DBVERSION = 2;
45
+
42 46
     private final ConfigFile configFile;
43 47
  
44 48
     public ConfigManager() {
@@ -59,13 +63,22 @@ public class ConfigManager {
59 63
             LOGGER.log(Level.WARNING, "Error parsing config file", ex);
60 64
         }
61 65
 
62
-        initDefaults();
63
-
64 66
         Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownHook()));
65 67
     }
66 68
 
67
-    protected void initDefaults() {
68
-        setGeneralSetting("dbVersion", getGeneralSettingInt("dbVersion", 1));
69
+    public void checkDatabase(final Connection conn) {
70
+        final DBMigrator migrator = new DBMigrator(conn);
71
+        for (int i = getGeneralSettingInt("dbVersion", DBVERSION) + 1; i <= DBVERSION; i++) {
72
+            LOGGER.log(Level.INFO, "Running database migration method #" + i);
73
+            
74
+            try {
75
+                DBMigrator.class.getMethod("migrateTo" + i).invoke(migrator);
76
+            } catch (Exception ex) {
77
+                LOGGER.log(Level.SEVERE, "Unable to run database migration", ex);
78
+            }
79
+        }
80
+
81
+        setGeneralSetting("dbVersion", DBVERSION);
69 82
     }
70 83
 
71 84
     public String getGeneralSetting(final String key) {

+ 48
- 0
src/uk/co/md87/evetool/DBMigrator.java View File

@@ -0,0 +1,48 @@
1
+/*
2
+ * Copyright (c) 2009 Chris Smith
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package uk.co.md87.evetool;
24
+
25
+import java.sql.Connection;
26
+import java.sql.SQLException;
27
+
28
+/**
29
+ *
30
+ * TODO: Document DBMigrator
31
+ * @author chris
32
+ */
33
+public class DBMigrator {
34
+
35
+    private final Connection conn;
36
+
37
+    public DBMigrator(final Connection conn) {
38
+        this.conn = conn;
39
+    }
40
+
41
+    public void migrateTo2() throws SQLException {
42
+        // The API changed to include a new raceID field, but the data may
43
+        // have previously been cached for months
44
+        conn.prepareStatement("DELETE FROM PageCache WHERE pc_method LIKE " +
45
+                "'http://evetool.md87.co.uk/%'").execute();
46
+    }
47
+
48
+}

+ 2
- 0
src/uk/co/md87/evetool/Main.java View File

@@ -58,6 +58,8 @@ public class Main {
58 58
         final ConfigManager config = new ConfigManager();
59 59
         final ApiFactory factory = new ApiFactory(config);
60 60
 
61
+        config.checkDatabase(factory.getConnection());
62
+
61 63
         readVersion();
62 64
         initTables(factory);
63 65
 

Loading…
Cancel
Save