diff --git a/src/java/com/metabase/corvus/migrations/LiquibaseMigrations.java b/src/java/com/metabase/corvus/migrations/LiquibaseMigrations.java deleted file mode 100644 index c40f4fa0d693d1de46656e719ede0f5ffa9ff1f3..0000000000000000000000000000000000000000 --- a/src/java/com/metabase/corvus/migrations/LiquibaseMigrations.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.metabase.corvus.migrations; - -import java.io.FileWriter; -import java.io.StringWriter; -import java.sql.SQLException; -import liquibase.Liquibase; -import liquibase.database.DatabaseFactory; -import liquibase.database.Database; -import liquibase.database.jvm.JdbcConnection; -import liquibase.exception.DatabaseException; -import liquibase.resource.ClassLoaderResourceAccessor; - - -public class LiquibaseMigrations { - - private static final String LIQUIBASE_CHANGELOG = "migrations/liquibase.json"; - - - public static final void setupDatabase(java.sql.Connection dbConnection) throws Exception { - try { - Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(dbConnection)); - Liquibase liquibase = new Liquibase(LIQUIBASE_CHANGELOG, new ClassLoaderResourceAccessor(), database); - liquibase.update(""); - } catch (Exception e) { - throw new DatabaseException(e); - } finally { - if (dbConnection != null) { - try { - dbConnection.rollback(); - dbConnection.close(); - } catch (SQLException e){ - //nothing to do - } - } - } - } - - public static final void teardownDatabase(java.sql.Connection dbConnection) throws Exception { - try { - Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(dbConnection)); - Liquibase liquibase = new Liquibase(LIQUIBASE_CHANGELOG, new ClassLoaderResourceAccessor(), database); - liquibase.rollback(10000, ""); - } catch (Exception e) { - throw new DatabaseException(e); - } finally { - if (dbConnection != null) { - try { - dbConnection.rollback(); - dbConnection.close(); - } catch (SQLException e){ - //nothing to do - } - } - } - } - - public static final String genSqlDatabase(java.sql.Connection dbConnection) throws Exception { - try { - StringWriter strw = new StringWriter(); - Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(dbConnection)); - Liquibase liquibase = new Liquibase(LIQUIBASE_CHANGELOG, new ClassLoaderResourceAccessor(), database); - liquibase.update("", strw); - return strw.toString(); - } catch (Exception e) { - throw new DatabaseException(e); - } finally { - if (dbConnection != null) { - try { - dbConnection.rollback(); - dbConnection.close(); - } catch (SQLException e){ - //nothing to do - } - } - } - } - -} diff --git a/src/metabase/db.clj b/src/metabase/db.clj index 7759505bda895e51a008847b4a45a5a99c07f641..b10bca61cb3c577d442862e25ab29c1c1f7c89f2 100644 --- a/src/metabase/db.clj +++ b/src/metabase/db.clj @@ -12,7 +12,13 @@ [metabase.db.internal :as i] [metabase.models.interface :as models] [metabase.util :as u]) - (:import com.metabase.corvus.migrations.LiquibaseMigrations)) + (:import java.io.StringWriter + java.sql.Connection + liquibase.Liquibase + (liquibase.database DatabaseFactory Database) + liquibase.database.jvm.JdbcConnection + liquibase.exception.DatabaseException + liquibase.resource.ClassLoaderResourceAccessor)) ;; ## DB FILE, JDBC/KORMA DEFINITONS @@ -54,14 +60,25 @@ ;; ## MIGRATE +(def ^:private ^:const changelog-file + "migrations/liquibase.json") + (defn migrate "Migrate the database `:up`, `:down`, or `:print`." [jdbc-connection-details direction] - (let [conn (jdbc/get-connection jdbc-connection-details)] - (case direction - :up (LiquibaseMigrations/setupDatabase conn) - :down (LiquibaseMigrations/teardownDatabase conn) - :print (LiquibaseMigrations/genSqlDatabase conn)))) + (try + (jdbc/with-db-transaction [conn jdbc-connection-details] + (let [^Database database (-> (DatabaseFactory/getInstance) + (.findCorrectDatabaseImplementation (JdbcConnection. (jdbc/get-connection conn)))) + ^Liquibase liquibase (Liquibase. changelog-file (ClassLoaderResourceAccessor.) database)] + (case direction + :up (.update liquibase "") + :down (.rollback liquibase 10000 "") + :print (let [writer (StringWriter.)] + (.update liquibase "" writer) + (.toString writer))))) + (catch Throwable e + (throw (DatabaseException. e))))) ;; ## SETUP-DB