diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/Default.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/Default.kt index 539ef07384..30d5606eab 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/Default.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/Default.kt @@ -2,6 +2,7 @@ package org.jetbrains.exposed.sql.vendors import org.jetbrains.exposed.exceptions.throwUnsupportedException import org.jetbrains.exposed.sql.* +import org.jetbrains.exposed.sql.Function import org.jetbrains.exposed.sql.transactions.TransactionManager import java.nio.ByteBuffer import java.util.* @@ -109,6 +110,7 @@ abstract class DataTypeProvider { /** Returns the SQL representation of the specified expression, for it to be used as a column default value. */ open fun processForDefaultValue(e: Expression<*>): String = when { e is LiteralOp<*> -> "$e" + e is Function<*> -> "$e" currentDialect is MysqlDialect -> "$e" currentDialect is SQLServerDialect -> "$e" else -> "($e)" diff --git a/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DefaultsTest.kt b/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DefaultsTest.kt index 2829911dc4..334ff3b8d9 100644 --- a/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DefaultsTest.kt +++ b/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DefaultsTest.kt @@ -359,4 +359,23 @@ class DefaultsTest : DatabaseTestsBase() { assertEquals(1, count) } } + + @Test + fun testConsistentSchemeWithFunctionAsDefaultExpression() { + val foo = object : IntIdTable("foo") { + val name = text("name") + val defaultDateTime = datetime("defaultDateTime").defaultExpression(CurrentDateTime) + } + + withDb { + try { + SchemaUtils.create(foo) + + val actual = SchemaUtils.statementsRequiredToActualizeScheme(foo) + assertTrue(actual.isEmpty()) + } finally { + SchemaUtils.drop(foo) + } + } + } } 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 9f0cef0e02..10c0519a38 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 @@ -1,4 +1,5 @@ @file:OptIn(ExperimentalTime::class) + package org.jetbrains.exposed.sql.kotlin.datetime import kotlinx.datetime.* @@ -24,6 +25,7 @@ import org.jetbrains.exposed.sql.vendors.OracleDialect import org.jetbrains.exposed.sql.vendors.SQLServerDialect import org.jetbrains.exposed.sql.vendors.h2Mode import org.junit.Test +import org.junit.runners.model.MultipleFailureException import kotlin.test.assertEquals import kotlin.test.assertNotNull import kotlin.test.assertTrue @@ -230,6 +232,7 @@ class DefaultsTest : DatabaseTestsBase() { fun Expression<*>.itOrNull() = when { currentDialectTest.isAllowedAsColumnDefault(this) -> "DEFAULT ${currentDialectTest.dataTypeProvider.processForDefaultValue(this)} NOT NULL" + else -> "NULL" } @@ -352,9 +355,9 @@ class DefaultsTest : DatabaseTestsBase() { withTables(foo) { val d2020 = LocalDate(2020, 1, 1) - val dt2020 = d2020.atTime(0,0,0) - val dt2020m1w = d2020.minus(DateTimeUnit.WEEK).atTime(0,0,0) - val dt2020p1w = d2020.plus(DateTimeUnit.WEEK).atTime(0,0,0) + val dt2020 = d2020.atTime(0, 0, 0) + val dt2020m1w = d2020.minus(DateTimeUnit.WEEK).atTime(0, 0, 0) + val dt2020p1w = d2020.plus(DateTimeUnit.WEEK).atTime(0, 0, 0) foo.insert { it[dt] = LocalDateTime(2019, 1, 1, 1, 1) } foo.insert { it[dt] = dt2020 } @@ -363,4 +366,23 @@ class DefaultsTest : DatabaseTestsBase() { assertEquals(1, count) } } + + @Test + fun testConsistentSchemeWithFunctionAsDefaultExpression() { + val foo = object : IntIdTable("foo") { + val name = text("name") + val defaultDateTime = datetime("defaultDateTime").defaultExpression(CurrentDateTime) + } + + withDb { + try { + SchemaUtils.create(foo) + + val actual = SchemaUtils.statementsRequiredToActualizeScheme(foo) + assertTrue(actual.isEmpty()) + } finally { + SchemaUtils.drop(foo) + } + } + } }