This is an automated email from the git hooks/post-receive script. New commit to branch feature/1161 in repository lima. See http://git.chorem.org/lima.git commit 38825431edce68f9c5224aede9fad03d46515e8f Author: Sylvain Bavencoff <bavencoff@codelutin.com> Date: Fri Feb 27 14:17:56 2015 +0100 refs #1161 : activate flyway migration service --- .../chorem/lima/business/LimaServiceConfig.java | 4 +- lima-callao/pom.xml | 6 ++ .../chorem/lima/entity/LimaFlywayServiceImpl.java | 95 ++++++++++++++++++++++ .../db/migration/V0_5_0_0__migration1.sql | 3 + .../db/migration/V0_6_0_0__migration2.sql | 23 ++++++ 5 files changed, 129 insertions(+), 2 deletions(-) diff --git a/lima-business/src/main/java/org/chorem/lima/business/LimaServiceConfig.java b/lima-business/src/main/java/org/chorem/lima/business/LimaServiceConfig.java index 66a079b..7664760 100644 --- a/lima-business/src/main/java/org/chorem/lima/business/LimaServiceConfig.java +++ b/lima-business/src/main/java/org/chorem/lima/business/LimaServiceConfig.java @@ -29,11 +29,11 @@ import org.apache.commons.logging.LogFactory; import org.chorem.lima.LimaTechnicalException; import org.chorem.lima.business.accountingrules.FranceAccountingRules; import org.chorem.lima.entity.LimaCallaoEntityEnum; +import org.chorem.lima.entity.LimaFlywayServiceImpl; import org.nuiton.config.ApplicationConfig; import org.nuiton.config.ArgumentsParserException; import org.nuiton.config.ConfigOptionDef; import org.nuiton.topia.flyway.TopiaFlywayService; -import org.nuiton.topia.flyway.TopiaFlywayServiceImpl; import org.nuiton.topia.persistence.TopiaConfigurationConstants; import java.io.File; @@ -100,7 +100,7 @@ public class LimaServiceConfig { result.setProperty(TopiaConfigurationConstants.CONFIG_PERSISTENCE_CLASSES, LimaCallaoEntityEnum.getImplementationClassesAsString()); Map<String, String> toAddIfNotPresent = Maps.newLinkedHashMap(); - toAddIfNotPresent.put("topia.service.migration", TopiaFlywayServiceImpl.class.getName()); + toAddIfNotPresent.put("topia.service.migration", LimaFlywayServiceImpl.class.getName()); toAddIfNotPresent.put("topia.service.migration." + TopiaFlywayService.USE_MODEL_VERSION, "true"); toAddIfNotPresent.put(TopiaConfigurationConstants.CONFIG_PERSISTENCE_INIT_SCHEMA, "true"); diff --git a/lima-callao/pom.xml b/lima-callao/pom.xml index 666a3ad..5c0029f 100644 --- a/lima-callao/pom.xml +++ b/lima-callao/pom.xml @@ -49,11 +49,17 @@ <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> + <scope>compile</scope> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency> + <dependency> + <groupId>org.nuiton.topia</groupId> + <artifactId>topia-service-flyway</artifactId> + <version>3.0-SNAPSHOT</version> + </dependency> </dependencies> <build> diff --git a/lima-callao/src/main/java/org/chorem/lima/entity/LimaFlywayServiceImpl.java b/lima-callao/src/main/java/org/chorem/lima/entity/LimaFlywayServiceImpl.java new file mode 100644 index 0000000..d239e3f --- /dev/null +++ b/lima-callao/src/main/java/org/chorem/lima/entity/LimaFlywayServiceImpl.java @@ -0,0 +1,95 @@ +package org.chorem.lima.entity; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.h2.Driver; +import org.nuiton.topia.flyway.TopiaFlywayServiceImpl; +import org.nuiton.topia.persistence.TopiaApplicationContext; +import org.nuiton.topia.persistence.TopiaConfigurationConstants; +import org.nuiton.topia.persistence.TopiaException; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Map; + +/** + * @author Sylvain Bavencoff - bavencoff@codelutin.com + */ +public class LimaFlywayServiceImpl extends TopiaFlywayServiceImpl { + + private static final Log log = LogFactory.getLog(LimaFlywayServiceImpl.class); + + protected final static String INITIAL_VERSION = "0.3.0.0"; + protected final static String OLD_TABLE_VERSION = "TMS_VERSION"; + protected final static String VERSION_FROM_OLD_TABLE_VERSION = "SELECT VERSION FROM " + OLD_TABLE_VERSION + ";"; + protected final static String FLYWAY_TABLE_VERSION = "schema_version"; + + protected Connection getConnection(TopiaApplicationContext topiaApplicationContext) throws SQLException { + + Map<String, String> configuration = topiaApplicationContext.getConfiguration(); + + String url = configuration.get(TopiaConfigurationConstants.CONFIG_URL); + String user = configuration.get(TopiaConfigurationConstants.CONFIG_USER); + String password = configuration.get(TopiaConfigurationConstants.CONFIG_PASS); + + DriverManager.registerDriver(new Driver()); + Connection connection = DriverManager.getConnection(url, user, password); + + return connection; + + } + + @Override + public void initTopiaService(TopiaApplicationContext topiaApplicationContext, Map<String, String> serviceConfiguration) { + + Connection connection = null; + ResultSet flywayTableVersion = null; + ResultSet topiaTableVersion = null; + PreparedStatement preparedStatement = null; + ResultSet topiaVersion = null; + + try { + connection = getConnection(topiaApplicationContext); + flywayTableVersion = connection.getMetaData().getTables(null, null, FLYWAY_TABLE_VERSION, null); + + if (!flywayTableVersion.first()) { + topiaTableVersion = connection.getMetaData().getTables(null, null, OLD_TABLE_VERSION, null); + if (topiaTableVersion.first()) { + preparedStatement = connection.prepareStatement(VERSION_FROM_OLD_TABLE_VERSION); + topiaVersion = preparedStatement.executeQuery(); + topiaVersion.first(); + flywayBaselineVersion = topiaVersion.getString(1); + } else { + flywayBaselineVersion = INITIAL_VERSION; + } + } + + } catch (SQLException e) { + throw new TopiaException(e); + } finally { + closeQuietly(connection, flywayTableVersion, topiaTableVersion, preparedStatement, topiaVersion); + } + + super.initTopiaService(topiaApplicationContext, serviceConfiguration); + + } + + public static void closeQuietly(AutoCloseable... toClose) { + for (AutoCloseable closeable : toClose) { + if (closeable != null) { + try { + closeable.close(); + } catch (Exception e) { + if (log.isErrorEnabled()) { + log.error("Unable to close", e); + } + } + } + } + } + + +} diff --git a/lima-callao/src/main/resources/db/migration/V0_5_0_0__migration1.sql b/lima-callao/src/main/resources/db/migration/V0_5_0_0__migration1.sql new file mode 100644 index 0000000..9d4bfc5 --- /dev/null +++ b/lima-callao/src/main/resources/db/migration/V0_5_0_0__migration1.sql @@ -0,0 +1,3 @@ + +-- migration Lima data base from 0.4 to 0.5 +Alter table record alter AMOUNT clob; \ No newline at end of file diff --git a/lima-callao/src/main/resources/db/migration/V0_6_0_0__migration2.sql b/lima-callao/src/main/resources/db/migration/V0_6_0_0__migration2.sql new file mode 100644 index 0000000..eaf9f96 --- /dev/null +++ b/lima-callao/src/main/resources/db/migration/V0_6_0_0__migration2.sql @@ -0,0 +1,23 @@ + -- migration Lima data base from 0.5 to 0.6 + +-- migration from h2 to 1.3.166 not supporting index on clob +Alter table account alter column accountnumber varchar; + +-- add new columns +alter table account drop column masteraccount; +alter table account drop column generalledger; +alter table entry add column lettering varchar(30); +alter table financialtransaction drop column financialperiod; + +-- perform some migration +update entry e set lettering = (select l.code from Letter l where l.topiaid = e.letter); + +-- delete non necessary fields +Alter table FINANCIALTRANSACTION drop column AMOUNTDEBIT; +Alter table FINANCIALTRANSACTION drop column AMOUNTCREDIT; +alter table vatstatement drop column amount; +alter table entry drop column letter; + +-- delete tables +drop table record; +drop table letter; -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.