From b78f646523d30f53884506350a60c6f05ef8620e Mon Sep 17 00:00:00 2001 From: Athou Date: Tue, 11 Jun 2013 17:01:21 +0200 Subject: [PATCH] initial liquibase setup --- pom.xml | 5 + .../commafeed/backend/DatabaseUpdater.java | 64 ++++ .../com/commafeed/backend/StartupBean.java | 6 +- src/main/resources/META-INF/persistence.xml | 4 +- src/main/resources/application.properties | 3 +- .../resources/changelogs/db.changelog-1.0.xml | 341 ++++++++++++++++++ .../changelogs/db.changelog-master.xml | 8 + 7 files changed, 426 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/commafeed/backend/DatabaseUpdater.java create mode 100644 src/main/resources/changelogs/db.changelog-1.0.xml create mode 100644 src/main/resources/changelogs/db.changelog-master.xml diff --git a/pom.xml b/pom.xml index 800dc261..f28bda08 100644 --- a/pom.xml +++ b/pom.xml @@ -161,6 +161,11 @@ + + org.liquibase + liquibase-core + 2.0.5 + com.google.guava diff --git a/src/main/java/com/commafeed/backend/DatabaseUpdater.java b/src/main/java/com/commafeed/backend/DatabaseUpdater.java new file mode 100644 index 00000000..05c286ea --- /dev/null +++ b/src/main/java/com/commafeed/backend/DatabaseUpdater.java @@ -0,0 +1,64 @@ +package com.commafeed.backend; + +import java.sql.Connection; +import java.util.ResourceBundle; + +import javax.ejb.Stateless; +import javax.ejb.TransactionManagement; +import javax.ejb.TransactionManagementType; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.sql.DataSource; + +import liquibase.Liquibase; +import liquibase.database.Database; +import liquibase.database.DatabaseFactory; +import liquibase.database.jvm.JdbcConnection; +import liquibase.resource.ClassLoaderResourceAccessor; +import liquibase.resource.ResourceAccessor; + +@Stateless +@TransactionManagement(TransactionManagementType.BEAN) +public class DatabaseUpdater { + + public void update() { + String datasourceName = ResourceBundle.getBundle("application") + .getString("datasource"); + try { + Context context = null; + Connection connection = null; + try { + Thread currentThread = Thread.currentThread(); + ClassLoader classLoader = currentThread.getContextClassLoader(); + ResourceAccessor accessor = new ClassLoaderResourceAccessor( + classLoader); + + context = new InitialContext(); + DataSource dataSource = (DataSource) context + .lookup(datasourceName); + connection = dataSource.getConnection(); + + Database database = DatabaseFactory.getInstance() + .findCorrectDatabaseImplementation( + new JdbcConnection(connection)); + + Liquibase liq = new Liquibase( + "changelogs/db.changelog-master.xml", accessor, + database); + liq.update("prod"); + } finally { + if (context != null) { + context.close(); + } + if (connection != null) { + connection.close(); + } + } + + } catch (Exception e) { + throw new RuntimeException(e); + } + + } + +} diff --git a/src/main/java/com/commafeed/backend/StartupBean.java b/src/main/java/com/commafeed/backend/StartupBean.java index 86207e79..16e85af3 100644 --- a/src/main/java/com/commafeed/backend/StartupBean.java +++ b/src/main/java/com/commafeed/backend/StartupBean.java @@ -43,6 +43,9 @@ public class StartupBean { public static final String USERNAME_ADMIN = "admin"; public static final String USERNAME_DEMO = "demo"; + @Inject + DatabaseUpdater databaseUpdater; + @Inject FeedDAO feedDAO; @@ -74,6 +77,8 @@ public class StartupBean { private void init() { startupTime = Calendar.getInstance().getTimeInMillis(); + databaseUpdater.update(); + if (userDAO.getCount() == 0) { initialData(); } @@ -148,5 +153,4 @@ public class StartupBean { } } } - } diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml index 32cc98f4..621ad5d7 100644 --- a/src/main/resources/META-INF/persistence.xml +++ b/src/main/resources/META-INF/persistence.xml @@ -4,12 +4,10 @@ xsi:schemaLocation=" http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> - + org.hibernate.ejb.HibernatePersistence ${jpa.datasource.name} - ENABLE_SELECTIVE - diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index b46286e2..e1bf01d2 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,2 @@ -production=${production} \ No newline at end of file +production=${production} +datasource=${jpa.datasource.name} \ No newline at end of file diff --git a/src/main/resources/changelogs/db.changelog-1.0.xml b/src/main/resources/changelogs/db.changelog-1.0.xml new file mode 100644 index 00000000..a3fee438 --- /dev/null +++ b/src/main/resources/changelogs/db.changelog-1.0.xml @@ -0,0 +1,341 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/changelogs/db.changelog-master.xml b/src/main/resources/changelogs/db.changelog-master.xml new file mode 100644 index 00000000..163c0b9d --- /dev/null +++ b/src/main/resources/changelogs/db.changelog-master.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file