From 95a7cb2b8b1b11e7bf1fa5c4c49858756593db5b Mon Sep 17 00:00:00 2001 From: Winston Li Date: Sun, 4 Sep 2016 10:50:51 +0100 Subject: [PATCH] Add check for last_accessed column in case adding failed due to an error rather than because it already exists --- .../bridge/db/sqlite/SqliteDBStore.java | 4 +++ .../query/LastAccessedColumnExists.java | 31 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/sqlite/query/LastAccessedColumnExists.java diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/sqlite/SqliteDBStore.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/sqlite/SqliteDBStore.java index 2a62aeda78..182cac123f 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/sqlite/SqliteDBStore.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/sqlite/SqliteDBStore.java @@ -1,5 +1,6 @@ package uk.ac.ic.wlgitbridge.bridge.db.sqlite; +import com.google.common.base.Preconditions; import uk.ac.ic.wlgitbridge.bridge.db.DBInitException; import uk.ac.ic.wlgitbridge.bridge.db.DBStore; import uk.ac.ic.wlgitbridge.bridge.db.ProjectState; @@ -143,6 +144,9 @@ public class SqliteDBStore implements DBStore { new CreateIndexURLIndexStore(), new CreateProjectsIndexLastAccessed() ).forEach(this::update); + /* In the case of needing to change the schema, we need to check that + ProjectsAddLastAccessed didn't just fail */ + Preconditions.checkState(query(new LastAccessedColumnExists())); } private void update(SQLUpdate update) { diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/sqlite/query/LastAccessedColumnExists.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/sqlite/query/LastAccessedColumnExists.java new file mode 100644 index 0000000000..da3b525ff3 --- /dev/null +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/sqlite/query/LastAccessedColumnExists.java @@ -0,0 +1,31 @@ +package uk.ac.ic.wlgitbridge.bridge.db.sqlite.query; + +import uk.ac.ic.wlgitbridge.bridge.db.sqlite.SQLQuery; + +import java.sql.ResultSet; +import java.sql.SQLException; + +/** + * Created by winston on 04/09/2016. + */ +public class LastAccessedColumnExists implements SQLQuery { + + private static final String LAST_ACCESSED_COLUMN_EXISTS = + "PRAGMA table_info(`projects`)"; + + @Override + public String getSQL() { + return LAST_ACCESSED_COLUMN_EXISTS; + } + + @Override + public Boolean processResultSet(ResultSet resultSet) throws SQLException { + while (resultSet.next()) { + if (resultSet.getString(2).equals("last_accessed")) { + return true; + } + } + return false; + } + +}