NIFIREG-429 Detect if older flyway table is already present in database, if so then set Flyway config to keep using that
Signed-off-by: Pierre Villard <pierre.villard.fr@gmail.com>
This closes #311.
diff --git a/nifi-registry-core/nifi-registry-framework/src/main/java/org/apache/nifi/registry/db/CustomFlywayConfiguration.java b/nifi-registry-core/nifi-registry-framework/src/main/java/org/apache/nifi/registry/db/CustomFlywayConfiguration.java
index 0288f9d..7f9ef7a 100644
--- a/nifi-registry-core/nifi-registry-framework/src/main/java/org/apache/nifi/registry/db/CustomFlywayConfiguration.java
+++ b/nifi-registry-core/nifi-registry-framework/src/main/java/org/apache/nifi/registry/db/CustomFlywayConfiguration.java
@@ -19,6 +19,7 @@
import org.flywaydb.core.api.FlywayException;
import org.flywaydb.core.api.configuration.FluentConfiguration;
import org.flywaydb.core.internal.jdbc.DatabaseType;
+import org.flywaydb.core.internal.jdbc.JdbcUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.flyway.FlywayConfigurationCustomizer;
@@ -26,6 +27,8 @@
import javax.sql.DataSource;
import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
import java.sql.SQLException;
@Configuration
@@ -44,6 +47,8 @@
private static final String LOCATION_POSTGRES = "classpath:db/migration/postgres";
private static final String[] LOCATIONS_POSTGRES = {LOCATION_COMMON, LOCATION_POSTGRES};
+ private static final String LEGACY_FLYWAY_SCHEMA_TABLE = "schema_version";
+
@Override
public void customize(final FluentConfiguration configuration) {
final DatabaseType databaseType = getDatabaseType(configuration.getDataSource());
@@ -63,6 +68,16 @@
configuration.locations(LOCATIONS_DEFAULT);
break;
}
+
+ // At some point Flyway changed their default table name: https://github.com/flyway/flyway/issues/1848
+ // So we need to determine if we are upgrading from an existing nifi registry that is using the older
+ // name, and if so then continue using that name, otherwise use the new default name
+ if (isLegacyFlywaySchemaTable(configuration.getDataSource())) {
+ LOGGER.info("Using legacy Flyway configuration table - {}", LEGACY_FLYWAY_SCHEMA_TABLE);
+ configuration.table(LEGACY_FLYWAY_SCHEMA_TABLE);
+ } else {
+ LOGGER.info("Using default Flyway configuration table");
+ }
}
/**
@@ -79,4 +94,31 @@
throw new FlywayException("Unable to obtain connection from Flyway DataSource", e);
}
}
+
+ /**
+ * Determines if the legacy flyway schema table exists.
+ *
+ * @param dataSource the data source
+ * @return true if the legacy schema tables exists, false otherwise
+ */
+ private boolean isLegacyFlywaySchemaTable(final DataSource dataSource) {
+ try (final Connection connection = dataSource.getConnection()) {
+ final DatabaseMetaData databaseMetaData = JdbcUtils.getDatabaseMetaData(connection);
+
+ try (final ResultSet resultSet = databaseMetaData.getTables(null, null, null, null)) {
+ while (resultSet.next()) {
+ final String table = resultSet.getString(3);
+ LOGGER.trace("Found table {}", table);
+ if (LEGACY_FLYWAY_SCHEMA_TABLE.equals(table)) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ } catch (SQLException e) {
+ LOGGER.error(e.getMessage(), e);
+ throw new FlywayException("Unable to obtain connection from Flyway DataSource", e);
+ }
+ }
}
diff --git a/pom.xml b/pom.xml
index 1f271ee..bf653b5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -99,8 +99,8 @@
<jackson.version>2.10.3</jackson.version>
<spring.boot.version>2.2.6.RELEASE</spring.boot.version>
<spring.security.version>5.1.9.RELEASE</spring.security.version>
- <flyway.version>6.0.8</flyway.version>
- <flyway.tests.version>6.0.0</flyway.tests.version>
+ <flyway.version>6.5.7</flyway.version>
+ <flyway.tests.version>6.4.0</flyway.tests.version>
<swagger.ui.version>3.12.0</swagger.ui.version>
<testcontainers.version>1.11.2</testcontainers.version>
<h2.version>1.4.199</h2.version>