Skip to content

Commit

Permalink
fix: EXPOSED-737 Timestamp column broken for MySQL 8.0
Browse files Browse the repository at this point in the history
In this [PR](#2386), I modified the notNullValueToDB function to return the formatted String for MySQL (excluding MariaDB) because there was an issue with MySQL 5 where it implicitly converted the value to the session time zone when storing it and lead to an unexpected behaviour when the value is retrieved. This should have been done for MySQL 5 only and not for MySQL 8.0. This commit fixes that.
  • Loading branch information
joc-a committed Feb 18, 2025
1 parent 4421c59 commit 470039e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.ColumnType
import org.jetbrains.exposed.sql.IDateColumnType
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.vendors.*
import java.sql.ResultSet
import java.time.*
Expand Down Expand Up @@ -377,12 +378,13 @@ class JavaInstantColumnType : ColumnType<Instant>(), IDateColumnType {
return rs.getTimestamp(index)
}

@Suppress("MagicNumber")
override fun notNullValueToDB(value: Instant): Any {
val dialect = currentDialect
return when {
dialect is SQLiteDialect ->
SQLITE_AND_ORACLE_TIMESTAMP_STRING_FORMATTER.format(value)
dialect is MysqlDialect && dialect !is MariaDBDialect -> {
dialect is MysqlDialect && dialect !is MariaDBDialect && !TransactionManager.current().db.isVersionCovers(8, 0) -> {
val formatter = if (dialect.isFractionDateTimeSupported()) MYSQL_FRACTION_TIMESTAMP_STRING_FORMATTER else MYSQL_TIMESTAMP_STRING_FORMATTER
formatter.format(value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.ColumnType
import org.jetbrains.exposed.sql.IDateColumnType
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.vendors.*
import java.sql.ResultSet
import java.time.OffsetDateTime
Expand Down Expand Up @@ -393,12 +394,13 @@ class KotlinInstantColumnType : ColumnType<Instant>(), IDateColumnType {
return rs.getTimestamp(index)
}

@Suppress("MagicNumber")
override fun notNullValueToDB(value: Instant): Any {
val dialect = currentDialect
return when {
dialect is SQLiteDialect ->
SQLITE_AND_ORACLE_TIMESTAMP_STRING_FORMATTER.format(value.toJavaInstant())
dialect is MysqlDialect && dialect !is MariaDBDialect -> {
dialect is MysqlDialect && dialect !is MariaDBDialect && !TransactionManager.current().db.isVersionCovers(8, 0) -> {
val formatter = if (dialect.isFractionDateTimeSupported()) MYSQL_FRACTION_TIMESTAMP_STRING_FORMATTER else MYSQL_TIMESTAMP_STRING_FORMATTER
formatter.format(value.toJavaInstant())
}
Expand Down

0 comments on commit 470039e

Please sign in to comment.