From be94481e3349cc1bf81be50e3833d2128d3c66a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Mon, 21 Dec 2015 16:06:11 +0100 Subject: [PATCH] HHH-10191 test case --- .../SchemaUpdateWithFunctionIndexTest.java | 137 ++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaUpdateWithFunctionIndexTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaUpdateWithFunctionIndexTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaUpdateWithFunctionIndexTest.java new file mode 100644 index 0000000000..a560beff8c --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaUpdateWithFunctionIndexTest.java @@ -0,0 +1,137 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.schemaupdate; + +import javax.persistence.Basic; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Index; +import javax.persistence.Table; + +import org.hibernate.Query; +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.boot.spi.MetadataImplementor; +import org.hibernate.cfg.Environment; +import org.hibernate.dialect.PostgreSQL81Dialect; +import org.hibernate.service.ServiceRegistry; +import org.hibernate.tool.hbm2ddl.SchemaExport; +import org.hibernate.tool.hbm2ddl.SchemaUpdate; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.hibernate.testing.RequiresDialect; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; + +/** + * @author Yoann Rodiere + */ +@TestForIssue(jiraKey = "HHH-10191") +@RequiresDialect(PostgreSQL81Dialect.class) +public class SchemaUpdateWithFunctionIndexTest extends BaseNonConfigCoreFunctionalTestCase { + protected ServiceRegistry serviceRegistry; + protected MetadataImplementor metadata; + + @Test + public void testUpdateSchema() { + SchemaUpdate schemaUpdate = new SchemaUpdate( serviceRegistry, metadata ); + schemaUpdate.execute( true, true ); + } + + @Before + public void setUp() { + createTable(); + createFunctionIndex(); + serviceRegistry = new StandardServiceRegistryBuilder() + .applySetting( Environment.GLOBALLY_QUOTED_IDENTIFIERS, "false" ) + .applySetting( Environment.DEFAULT_SCHEMA, "public" ) + .build(); + metadata = (MetadataImplementor) new MetadataSources( serviceRegistry ) + .addAnnotatedClass( MyEntity.class ) + .buildMetadata(); + } + + private void createTable() { + Session session = openSession(); + Transaction transaction = session.beginTransaction(); + Query query = session.createSQLQuery( "CREATE TABLE MyEntity(id bigint, name varchar(255));" ); + query.executeUpdate(); + transaction.commit(); + session.close(); + } + + private void createFunctionIndex() { + Session session = openSession(); + Transaction transaction = session.beginTransaction(); + Query query = session.createSQLQuery( "CREATE UNIQUE INDEX uk_MyEntity_name_lowercase ON MyEntity (lower(name));" ); + query.executeUpdate(); + transaction.commit(); + session.close(); + } + + @After + public void tearDown() { + dropFunctionIndex(); + System.out.println( "********* Starting SchemaExport (drop) for TEAR-DOWN *************************" ); + SchemaExport schemaExport = new SchemaExport( serviceRegistry, metadata ); + schemaExport.drop( true, true ); + System.out.println( "********* Completed SchemaExport (drop) for TEAR-DOWN *************************" ); + + StandardServiceRegistryBuilder.destroy( serviceRegistry ); + serviceRegistry = null; + } + + private void dropFunctionIndex() { + Session session = openSession(); + Transaction transaction = session.beginTransaction(); + Query query = session.createSQLQuery( "DROP INDEX IF EXISTS uk_MyEntity_name_lowercase;" ); + query.executeUpdate(); + transaction.commit(); + session.close(); + } + + @Entity + @Table(name = "MyEntity", indexes = @Index(columnList = "otherInfo")) + public static class MyEntity { + + private int id; + + private String name; + + private int otherInfo; + + @Id + public int getId() { + return this.id; + } + + public void setId(final int id) { + this.id = id; + } + + @Basic + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Basic + public int getOtherInfo() { + return otherInfo; + } + + public void setOtherInfo(int otherInfo) { + this.otherInfo = otherInfo; + } + } +}