HHH-17691 - Avoid unnecessary sequence queries when hdb2ddl will recreate them anyway

Signed-off-by: Jan Schatteman <jschatte@redhat.com>
This commit is contained in:
Jan Schatteman 2024-02-09 23:58:00 +01:00 committed by Jan Schatteman
parent 4ac91f793f
commit 0f51feb69e
6 changed files with 44 additions and 6 deletions

View File

@ -6,8 +6,11 @@
*/
package org.hibernate.id.enhanced;
import java.util.Collections;
import java.util.Iterator;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
@ -30,7 +33,9 @@ import org.hibernate.id.SequenceMismatchStrategy;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.schema.Action;
import org.hibernate.tool.schema.extract.spi.SequenceInformation;
import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.ActionGrouping;
import org.hibernate.type.Type;
import org.jboss.logging.Logger;
@ -210,7 +215,8 @@ public class SequenceStyleGenerator
incrementSize,
physicalSequence,
optimizationStrategy,
serviceRegistry
serviceRegistry,
determineContributor( parameters )
);
if ( physicalSequence
@ -244,7 +250,8 @@ public class SequenceStyleGenerator
int incrementSize,
boolean physicalSequence,
OptimizerDescriptor optimizationStrategy,
ServiceRegistry serviceRegistry) {
ServiceRegistry serviceRegistry,
String contributor) {
final ConfigurationService configurationService = serviceRegistry.requireService( ConfigurationService.class );
final SequenceMismatchStrategy sequenceMismatchStrategy = configurationService.getSetting(
AvailableSettings.SEQUENCE_INCREMENT_SIZE_MISMATCH_STRATEGY,
@ -256,7 +263,7 @@ public class SequenceStyleGenerator
&& optimizationStrategy.isPooled()
&& physicalSequence ) {
final String databaseSequenceName = sequenceName.getObjectName().getText();
final Number databaseIncrementValue = getSequenceIncrementValue( jdbcEnvironment, databaseSequenceName );
final Number databaseIncrementValue = isSchemaToBeRecreated( contributor, configurationService ) ? null : getSequenceIncrementValue( jdbcEnvironment, databaseSequenceName );
if ( databaseIncrementValue != null && databaseIncrementValue.intValue() != incrementSize) {
final int dbIncrementValue = databaseIncrementValue.intValue();
switch ( sequenceMismatchStrategy ) {
@ -280,6 +287,14 @@ public class SequenceStyleGenerator
return determineAdjustedIncrementSize( optimizationStrategy, incrementSize );
}
private boolean isSchemaToBeRecreated(String contributor, ConfigurationService configurationService) {
final Set<ActionGrouping> actions = ActionGrouping.interpret( Collections.singleton(contributor), configurationService.getSettings() );
// We know this will only contain at most 1 action
final Iterator<ActionGrouping> it = actions.iterator();
final Action dbAction = it.hasNext() ? it.next().getDatabaseAction() : null;
return dbAction == Action.CREATE || dbAction == Action.CREATE_DROP;
}
@Override
public void registerExportables(Database database) {
databaseStructure.registerExportables( database );

View File

@ -727,19 +727,18 @@ public class SchemaManagementToolCoordinator {
return Action.interpretHbm2ddlSetting( scriptsActionSetting );
}
public static Set<ActionGrouping> interpret(Metadata metadata, Map<?,?> configurationValues) {
public static Set<ActionGrouping> interpret(Set<String> contributors, Map<?,?> configurationValues) {
// these represent the base (non-contributor-specific) values
final Action rootDatabaseAction = determineJpaDbActionSetting( configurationValues, null, null );
final Action rootScriptAction = determineJpaScriptActionSetting( configurationValues, null, null );
final Action rootAutoAction = determineAutoSettingImpliedAction( configurationValues, null, null );
final Set<String> contributors = metadata.getContributors();
final Set<ActionGrouping> groupings = new HashSet<>( contributors.size() );
// for each contributor, look for specific tooling config values
for ( String contributor : contributors ) {
Action databaseActionToUse = determineJpaDbActionSetting( configurationValues, contributor, rootDatabaseAction );;
Action databaseActionToUse = determineJpaDbActionSetting( configurationValues, contributor, rootDatabaseAction );
Action scriptActionToUse = determineJpaScriptActionSetting( configurationValues, contributor, rootScriptAction );
if ( databaseActionToUse == null && scriptActionToUse == null ) {
@ -769,6 +768,10 @@ public class SchemaManagementToolCoordinator {
return groupings;
}
public static Set<ActionGrouping> interpret(Metadata metadata, Map<?,?> configurationValues) {
return interpret( metadata.getContributors(), configurationValues );
}
@Override
public boolean equals(Object o) {
if ( this == o ) {

View File

@ -107,6 +107,11 @@ public class PostgreSQLSequenceGeneratorWithSerialTest extends EntityManagerFact
settings.put( AvailableSettings.SEQUENCE_INCREMENT_SIZE_MISMATCH_STRATEGY, SequenceMismatchStrategy.FIX );
}
@Override
protected boolean exportSchema() {
return false;
}
@Override
protected void entityManagerFactoryBuilt(EntityManagerFactory factory) {
assertTrue( triggerable.wasTriggered() );

View File

@ -81,6 +81,11 @@ public class SequenceMismatchStrategyDefaultExceptionTest extends EntityManagerF
options.put( AvailableSettings.HBM2DDL_AUTO, "none" );
}
@Override
protected boolean exportSchema() {
return false;
}
@Test
public void test() {
produceEntityManagerFactory();

View File

@ -103,6 +103,11 @@ public class SequenceMismatchStrategyFixWithSequenceGeneratorTest extends Entity
assertTrue( triggerable.wasTriggered() );
}
@Override
protected boolean exportSchema() {
return false;
}
@Test
public void test() {

View File

@ -102,6 +102,11 @@ public class SequenceMismatchStrategyLogTest extends EntityManagerFactoryBasedFu
assertTrue( triggerable.wasTriggered() );
}
@Override
protected boolean exportSchema() {
return false;
}
@Test
public void test() {
produceEntityManagerFactory();