From c6c3e07d113b21cbbac51c4f14eebbeb70aeec2f Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Sat, 4 Mar 2023 09:24:26 +0000 Subject: [PATCH] HHH-16252 SQL syntax error on drop constraint --- .../unique/AlterTableUniqueDelegate.java | 13 ++- .../dialect/PostgreSQLDialectTestCase.java | 92 +++++++++++++++++++ .../tableperclass/SchemaCreationTest.java | 4 +- .../UniqueConstraintDropTest.java | 6 +- 4 files changed, 107 insertions(+), 8 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/unique/AlterTableUniqueDelegate.java b/hibernate-core/src/main/java/org/hibernate/dialect/unique/AlterTableUniqueDelegate.java index bbd29fe7e4..821f7f77cf 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/unique/AlterTableUniqueDelegate.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/unique/AlterTableUniqueDelegate.java @@ -76,14 +76,21 @@ public class AlterTableUniqueDelegate implements UniqueDelegate { SqlStringGenerationContext context) { final String tableName = context.format( uniqueKey.getTable().getQualifiedTableName() ); final StringBuilder command = new StringBuilder( dialect.getAlterTableString(tableName) ); + command.append( ' ' ); command.append( dialect.getDropUniqueKeyString() ); if ( dialect.supportsIfExistsBeforeConstraintName() ) { - command.append( "if exists " ); + command.append( " if exists " ); + command.append( dialect.quote( uniqueKey.getName() ) ); } - command.append( dialect.quote( uniqueKey.getName() ) ); - if ( dialect.supportsIfExistsAfterConstraintName() ) { + else if ( dialect.supportsIfExistsAfterConstraintName() ) { + command.append( ' ' ); + command.append( dialect.quote( uniqueKey.getName() ) ); command.append( " if exists" ); } + else { + command.append( ' ' ); + command.append( dialect.quote( uniqueKey.getName() ) ); + } return command.toString(); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/dialect/PostgreSQLDialectTestCase.java b/hibernate-core/src/test/java/org/hibernate/orm/test/dialect/PostgreSQLDialectTestCase.java index ec09836199..9a32b9dfe7 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/dialect/PostgreSQLDialectTestCase.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/dialect/PostgreSQLDialectTestCase.java @@ -14,10 +14,20 @@ import org.hibernate.JDBCException; import org.hibernate.LockMode; import org.hibernate.LockOptions; import org.hibernate.PessimisticLockException; +import org.hibernate.boot.model.naming.Identifier; +import org.hibernate.boot.model.relational.QualifiedName; +import org.hibernate.boot.model.relational.QualifiedSequenceName; +import org.hibernate.boot.model.relational.QualifiedTableName; +import org.hibernate.boot.model.relational.SqlStringGenerationContext; +import org.hibernate.dialect.Dialect; import org.hibernate.dialect.PostgreSQLDialect; import org.hibernate.QueryTimeoutException; +import org.hibernate.dialect.unique.AlterTableUniqueDelegate; +import org.hibernate.engine.jdbc.env.spi.IdentifierHelper; import org.hibernate.exception.LockAcquisitionException; import org.hibernate.exception.spi.SQLExceptionConversionDelegate; +import org.hibernate.mapping.Table; +import org.hibernate.mapping.UniqueKey; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseUnitTestCase; @@ -124,4 +134,86 @@ public class PostgreSQLDialectTestCase extends BaseUnitTestCase { assertEquals("alter table if exists table_name", dialect.getAlterTableString( "table_name" )); } + + @Test + @TestForIssue( jiraKey = "HHH-16252" ) + public void testAlterTableDropConstraintString() { + PostgreSQLDialect dialect = new PostgreSQLDialect(); + AlterTableUniqueDelegate alterTable = new AlterTableUniqueDelegate( dialect ); + final Table table = new Table( "orm", "table_name" ); + final UniqueKey uniqueKey = new UniqueKey(); + uniqueKey.setName( "unique_something" ); + uniqueKey.setTable( table ); + final String sql = alterTable.getAlterTableToDropUniqueKeyCommand( + uniqueKey, + null, + new MockSqlStringGenerationContext() + ); + + assertEquals("alter table if exists table_name drop constraint if exists unique_something", sql ); + } + + private static class MockSqlStringGenerationContext implements SqlStringGenerationContext { + + @Override + public Dialect getDialect() { + return null; + } + + @Override + public IdentifierHelper getIdentifierHelper() { + return null; + } + + @Override + public Identifier toIdentifier(String text) { + return null; + } + + @Override + public Identifier getDefaultCatalog() { + return null; + } + + @Override + public Identifier catalogWithDefault(Identifier explicitCatalogOrNull) { + return null; + } + + @Override + public Identifier getDefaultSchema() { + return null; + } + + @Override + public Identifier schemaWithDefault(Identifier explicitSchemaOrNull) { + return null; + } + + @Override + public String format(QualifiedTableName qualifiedName) { + return qualifiedName.getTableName().render(); + } + + @Override + public String format(QualifiedSequenceName qualifiedName) { + return null; + } + + @Override + public String format(QualifiedName qualifiedName) { + return null; + } + + @Override + public String formatWithoutCatalog(QualifiedSequenceName qualifiedName) { + return null; + } + + @Override + public boolean isMigration() { + return false; + } + } + } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/inheritance/tableperclass/SchemaCreationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/inheritance/tableperclass/SchemaCreationTest.java index a8a8d0d9ca..09056cd01e 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/inheritance/tableperclass/SchemaCreationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/inheritance/tableperclass/SchemaCreationTest.java @@ -88,9 +88,9 @@ public class SchemaCreationTest { && statement.contains("category (code)") || statement.startsWith("create unique nonclustered index") && statement.contains("category (code)") - || statement.startsWith("alter table if exists category add constraint") + || statement.startsWith("alter table if exists category add constraint ") && statement.contains("unique (code)") - || statement.startsWith("alter table category add constraint") + || statement.startsWith("alter table category add constraint ") && statement.contains("unique (code)") || statement.startsWith("create table category") && statement.contains("code " + varchar255 + " not null unique") diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/uniqueconstraint/UniqueConstraintDropTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/uniqueconstraint/UniqueConstraintDropTest.java index 3c92799121..593bdb4880 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/uniqueconstraint/UniqueConstraintDropTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/uniqueconstraint/UniqueConstraintDropTest.java @@ -139,11 +139,11 @@ public class UniqueConstraintDropTest { } private boolean checkDropConstraint(String tableName, String columnName) throws IOException { - String regex = getDialect().getAlterTableString( tableName ) + getDialect().getDropUniqueKeyString(); + String regex = getDialect().getAlterTableString( tableName ) + ' ' + getDialect().getDropUniqueKeyString(); if ( getDialect().supportsIfExistsBeforeConstraintName() ) { - regex += "if exists "; + regex += " if exists"; } - regex += "uk_.*"; + regex += " uk_.*"; if ( getDialect().supportsIfExistsAfterConstraintName() ) { regex += " if exists"; }