FINERACT-2099: Initial Liquibase got executed every execution

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