From 05bf44a41b0907098276b9077b28a98d7943dc53 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Mon, 20 May 2024 14:18:54 +0200 Subject: [PATCH] HHH-18140 schema migration for @Column(length=LONG) on MySQL and similar cases where the column type doesn't have explicit length Signed-off-by: Gavin King --- .../schema/internal/ColumnDefinitions.java | 59 +++++++++++-------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/ColumnDefinitions.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/ColumnDefinitions.java index 065c717d8c..8f0a6ab559 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/ColumnDefinitions.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/ColumnDefinitions.java @@ -47,31 +47,38 @@ class ColumnDefinitions { } static boolean hasMatchingLength(Column column, ColumnInformation columnInformation, Metadata metadata, Dialect dialect) { - int sqlType = columnInformation.getTypeCode(); - if ( isStringType( sqlType ) ) { - final int actualLength = columnInformation.getColumnSize(); - final Size size = column.getColumnSize( dialect, metadata ); - final Long requiredLength = size.getLength(); - return requiredLength == null - || requiredLength == actualLength; - } - else if ( isNumericOrDecimal( sqlType ) ) { - // Postgres, H2, SQL Server, and MySQL agree on the following: - final int actualPrecision = columnInformation.getColumnSize(); - final int actualScale = columnInformation.getDecimalDigits(); - final Size size = column.getColumnSize( dialect, metadata ); - final Integer requiredPrecision = size.getPrecision(); - final Integer requiredScale = size.getScale(); - return requiredPrecision == null - || requiredScale == null - || requiredScale == actualScale && requiredPrecision == actualPrecision; - } - // I would really love this to be able to change the binary - // precision of a float/double type, but there simply doesn't - // seem to be any good way to implement it - else { + if ( !column.getSqlType( metadata ).contains("(") ) { + // the DDL type does not explicitly specify a length, + // and so we do not require an exact match return true; } + else { + int sqlType = columnInformation.getTypeCode(); + if ( isStringType( sqlType ) ) { + final int actualLength = columnInformation.getColumnSize(); + final Size size = column.getColumnSize( dialect, metadata ); + final Long requiredLength = size.getLength(); + return requiredLength == null + || requiredLength == actualLength; + } + else if ( isNumericOrDecimal( sqlType ) ) { + // Postgres, H2, SQL Server, and MySQL agree on the following: + final int actualPrecision = columnInformation.getColumnSize(); + final int actualScale = columnInformation.getDecimalDigits(); + final Size size = column.getColumnSize( dialect, metadata ); + final Integer requiredPrecision = size.getPrecision(); + final Integer requiredScale = size.getScale(); + return requiredPrecision == null + || requiredScale == null + || requiredScale == actualScale && requiredPrecision == actualPrecision; + } + // I would really love this to be able to change the binary + // precision of a float/double type, but there simply doesn't + // seem to be any good way to implement it + else { + return true; + } + } } static String getFullColumnDeclaration( @@ -216,8 +223,8 @@ class ColumnDefinitions { return null; } else { - final String lowerCaseTypName = typeName.toLowerCase(Locale.ROOT); - switch (lowerCaseTypName) { + final String lowercaseTypeName = typeName.toLowerCase(Locale.ROOT); + switch (lowercaseTypeName) { case "int": return "integer"; case "character": @@ -233,7 +240,7 @@ class ColumnDefinitions { case "interval second": return "interval"; default: - return lowerCaseTypName; + return lowercaseTypeName; } } }