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