From e1c68680f15da8d49df51822ffbac1d2f0c4c32a Mon Sep 17 00:00:00 2001 From: Jocelyne Date: Thu, 1 Feb 2024 13:19:06 +0100 Subject: [PATCH] fix: EXPOSED-256 Date default falsely triggers ALTER statement -`KotlinLocalDateColumnType` now overrides `nonNullValueAsDefaultString` to match the default value obtained from the metadata for PostgreSQL. --- .../api/exposed-kotlin-datetime.api | 1 + .../sql/kotlin/datetime/KotlinDateColumnType.kt | 5 +++++ .../exposed/sql/kotlin/datetime/DefaultsTest.kt | 14 ++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/exposed-kotlin-datetime/api/exposed-kotlin-datetime.api b/exposed-kotlin-datetime/api/exposed-kotlin-datetime.api index 7294f5aced..a97c4641c1 100644 --- a/exposed-kotlin-datetime/api/exposed-kotlin-datetime.api +++ b/exposed-kotlin-datetime/api/exposed-kotlin-datetime.api @@ -122,6 +122,7 @@ public final class org/jetbrains/exposed/sql/kotlin/datetime/KotlinLocalDateColu public static final field Companion Lorg/jetbrains/exposed/sql/kotlin/datetime/KotlinLocalDateColumnType$Companion; public fun ()V public fun getHasTimePart ()Z + public fun nonNullValueAsDefaultString (Ljava/lang/Object;)Ljava/lang/String; public fun nonNullValueToString (Ljava/lang/Object;)Ljava/lang/String; public fun notNullValueToDB (Ljava/lang/Object;)Ljava/lang/Object; public fun sqlType ()Ljava/lang/String; diff --git a/exposed-kotlin-datetime/src/main/kotlin/org/jetbrains/exposed/sql/kotlin/datetime/KotlinDateColumnType.kt b/exposed-kotlin-datetime/src/main/kotlin/org/jetbrains/exposed/sql/kotlin/datetime/KotlinDateColumnType.kt index 561f6a45b2..5ab588ea89 100644 --- a/exposed-kotlin-datetime/src/main/kotlin/org/jetbrains/exposed/sql/kotlin/datetime/KotlinDateColumnType.kt +++ b/exposed-kotlin-datetime/src/main/kotlin/org/jetbrains/exposed/sql/kotlin/datetime/KotlinDateColumnType.kt @@ -139,6 +139,11 @@ class KotlinLocalDateColumnType : ColumnType(), IDateColumnType { else -> value } + override fun nonNullValueAsDefaultString(value: Any): String = when (currentDialect) { + is PostgreSQLDialect -> "${nonNullValueToString(value)}::date" + else -> super.nonNullValueAsDefaultString(value) + } + private fun longToLocalDate(instant: Long) = Instant.fromEpochMilliseconds(instant).toLocalDateTime(DEFAULT_TIME_ZONE).date companion object { diff --git a/exposed-kotlin-datetime/src/test/kotlin/org/jetbrains/exposed/sql/kotlin/datetime/DefaultsTest.kt b/exposed-kotlin-datetime/src/test/kotlin/org/jetbrains/exposed/sql/kotlin/datetime/DefaultsTest.kt index 469b953026..507567b538 100644 --- a/exposed-kotlin-datetime/src/test/kotlin/org/jetbrains/exposed/sql/kotlin/datetime/DefaultsTest.kt +++ b/exposed-kotlin-datetime/src/test/kotlin/org/jetbrains/exposed/sql/kotlin/datetime/DefaultsTest.kt @@ -501,6 +501,20 @@ class DefaultsTest : DatabaseTestsBase() { } } + @Test + fun testDateDefaultDoesNotTriggerAlterStatement() { + val date = LocalDate(2024, 2, 1) + + val tester = object : Table("tester") { + val dateWithDefault = date("dateWithDefault").default(date) + } + + withTables(tester) { + val statements = SchemaUtils.addMissingColumnsStatements(tester) + assertEquals(0, statements.size) + } + } + @Test fun testTimestampDefaultDoesNotTriggerAlterStatement() { val instant = Clock.System.now() // In UTC