HHH-16762 SchemaMigrator generates ORA-22859 errors on columns annotated with @Lob

This commit is contained in:
Andrea Boriero 2023-08-04 12:26:36 +02:00 committed by Andrea Boriero
parent db602cb535
commit adc962957d
1 changed files with 126 additions and 0 deletions

View File

@ -0,0 +1,126 @@
package org.hibernate.orm.test.schemaupdate;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.time.LocalDateTime;
import java.util.EnumSet;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
import org.hibernate.tool.schema.TargetType;
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Lob;
import jakarta.persistence.Table;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@JiraKey("HHH-16762")
public class LobSchemaUpdateTest {
private File output;
private StandardServiceRegistry ssr;
private MetadataImplementor metadata;
@BeforeEach
public void setUp() throws IOException {
output = File.createTempFile( "update_script", ".sql" );
output.deleteOnExit();
ssr = new StandardServiceRegistryBuilder().build();
}
@AfterEach
public void tearsDown() {
dropDatabase( TestEntity.class );
output.delete();
StandardServiceRegistryBuilder.destroy( ssr );
}
@Test
public void testUpdateIsNotExecuted() throws Exception {
createSchema( TestEntity.class );
updateSchema( TestEntity.class );
String fileContent = new String( Files.readAllBytes( output.toPath() ) ).toLowerCase()
.replace( System.lineSeparator(), "" );
assertThat( fileContent ).isEmpty();
}
private void createSchema(Class... annotatedClasses) {
final MetadataSources metadataSources = new MetadataSources( ssr );
for ( Class c : annotatedClasses ) {
metadataSources.addAnnotatedClass( c );
}
metadata = (MetadataImplementor) metadataSources.buildMetadata();
metadata.orderColumns( false );
metadata.validate();
new SchemaExport()
.setHaltOnError( false )
.setFormat( false )
.create( EnumSet.of( TargetType.DATABASE ), metadata );
}
private void updateSchema(Class... annotatedClasses) {
final MetadataSources metadataSources = new MetadataSources( ssr );
for ( Class c : annotatedClasses ) {
metadataSources.addAnnotatedClass( c );
}
metadata = (MetadataImplementor) metadataSources.buildMetadata();
metadata.orderColumns( false );
metadata.validate();
new SchemaUpdate()
.setHaltOnError( true )
.setOutputFile( output.getAbsolutePath() )
.setFormat( false )
.execute( EnumSet.of( TargetType.SCRIPT, TargetType.DATABASE ), metadata );
}
private void dropDatabase(Class... annotatedClasses){
final MetadataSources metadataSources = new MetadataSources( ssr );
for ( Class c : annotatedClasses ) {
metadataSources.addAnnotatedClass( c );
}
metadata = (MetadataImplementor) metadataSources.buildMetadata();
metadata.orderColumns( false );
metadata.validate();
new SchemaExport()
.setHaltOnError( false )
.setFormat( false )
.drop( EnumSet.of( TargetType.DATABASE ), metadata );
}
@Entity(name = "TestEntity")
public class TestEntity {
@Id
@GeneratedValue
protected int id;
@Lob
protected String clobField;
@Lob
protected byte[] blobField;
public TestEntity() {}
}
}