100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Implemented flywaydb for database migrations
Michael Rademaker24e6ec2d68d5
diff --git a/container/build.gradle b/container/build.gradleindex d645d067d..22af78e1e 100644--- a/container/build.gradle+++ b/container/build.gradle@@ -34,6 +34,7 @@ dependencies {compile "org.keycloak:keycloak-admin-client:$keycloakVersion"compile "org.postgresql:postgresql:$postgresJdbcVersion"+ compile "org.flywaydb:flyway-core:$flywaydbVersion"compile "com.zaxxer:HikariCP:$hikaricpVersion"compile "org.hibernate:hibernate-core:$hibernateVersion"compile("org.hibernate:hibernate-spatial:$hibernateVersion") {diff --git a/container/src/main/java/org/openremote/container/persistence/PersistenceService.java b/container/src/main/java/org/openremote/container/persistence/PersistenceService.javaindex 4b70ddf16..8d0d42d4a 100644--- a/container/src/main/java/org/openremote/container/persistence/PersistenceService.java+++ b/container/src/main/java/org/openremote/container/persistence/PersistenceService.java@@ -19,6 +19,9 @@*/package org.openremote.container.persistence;+import org.flywaydb.core.Flyway;+import org.flywaydb.core.api.MigrationInfo;+import org.flywaydb.core.api.MigrationVersion;import org.hibernate.Session;import org.hibernate.engine.spi.SharedSessionContractImplementor;import org.openremote.container.Container;@@ -59,12 +62,16 @@ public class PersistenceService implements ContainerService {public static final int DATABASE_MAX_POOL_SIZE_DEFAULT = 20;public static final String DATABASE_CONNECTION_TIMEOUT_SECONDS = "DATABASE_CONNECTION_TIMEOUT_SECONDS";public static final int DATABASE_CONNECTION_TIMEOUT_SECONDS_DEFAULT = 300;+ public static final String DATABASE_BASELINE = "DATABASE_BASELINE";+ public static final String DATABASE_BASELINE_DEFAULT = "1";+ public static final String DATABASE_BASELINE_DESCRIPTION = "<< OpenRemote BaseLine >>";protected MessageBrokerService messageBrokerService;protected Database database;protected String persistenceUnitName;protected Map<String, Object> persistenceUnitProperties;protected EntityManagerFactory entityManagerFactory;+ protected Flyway flyway;@Overridepublic void init(Container container) throws Exception {@@ -99,6 +106,15 @@ public class PersistenceService implements ContainerService {int connectionTimeoutSeconds = getInteger(container.getConfig(), DATABASE_CONNECTION_TIMEOUT_SECONDS, DATABASE_CONNECTION_TIMEOUT_SECONDS_DEFAULT);LOG.info("Opening database connection: " + connectionUrl);database.open(persistenceUnitProperties, connectionUrl, databaseUsername, databasePassword, connectionTimeoutSeconds, databaseMinPoolSize, databaseMaxPoolSize);++ String baselineVersion = getString(container.getConfig(), DATABASE_BASELINE, DATABASE_BASELINE_DEFAULT);++ flyway = new Flyway();+ flyway.setDataSource(connectionUrl, databaseUsername, databasePassword);+ flyway.setLocations("classpath:org/openremote/container/persistence/migrations");+ flyway.setBaselineVersion(MigrationVersion.fromVersion(baselineVersion));+ flyway.setBaselineDescription(DATABASE_BASELINE_DESCRIPTION);+ flyway.baseline();}@Override@@ -135,9 +151,18 @@ public class PersistenceService implements ContainerService {}public void createSchema() {+generateSchema("create");}+ public void updateSchema() {+ for (MigrationInfo i : flyway.info().all()) {+ LOG.info("Migrate task: " + i.getVersion() + " : "+ + i.getDescription() + " from file: " + i.getScript());+ }+ flyway.migrate();+ }+public void dropSchema() {generateSchema("drop");}diff --git a/container/src/main/java/org/openremote/container/persistence/migrations/V0_1_2__ExampleMigration.java b/container/src/main/java/org/openremote/container/persistence/migrations/V0_1_2__ExampleMigration.javanew file mode 100644index 000000000..0cec579ab--- /dev/null+++ b/container/src/main/java/org/openremote/container/persistence/migrations/V0_1_2__ExampleMigration.java@@ -0,0 +1,39 @@+/*+ * Copyright 2017, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.container.persistence.migrations;++import org.flywaydb.core.api.migration.jdbc.JdbcMigration;++import java.sql.Connection;+import java.sql.PreparedStatement;++public class V0_1_2__ExampleMigration implements JdbcMigration {+ @Override+ public void migrate(Connection connection) throws Exception {+ PreparedStatement statement =+ connection.prepareStatement("ALTER TABLE public.asset ADD COLUMN test_column varchar(50)");++ try {+ statement.execute();+ } finally {+ statement.close();+ }+ }+}diff --git a/gradle.properties b/gradle.propertiesindex 2da0ba167..5b93dc66a 100644--- a/gradle.properties+++ b/gradle.properties@@ -35,4 +35,5 @@ uuidGeneratorVersion = 3.1.3nrjavaserialVersion = 3.13.0saxonHEVersion = 9.8.0-3jmxtermVersion = 1.0.0-nettyVersion = 4.1.16.Final\ No newline at end of file+nettyVersion = 4.1.16.Final+flywaydbVersion = 4.2.0\ No newline at end of filediff --git a/manager/server/src/main/java/org/openremote/manager/server/setup/builtin/ManagerUpdateSetup.java b/manager/server/src/main/java/org/openremote/manager/server/setup/builtin/ManagerUpdateSetup.javanew file mode 100644index 000000000..08c769827--- /dev/null+++ b/manager/server/src/main/java/org/openremote/manager/server/setup/builtin/ManagerUpdateSetup.java@@ -0,0 +1,40 @@+/*+ * Copyright 2017, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.manager.server.setup.builtin;++import org.openremote.container.Container;+import org.openremote.manager.server.setup.AbstractManagerSetup;++import java.util.logging.Logger;++public class ManagerUpdateSetup extends AbstractManagerSetup {++ private static final Logger LOG = Logger.getLogger(ManagerUpdateSetup.class.getName());++ public ManagerUpdateSetup(Container container) {+ super(container);+ }++ @Override+ public void execute() throws Exception {+ LOG.info("Updating database schema");+ persistenceService.updateSchema();+ }+}diff --git a/profile/demo.yml b/profile/demo.ymlindex 1934329ea..5efacf3f0 100644--- a/profile/demo.yml+++ b/profile/demo.yml@@ -103,6 +103,7 @@ services:DATABASE_MIN_POOL_SIZE: 5DATABASE_MAX_POOL_SIZE: 20DATABASE_CONNECTION_TIMEOUT_SECONDS: 300+ #DATABASE_BASELINE: '1'# The service-internal host name and port of the identity provider.KEYCLOAK_HOST: keycloakdiff --git a/profile/demo_rpi.yml b/profile/demo_rpi.ymlindex 7f2ac2e1b..dd876d4e3 100644--- a/profile/demo_rpi.yml+++ b/profile/demo_rpi.yml@@ -80,6 +80,7 @@ services:DATABASE_MIN_POOL_SIZE: 5DATABASE_MAX_POOL_SIZE: 20DATABASE_CONNECTION_TIMEOUT_SECONDS: 300+ #DATABASE_BASELINE: '1'# The service-internal host name and port of the identity provider.KEYCLOAK_HOST: keycloakdiff --git a/profile/manager_dev.yml b/profile/manager_dev.ymlindex 6512b1113..8fb88caec 100644--- a/profile/manager_dev.yml+++ b/profile/manager_dev.yml@@ -18,6 +18,8 @@ services:KEYCLOAK_HOST: keycloakKEYCLOAK_PORT: 8080DATABASE_CONNECTION_URL: jdbc:postgresql://postgresql:5432/openremote+ DATABASE_BASELINE: '1'+links:- keycloak_dev:keycloak- postgresql_dev:postgresql