Skip to content

Commit 1007b7e

Browse files
committed
Add postgres support to datetime column type
1 parent e54ce64 commit 1007b7e

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ kotlin.stdlib.default.dependency=false
33
org.gradle.parallel=true
44
#org.gradle.caching=true
55
#org.gradle.configureondemand=true
6-
version=2.0.2
6+
version=2.0.3

src/main/kotlin/dev/slne/surf/database/columns/time/UtcInstantDateTimeColumnType.kt

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,24 @@ open class CurrentTimestampBase<T>(
167167
private val includeUpdate: Boolean = false
168168
) : Function<T>(columnType) {
169169
override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder {
170-
+when {
171-
(currentDialect as? MysqlDialect)?.isFractionDateTimeSupported() == true -> "CURRENT_TIMESTAMP(6) ${
172-
if (includeUpdate) "ON UPDATE CURRENT_TIMESTAMP(6)" else ""
173-
}"
174-
else -> "CURRENT_TIMESTAMP ${if (includeUpdate) "ON UPDATE CURRENT_TIMESTAMP" else ""}"
170+
+when (val dialect = currentDialect) {
171+
// Postgres' CURRENT_TIMESTAMP is a `timestamp with time zone`. The column is stored as
172+
// `timestamp without time zone` holding UTC wall-clock values (see notNullValueToDB), so
173+
// convert to UTC to keep the DB default consistent with application-written instants.
174+
// Postgres has no inline `ON UPDATE` (that requires a trigger), so includeUpdate is not
175+
// expressible here and is intentionally ignored to keep the generated DDL valid.
176+
is PostgreSQLDialect ->
177+
"(CURRENT_TIMESTAMP AT TIME ZONE 'UTC')"
178+
179+
else -> {
180+
val current =
181+
if ((dialect as? MysqlDialect)?.isFractionDateTimeSupported() == true)
182+
"CURRENT_TIMESTAMP(6)"
183+
else
184+
"CURRENT_TIMESTAMP"
185+
186+
if (includeUpdate) "$current ON UPDATE $current" else current
187+
}
175188
}
176189
}
177190
}

0 commit comments

Comments
 (0)