From 564a264776c57a46cab207a27c7726806ac486b7 Mon Sep 17 00:00:00 2001 From: Leonid Stashevsky Date: Thu, 31 Aug 2023 14:26:38 +0200 Subject: [PATCH] Reduce date time test time --- .../org/jetbrains/exposed/sql/SchemaUtils.kt | 80 ++++++++----------- .../jetbrains/exposed/JodaTimeDefaultsTest.kt | 2 +- .../exposed/sql/tests/DatabaseTestsBase.kt | 37 +++++---- 3 files changed, 56 insertions(+), 63 deletions(-) diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SchemaUtils.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SchemaUtils.kt index 6592423b7d..d726d2a441 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SchemaUtils.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SchemaUtils.kt @@ -158,32 +158,37 @@ object SchemaUtils { is PostgreSQLDialect -> value.toString() else -> booleanToStatementString(value) } + is String -> when { - dialect is PostgreSQLDialect -> - when (column.columnType) { - is VarCharColumnType -> "'$value'::character varying" - is TextColumnType -> "'$value'::text" - else -> processForDefaultValue(exp) - } - dialect is OracleDialect || dialect.h2Mode == H2Dialect.H2CompatibilityMode.Oracle -> - when { - column.columnType is VarCharColumnType && value == "" -> "NULL" - column.columnType is TextColumnType && value == "" -> "NULL" - else -> value - } + dialect is PostgreSQLDialect -> when (column.columnType) { + is VarCharColumnType -> "'$value'::character varying" + is TextColumnType -> "'$value'::text" + else -> processForDefaultValue(exp) + } + + dialect is OracleDialect || dialect.h2Mode == H2Dialect.H2CompatibilityMode.Oracle -> when { + column.columnType is VarCharColumnType && value == "" -> "NULL" + column.columnType is TextColumnType && value == "" -> "NULL" + else -> value + } + else -> value } + is Enum<*> -> when (exp.columnType) { is EnumerationNameColumnType<*> -> when (dialect) { is PostgreSQLDialect -> "'${value.name}'::character varying" else -> value.name } + else -> processForDefaultValue(exp) } + is BigDecimal -> when (dialect) { is MysqlDialect -> value.setScale((exp.columnType as DecimalColumnType).scale).toString() else -> processForDefaultValue(exp) } + else -> { if (column.columnType is JsonColumnMarker) { val processed = processForDefaultValue(exp) @@ -195,6 +200,7 @@ object SchemaUtils { processed } } + is MariaDBDialect -> processed.trim('\'') is MysqlDialect -> "_utf8mb4\\'${processed.trim('(', ')', '\'')}\\" else -> processed.trim('\'') @@ -205,12 +211,10 @@ object SchemaUtils { } } } + is Function<*> -> { var processed = processForDefaultValue(exp) - if ( - exp.columnType is IDateColumnType && - (processed.startsWith("CURRENT_TIMESTAMP") || processed == "GETDATE()") - ) { + if (exp.columnType is IDateColumnType && (processed.startsWith("CURRENT_TIMESTAMP") || processed == "GETDATE()")) { when (currentDialect) { is SQLServerDialect -> processed = "getdate" is MariaDBDialect -> processed = processed.lowercase() @@ -218,6 +222,7 @@ object SchemaUtils { } processed } + else -> processForDefaultValue(exp) } } @@ -250,26 +255,21 @@ object SchemaUtils { if (dbSupportsAlterTableWithAddColumn) { // create indexes with new columns - table.indices - .filter { index -> index.columns.any { missingTableColumns.contains(it) } } - .forEach { statements.addAll(createIndex(it)) } + table.indices.filter { index -> index.columns.any { missingTableColumns.contains(it) } }.forEach { statements.addAll(createIndex(it)) } // sync existing columns val dataTypeProvider = currentDialect.dataTypeProvider - val redoColumns = existingTableColumns - .mapValues { (col, existingCol) -> + val redoColumns = existingTableColumns.mapValues { (col, existingCol) -> val columnType = col.columnType val incorrectNullability = existingCol.nullable != columnType.nullable // Exposed doesn't support changing sequences on columns - val incorrectAutoInc = existingCol.autoIncrement != columnType.isAutoInc && - col.autoIncColumnType?.autoincSeq == null + val incorrectAutoInc = existingCol.autoIncrement != columnType.isAutoInc && col.autoIncColumnType?.autoincSeq == null val incorrectDefaults = existingCol.defaultDbValue != col.dbDefaultValue?.let { dataTypeProvider.dbDefaultToString(col, it) } val incorrectCaseSensitiveName = existingCol.name.inProperCase() != col.nameUnquoted().inProperCase() ColumnDiff(incorrectNullability, incorrectAutoInc, incorrectDefaults, incorrectCaseSensitiveName) - } - .filterValues { it.hasDifferences() } + }.filterValues { it.hasDifferences() } redoColumns.flatMapTo(statements) { (col, changedState) -> col.modifyStatements(changedState) } @@ -305,10 +305,7 @@ object SchemaUtils { for ((foreignKey, existingConstraint) in foreignKeyConstraints) { if (existingConstraint == null) { statements.addAll(createFKey(foreignKey)) - } else if (existingConstraint.targetTable != foreignKey.targetTable || - foreignKey.deleteRule != existingConstraint.deleteRule || - foreignKey.updateRule != existingConstraint.updateRule - ) { + } else if (existingConstraint.targetTable != foreignKey.targetTable || foreignKey.deleteRule != existingConstraint.deleteRule || foreignKey.updateRule != existingConstraint.updateRule) { statements.addAll(existingConstraint.dropStatement()) statements.addAll(createFKey(foreignKey)) } @@ -355,8 +352,7 @@ object SchemaUtils { } catch (exception: ExposedSQLException) { if (currentDialect.requiresAutoCommitOnCreateDrop && !transaction.connection.autoCommit) { throw IllegalStateException( - "${currentDialect.name} requires autoCommit to be enabled for CREATE DATABASE", - exception + "${currentDialect.name} requires autoCommit to be enabled for CREATE DATABASE", exception ) } else { throw exception @@ -384,8 +380,7 @@ object SchemaUtils { } catch (exception: ExposedSQLException) { if (currentDialect.requiresAutoCommitOnCreateDrop && !transaction.connection.autoCommit) { throw IllegalStateException( - "${currentDialect.name} requires autoCommit to be enabled for DROP DATABASE", - exception + "${currentDialect.name} requires autoCommit to be enabled for DROP DATABASE", exception ) } else { throw exception @@ -429,8 +424,7 @@ object SchemaUtils { } val executedStatements = createStatements + alterStatements logTimeSpent("Checking mapping consistence", withLogs) { - val modifyTablesStatements = checkMappingConsistence(tables = tables, withLogs) - .filter { it !in executedStatements } + val modifyTablesStatements = checkMappingConsistence(tables = tables, withLogs).filter { it !in executedStatements } execStatements(inBatch, modifyTablesStatements) commit() } @@ -452,8 +446,7 @@ object SchemaUtils { } val executedStatements = createStatements + alterStatements val modifyTablesStatements = logTimeSpent("Checking mapping consistence", withLogs) { - checkMappingConsistence(tables = tablesToAlter.toTypedArray(), withLogs) - .filter { it !in executedStatements } + checkMappingConsistence(tables = tablesToAlter.toTypedArray(), withLogs).filter { it !in executedStatements } } return executedStatements + modifyTablesStatements } @@ -490,9 +483,7 @@ object SchemaUtils { } val excessiveIndices = - currentDialect.existingIndices(*tables) - .flatMap { it.value } - .groupBy { Triple(it.table, it.unique, it.columns.joinToString { it.name }) } + currentDialect.existingIndices(*tables).flatMap { it.value }.groupBy { Triple(it.table, it.unique, it.columns.joinToString { it.name }) } .filter { it.value.size > 1 } if (excessiveIndices.isNotEmpty()) { exposedLogger.warn("List of excessive indices:") @@ -552,8 +543,7 @@ object SchemaUtils { nameDiffers.add(mappedIndex) } - notMappedIndices.getOrPut(table.nameInDatabaseCase()) { hashSetOf() } - .addAll(existingTableIndices.subtract(mappedIndices)) + notMappedIndices.getOrPut(table.nameInDatabaseCase()) { hashSetOf() }.addAll(existingTableIndices.subtract(mappedIndices)) missingIndices.addAll(mappedIndices.subtract(existingTableIndices)) } @@ -592,10 +582,7 @@ object SchemaUtils { fun drop(vararg tables: Table, inBatch: Boolean = false) { if (tables.isEmpty()) return with(TransactionManager.current()) { - var tablesForDeletion = - sortTablesByReferences(tables.toList()) - .reversed() - .filter { it in tables } + var tablesForDeletion = sortTablesByReferences(tables.toList()).reversed().filter { it in tables } if (!currentDialect.supportsIfNotExists) { tablesForDeletion = tablesForDeletion.filter { it.exists() } } @@ -623,6 +610,7 @@ object SchemaUtils { is MysqlDialect -> { connection.catalog = schema.identifier } + is H2Dialect -> { connection.schema = schema.identifier } diff --git a/exposed-jodatime/src/test/kotlin/org/jetbrains/exposed/JodaTimeDefaultsTest.kt b/exposed-jodatime/src/test/kotlin/org/jetbrains/exposed/JodaTimeDefaultsTest.kt index b046d3e13d..e63120cf7e 100644 --- a/exposed-jodatime/src/test/kotlin/org/jetbrains/exposed/JodaTimeDefaultsTest.kt +++ b/exposed-jodatime/src/test/kotlin/org/jetbrains/exposed/JodaTimeDefaultsTest.kt @@ -282,7 +282,7 @@ class JodaTimeDefaultsTest : JodaTimeBaseTest() { } withTables(testDate) { - val duration: Long = 2_000 + val duration: Long = 2_00 val before = currentDateTime() Thread.sleep(duration) diff --git a/exposed-tests/src/main/kotlin/org/jetbrains/exposed/sql/tests/DatabaseTestsBase.kt b/exposed-tests/src/main/kotlin/org/jetbrains/exposed/sql/tests/DatabaseTestsBase.kt index 377bc95a4e..6a64fead1d 100644 --- a/exposed-tests/src/main/kotlin/org/jetbrains/exposed/sql/tests/DatabaseTestsBase.kt +++ b/exposed-tests/src/main/kotlin/org/jetbrains/exposed/sql/tests/DatabaseTestsBase.kt @@ -103,24 +103,29 @@ abstract class DatabaseTestsBase { } fun withTables(excludeSettings: List, vararg tables: Table, statement: Transaction.(TestDB) -> Unit) { - val toTest = TestDB.enabledDialects() - excludeSettings - Assume.assumeTrue(toTest.isNotEmpty()) - toTest.forEach { testDB -> - withDb(testDB) { - SchemaUtils.create(*tables) + val testDB: TestDB? = dialect as? TestDB + + if (testDB == null) { + Assume.assumeFalse(false) + return + } + + Assume.assumeFalse(testDB in excludeSettings) + + withDb(testDB) { + SchemaUtils.create(*tables) + try { + statement(testDB) + commit() // Need commit to persist data before drop tables + } finally { try { - statement(testDB) - commit() // Need commit to persist data before drop tables - } finally { - try { + SchemaUtils.drop(*tables) + commit() + } catch (_: Exception) { + val database = testDB.db!! + inTopLevelTransaction(database.transactionManager.defaultIsolationLevel, db = database) { + repetitionAttempts = 1 SchemaUtils.drop(*tables) - commit() - } catch (_: Exception) { - val database = testDB.db!! - inTopLevelTransaction(database.transactionManager.defaultIsolationLevel, db = database) { - repetitionAttempts = 1 - SchemaUtils.drop(*tables) - } } } }