|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.schemaupdate; |
| 6 | + |
| 7 | +import jakarta.persistence.Column; |
| 8 | +import jakarta.persistence.Entity; |
| 9 | +import jakarta.persistence.Id; |
| 10 | +import jakarta.persistence.Table; |
| 11 | +import org.hibernate.boot.MetadataSources; |
| 12 | +import org.hibernate.boot.registry.StandardServiceRegistry; |
| 13 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 14 | +import org.hibernate.boot.spi.MetadataImplementor; |
| 15 | +import org.hibernate.dialect.H2Dialect; |
| 16 | +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
| 17 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 18 | +import org.hibernate.testing.orm.junit.RequiresDialect; |
| 19 | +import org.hibernate.testing.transaction.TransactionUtil; |
| 20 | +import org.hibernate.testing.util.ServiceRegistryUtil; |
| 21 | +import org.hibernate.tool.hbm2ddl.SchemaUpdate; |
| 22 | +import org.hibernate.tool.schema.TargetType; |
| 23 | +import org.junit.After; |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +import java.io.File; |
| 28 | +import java.io.IOException; |
| 29 | +import java.math.BigDecimal; |
| 30 | +import java.nio.file.Files; |
| 31 | +import java.util.EnumSet; |
| 32 | + |
| 33 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 34 | + |
| 35 | +/** |
| 36 | + * @author Yanming Zhou |
| 37 | + */ |
| 38 | +@JiraKey("HHH-18784") |
| 39 | +@RequiresDialect(H2Dialect.class) |
| 40 | +public class ColumnLengthTest extends BaseCoreFunctionalTestCase { |
| 41 | + |
| 42 | + private File output; |
| 43 | + |
| 44 | + @Override |
| 45 | + protected Class<?>[] getAnnotatedClasses() { |
| 46 | + return new Class<?>[] { TestEntity.class }; |
| 47 | + } |
| 48 | + |
| 49 | + @Before |
| 50 | + public void setUp() throws IOException { |
| 51 | + output = File.createTempFile( "update_script", ".sql" ); |
| 52 | + output.deleteOnExit(); |
| 53 | + TransactionUtil.doInHibernate( this::sessionFactory, session -> { |
| 54 | + session.createNativeQuery( "DROP TABLE IF EXISTS en CASCADE" ).executeUpdate(); |
| 55 | + session.createNativeQuery( |
| 56 | + "CREATE TABLE en ( " + |
| 57 | + " `ID` integer NOT NULL, " + |
| 58 | + " `TEXT` varchar(1000), " + |
| 59 | + " `DECIMAL` numeric(12, 4), " + |
| 60 | + " PRIMARY KEY (`ID`)" + |
| 61 | + ")" ) |
| 62 | + .executeUpdate(); |
| 63 | + } ); |
| 64 | + } |
| 65 | + |
| 66 | + @After |
| 67 | + public void tearDown() { |
| 68 | + TransactionUtil.doInHibernate( this::sessionFactory, session -> { |
| 69 | + session.createNativeQuery( "DROP TABLE en CASCADE" ).executeUpdate(); |
| 70 | + } ); |
| 71 | + output.delete(); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testUpdateIsNotExecuted() throws Exception { |
| 76 | + StandardServiceRegistry ssr = ServiceRegistryUtil.serviceRegistryBuilder() |
| 77 | + .build(); |
| 78 | + try { |
| 79 | + final MetadataSources metadataSources = new MetadataSources( ssr ); |
| 80 | + metadataSources.addAnnotatedClass( TestEntity.class ); |
| 81 | + MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata(); |
| 82 | + metadata.orderColumns( false ); |
| 83 | + new SchemaUpdate() |
| 84 | + .setHaltOnError( true ) |
| 85 | + .setOutputFile( output.getAbsolutePath() ) |
| 86 | + .setFormat( false ) |
| 87 | + .execute( EnumSet.of( TargetType.SCRIPT, TargetType.DATABASE ), metadata ); |
| 88 | + |
| 89 | + String fileContent = new String( Files.readAllBytes( output.toPath() ) ); |
| 90 | + assertThat( fileContent ).isEmpty(); |
| 91 | + } |
| 92 | + finally { |
| 93 | + StandardServiceRegistryBuilder.destroy( ssr ); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + @Entity(name = "en") |
| 99 | + @Table(name = "en") |
| 100 | + public static class TestEntity { |
| 101 | + @Id |
| 102 | + private Integer id; |
| 103 | + |
| 104 | + private String text; |
| 105 | + |
| 106 | + @Column( precision = 8, scale = 2 ) |
| 107 | + private BigDecimal decimal; |
| 108 | + } |
| 109 | +} |
0 commit comments