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; package org.hibernate.id.enhanced;
import java.util.Collections;
import java.util.Iterator;
import java.util.Objects; import java.util.Objects;
import java.util.Properties; import java.util.Properties;
import java.util.Set;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.MappingException; import org.hibernate.MappingException;
@ -30,7 +33,9 @@ import org.hibernate.id.SequenceMismatchStrategy;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.schema.Action;
import org.hibernate.tool.schema.extract.spi.SequenceInformation; import org.hibernate.tool.schema.extract.spi.SequenceInformation;
import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.ActionGrouping;
import org.hibernate.type.Type; import org.hibernate.type.Type;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
@ -210,7 +215,8 @@ public class SequenceStyleGenerator
incrementSize, incrementSize,
physicalSequence, physicalSequence,
optimizationStrategy, optimizationStrategy,
serviceRegistry serviceRegistry,
determineContributor( parameters )
); );
if ( physicalSequence if ( physicalSequence
@ -244,7 +250,8 @@ public class SequenceStyleGenerator
int incrementSize, int incrementSize,
boolean physicalSequence, boolean physicalSequence,
OptimizerDescriptor optimizationStrategy, OptimizerDescriptor optimizationStrategy,
ServiceRegistry serviceRegistry) { ServiceRegistry serviceRegistry,
String contributor) {
final ConfigurationService configurationService = serviceRegistry.requireService( ConfigurationService.class ); final ConfigurationService configurationService = serviceRegistry.requireService( ConfigurationService.class );
final SequenceMismatchStrategy sequenceMismatchStrategy = configurationService.getSetting( final SequenceMismatchStrategy sequenceMismatchStrategy = configurationService.getSetting(
AvailableSettings.SEQUENCE_INCREMENT_SIZE_MISMATCH_STRATEGY, AvailableSettings.SEQUENCE_INCREMENT_SIZE_MISMATCH_STRATEGY,
@ -256,7 +263,7 @@ public class SequenceStyleGenerator
&& optimizationStrategy.isPooled() && optimizationStrategy.isPooled()
&& physicalSequence ) { && physicalSequence ) {
final String databaseSequenceName = sequenceName.getObjectName().getText(); 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) { if ( databaseIncrementValue != null && databaseIncrementValue.intValue() != incrementSize) {
final int dbIncrementValue = databaseIncrementValue.intValue(); final int dbIncrementValue = databaseIncrementValue.intValue();
switch ( sequenceMismatchStrategy ) { switch ( sequenceMismatchStrategy ) {
@ -280,6 +287,14 @@ public class SequenceStyleGenerator
return determineAdjustedIncrementSize( optimizationStrategy, incrementSize ); 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 @Override
public void registerExportables(Database database) { public void registerExportables(Database database) {
databaseStructure.registerExportables( database ); databaseStructure.registerExportables( database );

View File

@ -727,19 +727,18 @@ public class SchemaManagementToolCoordinator {
return Action.interpretHbm2ddlSetting( scriptsActionSetting ); 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 // these represent the base (non-contributor-specific) values
final Action rootDatabaseAction = determineJpaDbActionSetting( configurationValues, null, null ); final Action rootDatabaseAction = determineJpaDbActionSetting( configurationValues, null, null );
final Action rootScriptAction = determineJpaScriptActionSetting( configurationValues, null, null ); final Action rootScriptAction = determineJpaScriptActionSetting( configurationValues, null, null );
final Action rootAutoAction = determineAutoSettingImpliedAction( configurationValues, null, null ); final Action rootAutoAction = determineAutoSettingImpliedAction( configurationValues, null, null );
final Set<String> contributors = metadata.getContributors();
final Set<ActionGrouping> groupings = new HashSet<>( contributors.size() ); final Set<ActionGrouping> groupings = new HashSet<>( contributors.size() );
// for each contributor, look for specific tooling config values // for each contributor, look for specific tooling config values
for ( String contributor : contributors ) { for ( String contributor : contributors ) {
Action databaseActionToUse = determineJpaDbActionSetting( configurationValues, contributor, rootDatabaseAction );; Action databaseActionToUse = determineJpaDbActionSetting( configurationValues, contributor, rootDatabaseAction );
Action scriptActionToUse = determineJpaScriptActionSetting( configurationValues, contributor, rootScriptAction ); Action scriptActionToUse = determineJpaScriptActionSetting( configurationValues, contributor, rootScriptAction );
if ( databaseActionToUse == null && scriptActionToUse == null ) { if ( databaseActionToUse == null && scriptActionToUse == null ) {
@ -769,6 +768,10 @@ public class SchemaManagementToolCoordinator {
return groupings; return groupings;
} }
public static Set<ActionGrouping> interpret(Metadata metadata, Map<?,?> configurationValues) {
return interpret( metadata.getContributors(), configurationValues );
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if ( this == 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 ); settings.put( AvailableSettings.SEQUENCE_INCREMENT_SIZE_MISMATCH_STRATEGY, SequenceMismatchStrategy.FIX );
} }
@Override
protected boolean exportSchema() {
return false;
}
@Override @Override
protected void entityManagerFactoryBuilt(EntityManagerFactory factory) { protected void entityManagerFactoryBuilt(EntityManagerFactory factory) {
assertTrue( triggerable.wasTriggered() ); assertTrue( triggerable.wasTriggered() );

View File

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

View File

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

View File

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