From 6e4e2fc24d344c79dfb2be9b2edc9670eed96d39 Mon Sep 17 00:00:00 2001 From: Maxr1998 Date: Thu, 1 Dec 2022 16:15:14 +0100 Subject: [PATCH 1/3] Remove unnecessary parentheses for functions --- .../src/main/kotlin/org/jetbrains/exposed/sql/vendors/Default.kt | 1 + 1 file changed, 1 insertion(+) 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..cf6569dc16 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 @@ -109,6 +109,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)" From 3b4ebf3ed9aaff4e04dcb2474e4fe0917f1c7807 Mon Sep 17 00:00:00 2001 From: Maxr1998 Date: Wed, 3 May 2023 16:51:57 +0200 Subject: [PATCH 2/3] Add regression tests for consistent scheme with default functions --- .../org/jetbrains/exposed/DefaultsTest.kt | 19 +++++++++++++ .../sql/kotlin/datetime/DefaultsTest.kt | 28 +++++++++++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) 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) + } + } + } } From e10d4fd113fd87319a3fd62e1e0027735b5a6797 Mon Sep 17 00:00:00 2001 From: Maxr1998 Date: Wed, 3 May 2023 17:00:12 +0200 Subject: [PATCH 3/3] Fix broken when entry due to missing import --- .../src/main/kotlin/org/jetbrains/exposed/sql/vendors/Default.kt | 1 + 1 file changed, 1 insertion(+) 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 cf6569dc16..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.*