Skip to content

Commit

Permalink
Reduce date time test time
Browse files Browse the repository at this point in the history
  • Loading branch information
e5l committed Sep 4, 2023
1 parent d9f390d commit 564a264
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -195,6 +200,7 @@ object SchemaUtils {
processed
}
}

is MariaDBDialect -> processed.trim('\'')
is MysqlDialect -> "_utf8mb4\\'${processed.trim('(', ')', '\'')}\\"
else -> processed.trim('\'')
Expand All @@ -205,19 +211,18 @@ 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()
}
}
processed
}

else -> processForDefaultValue(exp)
}
}
Expand Down Expand Up @@ -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) }

Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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:")
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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() }
}
Expand Down Expand Up @@ -623,6 +610,7 @@ object SchemaUtils {
is MysqlDialect -> {
connection.catalog = schema.identifier
}

is H2Dialect -> {
connection.schema = schema.identifier
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,29 @@ abstract class DatabaseTestsBase {
}

fun withTables(excludeSettings: List<TestDB>, 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)
}
}
}
}
Expand Down

0 comments on commit 564a264

Please sign in to comment.