Skip to content

Commit

Permalink
fix: EXPOSED-256 Date default falsely triggers ALTER statement
Browse files Browse the repository at this point in the history
-`KotlinLocalDateColumnType` now overrides `nonNullValueAsDefaultString` to match the default value obtained from the metadata for PostgreSQL.
  • Loading branch information
joc-a committed Feb 5, 2024
1 parent cace8a1 commit e1c6868
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions exposed-kotlin-datetime/api/exposed-kotlin-datetime.api
Original file line number Diff line number Diff line change
Expand Up @@ -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 <init> ()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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e1c6868

Please sign in to comment.