FINERACT-2099: Initial Liquibase got executed every execution
This commit is contained in:
committed by
Adam Saghy
parent
f17a0965db
commit
112787cf07
+1
-4
@@ -18,8 +18,6 @@
|
||||
*/
|
||||
package org.apache.fineract.infrastructure.core.service.database;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
import java.util.List;
|
||||
import javax.sql.DataSource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -45,8 +43,7 @@ public class MySQLQueryService implements DatabaseQueryService {
|
||||
@Override
|
||||
public boolean isTablePresent(DataSource dataSource, String tableName) {
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
SqlRowSet rs = jdbcTemplate.queryForRowSet(format("SHOW TABLES LIKE '%s'", tableName));
|
||||
return rs.next();
|
||||
return jdbcTemplate.queryForRowSet("SHOW TABLES LIKE ?", tableName).next();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+7
-9
@@ -18,8 +18,6 @@
|
||||
*/
|
||||
package org.apache.fineract.infrastructure.core.service.database;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import javax.sql.DataSource;
|
||||
@@ -46,8 +44,9 @@ public class PostgreSQLQueryService implements DatabaseQueryService {
|
||||
@Override
|
||||
public boolean isTablePresent(DataSource dataSource, String tableName) {
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
Integer result = jdbcTemplate.queryForObject(format("SELECT COUNT(table_name) " + "FROM information_schema.tables "
|
||||
+ "WHERE table_schema = 'public' " + "AND table_name = '%s';", tableName), Integer.class);
|
||||
Integer result = jdbcTemplate.queryForObject(
|
||||
"SELECT COUNT(table_name) FROM information_schema.tables " + "WHERE table_schema = 'public' AND table_name = ?",
|
||||
Integer.class, tableName);
|
||||
return Objects.equals(result, 1);
|
||||
}
|
||||
|
||||
@@ -56,9 +55,8 @@ public class PostgreSQLQueryService implements DatabaseQueryService {
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
String sql = "SELECT column_name, is_nullable, data_type,"
|
||||
+ " coalesce(character_maximum_length, numeric_precision, datetime_precision) AS max_length, ordinal_position = 1 AS column_key"
|
||||
+ " FROM information_schema.columns WHERE table_catalog = current_catalog AND table_schema = current_schema AND table_name = '"
|
||||
+ tableName + "' ORDER BY ordinal_position";
|
||||
final SqlRowSet columnDefinitions = jdbcTemplate.queryForRowSet(sql); // NOSONAR
|
||||
+ " FROM information_schema.columns WHERE table_catalog = current_catalog AND table_schema = current_schema AND table_name = ? ORDER BY ordinal_position";
|
||||
final SqlRowSet columnDefinitions = jdbcTemplate.queryForRowSet(sql, tableName); // NOSONAR
|
||||
if (columnDefinitions.next()) {
|
||||
return columnDefinitions;
|
||||
} else {
|
||||
@@ -69,8 +67,8 @@ public class PostgreSQLQueryService implements DatabaseQueryService {
|
||||
@Override
|
||||
public List<IndexDetail> getTableIndexes(DataSource dataSource, String tableName) {
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
String sql = "SELECT indexname FROM pg_indexes WHERE schemaname = 'public' AND tablename = '" + tableName + "'";
|
||||
final SqlRowSet indexDefinitions = jdbcTemplate.queryForRowSet(sql); // NOSONAR
|
||||
String sql = "SELECT indexname FROM pg_indexes WHERE schemaname = 'public' AND tablename = ?";
|
||||
final SqlRowSet indexDefinitions = jdbcTemplate.queryForRowSet(sql, tableName); // NOSONAR
|
||||
if (indexDefinitions.next()) {
|
||||
return DatabaseIndexMapper.getIndexDetails(indexDefinitions);
|
||||
} else {
|
||||
|
||||
+6
-4
@@ -23,6 +23,7 @@ import java.util.Objects;
|
||||
import javax.sql.DataSource;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.fineract.infrastructure.core.service.database.DatabaseIndependentQueryService;
|
||||
import org.apache.fineract.infrastructure.core.service.database.DatabaseTypeResolver;
|
||||
import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -40,10 +41,11 @@ public class TenantDatabaseStateVerifier {
|
||||
|
||||
private final LiquibaseProperties liquibaseProperties;
|
||||
private final DatabaseIndependentQueryService dbQueryService;
|
||||
private final DatabaseTypeResolver databaseTypeResolver;
|
||||
|
||||
public boolean isFirstLiquibaseMigration(DataSource dataSource) {
|
||||
boolean databaseChangelogTableExists = dbQueryService.isTablePresent(dataSource, "DATABASECHANGELOG");
|
||||
return !databaseChangelogTableExists;
|
||||
String tableName = "DATABASECHANGELOG";
|
||||
return !dbQueryService.isTablePresent(dataSource, databaseTypeResolver.isPostgreSQL() ? tableName.toLowerCase() : tableName);
|
||||
}
|
||||
|
||||
public boolean isFlywayPresent(DataSource dataSource) {
|
||||
@@ -70,8 +72,8 @@ public class TenantDatabaseStateVerifier {
|
||||
|
||||
NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
|
||||
Integer result = jdbcTemplate.queryForObject(
|
||||
"SELECT COUNT(script) FROM schema_version " + "WHERE version = :latestFlywayVersion "
|
||||
+ "AND script = :latestFlywayScriptName " + "AND checksum = :latestFlywayScriptChecksum " + "AND success = 1",
|
||||
"SELECT COUNT(script) FROM schema_version WHERE version = :latestFlywayVersion "
|
||||
+ "AND script = :latestFlywayScriptName AND checksum = :latestFlywayScriptChecksum AND success = 1",
|
||||
paramMap, Integer.class);
|
||||
return Objects.equals(result, 1);
|
||||
}
|
||||
|
||||
@@ -143,8 +143,8 @@ public class TestConfiguration {
|
||||
|
||||
@Bean
|
||||
public TenantDatabaseStateVerifier tenantDatabaseStateVerifier(DatabaseIndependentQueryService databaseIndependentQueryService,
|
||||
LiquibaseProperties liquibaseProperties) {
|
||||
return new TenantDatabaseStateVerifier(liquibaseProperties, databaseIndependentQueryService);
|
||||
LiquibaseProperties liquibaseProperties, DatabaseTypeResolver databaseTypeResolver) {
|
||||
return new TenantDatabaseStateVerifier(liquibaseProperties, databaseIndependentQueryService, databaseTypeResolver);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user