Skip to content

Commit

Permalink
MID-8842 ninja related - sqale repository/audit now validates schema …
Browse files Browse the repository at this point in the history
…version during start
  • Loading branch information
1azyman committed Jul 4, 2023
1 parent 2fed5fb commit a1f0a9b
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 4 deletions.
2 changes: 2 additions & 0 deletions config/sql/native-new/postgres-new-audit.sql
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,6 @@ limit 50;

-- Initializing the last change number used in postgres-new-upgrade.sql.
-- This is important to avoid applying any change more than once.
-- Also update SqaleUtils.CURRENT_SCHEMA_AUDIT_CHANGE_NUMBER
-- repo/repo-sqale/src/main/java/com/evolveum/midpoint/repo/sqale/SqaleUtils.java
call apply_audit_change(4, $$ SELECT 1 $$, true);
2 changes: 2 additions & 0 deletions config/sql/native-new/postgres-new-upgrade-audit.sql
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,5 @@ $aa$);
-- WRITE CHANGES ABOVE ^^
-- IMPORTANT: update apply_audit_change number at the end of postgres-new-audit.sql
-- to match the number used in the last change here!
-- Also update SqaleUtils.CURRENT_SCHEMA_AUDIT_CHANGE_NUMBER
-- repo/repo-sqale/src/main/java/com/evolveum/midpoint/repo/sqale/SqaleUtils.java
2 changes: 2 additions & 0 deletions config/sql/native-new/postgres-new-upgrade.sql
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,5 @@ $aa$);
-- WRITE CHANGES ABOVE ^^
-- IMPORTANT: update apply_change number at the end of postgres-new.sql
-- to match the number used in the last change here!
-- Also update SqaleUtils.CURRENT_SCHEMA_CHANGE_NUMBER
-- repo/repo-sqale/src/main/java/com/evolveum/midpoint/repo/sqale/SqaleUtils.java
2 changes: 2 additions & 0 deletions config/sql/native-new/postgres-new.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2085,4 +2085,6 @@ END $$;

-- Initializing the last change number used in postgres-new-upgrade.sql.
-- This is important to avoid applying any change more than once.
-- Also update SqaleUtils.CURRENT_SCHEMA_CHANGE_NUMBER
-- repo/repo-sqale/src/main/java/com/evolveum/midpoint/repo/sqale/SqaleUtils.java
call apply_change(15, $$ SELECT 1 $$, true);
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
import java.lang.reflect.Array;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Objects;
import java.util.Set;

import com.evolveum.midpoint.repo.sqale.qmodel.common.MGlobalMetadata;
import com.evolveum.midpoint.repo.sqale.qmodel.common.QGlobalMetadata;
import com.evolveum.midpoint.repo.sqlbase.JdbcSession;

import jakarta.annotation.PostConstruct;
import javax.sql.DataSource;
import javax.xml.namespace.QName;
Expand Down Expand Up @@ -51,6 +57,10 @@
*/
public class SqaleRepoContext extends SqlRepoContext {

private final String schemaChangeNumberLabel;

private final int schemaChangeNumberValue;

private final UriCache uriCache;
private final ExtItemCache extItemCache;

Expand All @@ -60,9 +70,12 @@ public SqaleRepoContext(
JdbcRepositoryConfiguration jdbcRepositoryConfiguration,
DataSource dataSource,
SchemaService schemaService,
QueryModelMappingRegistry mappingRegistry) {
QueryModelMappingRegistry mappingRegistry, String schemaChangeNumberLabel, int schemaChangeNumberValue) {
super(jdbcRepositoryConfiguration, dataSource, schemaService, mappingRegistry);

this.schemaChangeNumberLabel = schemaChangeNumberLabel;
this.schemaChangeNumberValue = schemaChangeNumberValue;

// each enum type must be registered if we want to map it as objects (to PG enum types)
querydslConfig.register(new EnumAsObjectType<>(AccessCertificationCampaignStateType.class));
querydslConfig.register(new EnumAsObjectType<>(ActivationStatusType.class));
Expand Down Expand Up @@ -104,8 +117,27 @@ public SqaleRepoContext(
extItemCache = new ExtItemCache();
}

// This has nothing to do with "repo cache" which is higher than this.
@PostConstruct
public void initialize() {
try (JdbcSession session = this.newJdbcSession().startReadOnlyTransaction()) {
MGlobalMetadata metadata = session.newQuery().from(QGlobalMetadata.DEFAULT)
.select(QGlobalMetadata.DEFAULT)
.where(QGlobalMetadata.DEFAULT.name.eq(schemaChangeNumberLabel))
.limit(1)
.fetchOne();
String current = metadata != null ? metadata.value : null;

if (!Objects.equals(current, schemaChangeNumberValue)) {
throw new SystemException("Can't initialize sqale repository context, database schema version (" + current
+ ") doesn't match expected value (" + schemaChangeNumberValue + ") for label '" + schemaChangeNumberLabel
+ "'. Seems like mismatch between midPoint executable version and DB schema version. Maybe DB schema was not updated?");
}
}

clearCaches();
}

// This has nothing to do with "repo cache" which is higher than this.
public void clearCaches() {
uriCache.initialize(this::newJdbcSession);
extItemCache.initialize(this::newJdbcSession);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ public SqaleRepoContext sqlRepoContext(
DataSource dataSource) {
QueryModelMappingRegistry mappingRegistry = new QueryModelMappingRegistry();
SqaleRepoContext repositoryContext = new SqaleRepoContext(
repositoryConfiguration, dataSource, schemaService, mappingRegistry);
repositoryConfiguration, dataSource, schemaService, mappingRegistry,
SqaleUtils.SCHEMA_CHANGE_NUMBER, SqaleUtils.CURRENT_SCHEMA_CHANGE_NUMBER);

// logger on com.evolveum.midpoint.repo.sqlbase.querydsl.SqlLogger
// DEBUG = show query, TRACE = add parameter values too (bindings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@

public class SqaleUtils {

/**
* Global metadata name for schema change number, related to
*/
public static final String SCHEMA_CHANGE_NUMBER = "schemaChangeNumber";

/**
* Global metadata name for schema audit change number
*/
public static final String SCHEMA_AUDIT_CHANGE_NUMBER = "schemaAuditChangeNumber";

public static final int CURRENT_SCHEMA_CHANGE_NUMBER = 15;

public static final int CURRENT_SCHEMA_AUDIT_CHANGE_NUMBER = 4;

/**
* Returns version from midPoint object as a number.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.util.List;
import javax.sql.DataSource;

import com.evolveum.midpoint.repo.sqale.SqaleUtils;

import com.google.common.base.Strings;
import com.querydsl.sql.ColumnMetadata;
import org.apache.commons.configuration2.BaseHierarchicalConfiguration;
Expand Down Expand Up @@ -115,7 +117,8 @@ private SqaleRepoContext createSqaleRepoContext(
SchemaService schemaService) {
QueryModelMappingRegistry mappingRegistry = new QueryModelMappingRegistry();
SqaleRepoContext repositoryContext =
new SqaleRepoContext(config, dataSource, schemaService, mappingRegistry);
new SqaleRepoContext(config, dataSource, schemaService, mappingRegistry,
SqaleUtils.SCHEMA_AUDIT_CHANGE_NUMBER, SqaleUtils.CURRENT_SCHEMA_AUDIT_CHANGE_NUMBER);
repositoryContext.setQuerydslSqlListener(new SqlLogger(config.getSqlDurationWarningMs()));

// Registered mapping needs repository context which needs registry - now we have both:
Expand Down

0 comments on commit a1f0a9b

Please sign in to comment.