|
| 1 | +package org.hibernate.orm.test.schemaupdate; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.nio.file.Files; |
| 5 | +import java.util.EnumSet; |
| 6 | + |
| 7 | +import org.hibernate.boot.MetadataSources; |
| 8 | +import org.hibernate.boot.registry.StandardServiceRegistry; |
| 9 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 10 | +import org.hibernate.boot.spi.MetadataImplementor; |
| 11 | +import org.hibernate.dialect.MySQLDialect; |
| 12 | +import org.hibernate.tool.hbm2ddl.SchemaExport; |
| 13 | +import org.hibernate.tool.schema.TargetType; |
| 14 | + |
| 15 | +import org.hibernate.testing.orm.junit.BaseUnitTest; |
| 16 | +import org.hibernate.testing.orm.junit.Jira; |
| 17 | +import org.hibernate.testing.orm.junit.RequiresDialect; |
| 18 | +import org.hibernate.testing.util.ServiceRegistryUtil; |
| 19 | +import org.junit.jupiter.api.AfterEach; |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import jakarta.persistence.Entity; |
| 24 | +import jakarta.persistence.Id; |
| 25 | +import jakarta.persistence.Lob; |
| 26 | +import jakarta.persistence.Table; |
| 27 | + |
| 28 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 29 | + |
| 30 | +@Jira("HHH-17097") |
| 31 | +@BaseUnitTest |
| 32 | +@RequiresDialect(MySQLDialect.class) |
| 33 | +public class MySQLLobSchemaCreationTest { |
| 34 | + private File output; |
| 35 | + private StandardServiceRegistry ssr; |
| 36 | + private MetadataImplementor metadata; |
| 37 | + |
| 38 | + @BeforeEach |
| 39 | + public void setUp() throws Exception { |
| 40 | + output = File.createTempFile( "update_script", ".sql" ); |
| 41 | + output.deleteOnExit(); |
| 42 | + ssr = ServiceRegistryUtil.serviceRegistry(); |
| 43 | + } |
| 44 | + |
| 45 | + @AfterEach |
| 46 | + public void tearsDown() { |
| 47 | + output.delete(); |
| 48 | + dropSchema( TestEntity.class ); |
| 49 | + StandardServiceRegistryBuilder.destroy( ssr ); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testSchemaCreation() throws Exception { |
| 54 | + createSchema( TestEntity.class ); |
| 55 | + String fileContent = new String( Files.readAllBytes( output.toPath() ) ).toLowerCase() |
| 56 | + .replace( System.lineSeparator(), "" ); |
| 57 | + assertThat( fileContent ).contains( "lobfield longtext" ); |
| 58 | + } |
| 59 | + |
| 60 | + private void createSchema(Class... annotatedClasses) { |
| 61 | + final MetadataSources metadataSources = new MetadataSources( ssr ); |
| 62 | + |
| 63 | + for ( Class c : annotatedClasses ) { |
| 64 | + metadataSources.addAnnotatedClass( c ); |
| 65 | + } |
| 66 | + metadata = (MetadataImplementor) metadataSources.buildMetadata(); |
| 67 | + metadata.orderColumns( false ); |
| 68 | + metadata.validate(); |
| 69 | + new SchemaExport() |
| 70 | + .setHaltOnError( true ) |
| 71 | + .setOutputFile( output.getAbsolutePath() ) |
| 72 | + .setFormat( false ) |
| 73 | + .create( EnumSet.of( TargetType.SCRIPT, TargetType.DATABASE ), metadata ); |
| 74 | + } |
| 75 | + |
| 76 | + private void dropSchema(Class... annotatedClasses) { |
| 77 | + final MetadataSources metadataSources = new MetadataSources( ssr ); |
| 78 | + |
| 79 | + for ( Class c : annotatedClasses ) { |
| 80 | + metadataSources.addAnnotatedClass( c ); |
| 81 | + } |
| 82 | + metadata = (MetadataImplementor) metadataSources.buildMetadata(); |
| 83 | + metadata.orderColumns( false ); |
| 84 | + metadata.validate(); |
| 85 | + new SchemaExport() |
| 86 | + .setHaltOnError( false ) |
| 87 | + .setFormat( false ) |
| 88 | + .drop( EnumSet.of( TargetType.DATABASE ), metadata ); |
| 89 | + } |
| 90 | + |
| 91 | + @Entity |
| 92 | + @Table(name = "TestEntity") |
| 93 | + public static class TestEntity { |
| 94 | + |
| 95 | + @Id |
| 96 | + private int id; |
| 97 | + |
| 98 | + @Lob |
| 99 | + private String lobField; |
| 100 | + |
| 101 | + } |
| 102 | +} |
0 commit comments