HHH-15665 - Fix and added test for issue

Signed-off-by: Jan Schatteman <jschatte@redhat.com>
This commit is contained in:
Jan Schatteman 2023-02-02 16:22:53 +01:00 committed by Christian Beikov
parent 2ee4c96852
commit 86b720fb1f
2 changed files with 97 additions and 2 deletions

View File

@ -12,6 +12,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.QualifiedSequenceName;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;
import org.hibernate.tool.schema.extract.spi.ExtractionContext;
@ -28,7 +29,7 @@ public class SequenceInformationExtractorMariaDBDatabaseImpl extends SequenceInf
// SQL to get metadata from individual sequence
private static final String SQL_SEQUENCE_QUERY =
"SELECT '%1$s' as sequence_name, minimum_value, maximum_value, start_value, increment, cache_size FROM %1$s ";
"SELECT '%1$s' as sequence_name, minimum_value, maximum_value, start_value, increment, cache_size FROM %2$s ";
private static final String UNION_ALL =
"UNION ALL ";
@ -56,7 +57,7 @@ public class SequenceInformationExtractorMariaDBDatabaseImpl extends SequenceInf
if ( sequenceInfoQueryBuilder.length() > 0 ) {
sequenceInfoQueryBuilder.append( UNION_ALL );
}
sequenceInfoQueryBuilder.append( String.format( SQL_SEQUENCE_QUERY, sequenceName ) );
sequenceInfoQueryBuilder.append( String.format( SQL_SEQUENCE_QUERY, sequenceName, Identifier.toIdentifier( sequenceName ) ) );
}
return extractionContext.getQueryResults(
sequenceInfoQueryBuilder.toString(),

View File

@ -0,0 +1,94 @@
package org.hibernate.orm.test.dialect.functional;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
import java.util.Optional;
import java.util.stream.StreamSupport;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.MariaDBDialect;
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorMariaDBDatabaseImpl;
import org.hibernate.tool.schema.extract.spi.ExtractionContext;
import org.hibernate.tool.schema.extract.spi.SequenceInformation;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.TestForIssue;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInAutoCommit;
/**
* @author Jan Schatteman
*/
@RequiresDialect(value = MariaDBDialect.class)
public class MariaDBExtractSequenceInformationTest {
private final static String hhh15665SeqName = "HHH-15665-seq";
private final static Map<String, Object> settings = Map.ofEntries(
Map.entry( AvailableSettings.URL, Environment.getProperties().getProperty( AvailableSettings.URL ) ),
Map.entry( AvailableSettings.USER, Environment.getProperties().getProperty( AvailableSettings.USER ) ),
Map.entry( AvailableSettings.PASS, Environment.getProperties().getProperty( AvailableSettings.PASS ) )
);
@BeforeAll
public static void setUp() throws Exception {
doInAutoCommit( settings, "CREATE SEQUENCE IF NOT EXISTS `" + hhh15665SeqName + "`" );
}
@AfterAll
public static void tearDown() throws SQLException {
doInAutoCommit( settings, "DROP SEQUENCE IF EXISTS `" + hhh15665SeqName + "`" );
}
@Test
@TestForIssue( jiraKey = "HHH-15665" )
public void testExtractSequenceInformationForSqlServerWithCaseSensitiveCollation() {
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySettings( settings ).build();
JdbcEnvironment jdbcEnvironment = ssr.getService( JdbcEnvironment.class );
JdbcConnectionAccess bootstrapJdbcConnectionAccess = ssr.getService( JdbcServices.class ).getBootstrapJdbcConnectionAccess();
try ( Connection connection = bootstrapJdbcConnectionAccess.obtainConnection() ) {
Iterable<SequenceInformation> sequenceInformations = SequenceInformationExtractorMariaDBDatabaseImpl.INSTANCE.extractMetadata(
new ExtractionContext.EmptyExtractionContext() {
@Override
public Connection getJdbcConnection() {
return connection;
}
@Override
public JdbcEnvironment getJdbcEnvironment() {
return jdbcEnvironment;
}
} );
Assertions.assertNotNull( sequenceInformations );
Optional<SequenceInformation> seq = StreamSupport.stream( sequenceInformations.spliterator(), false )
.filter(
sequence -> hhh15665SeqName.equals( sequence.getSequenceName()
.getSequenceName()
.getText() )
)
.findFirst();
Assertions.assertTrue( seq.isPresent(), hhh15665SeqName + " not found" );
}
catch ( SQLException e ) {
Assertions.fail( "Sequence information for " + hhh15665SeqName + " was not retrieved: " + e.getMessage() );
}
finally {
StandardServiceRegistryBuilder.destroy( ssr );
}
}
}