Skip to content

Commit

Permalink
Fix remaining tests
Browse files Browse the repository at this point in the history
  • Loading branch information
e5l committed Sep 4, 2023
1 parent e790e91 commit dc8009a
Show file tree
Hide file tree
Showing 18 changed files with 229 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import org.gradle.api.Project
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.configure


fun Project.configureDetekt() {
apply<DetektPlugin>()

configure<DetektExtension> {
ignoreFailures = false
buildUponDefaultConfig = true
config = files(
rootDir.resolve("detekt/detekt-config.yml").takeIf { it.isFile },
rootDir.resolve("detekt/detekt-config.yml").takeIf {
it.isFile
},
projectDir.resolve("detekt/detekt-config.yml").takeIf { it.isFile }
)
reports {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.getValue
import org.gradle.kotlin.dsl.provideDelegate
import org.gradle.kotlin.dsl.register
import java.io.File
import java.time.Duration

const val HEALTH_TIMEOUT: Long = 60

class TestDb(val name: String) {
internal val dialects = mutableListOf<String>()
Expand Down Expand Up @@ -76,7 +76,7 @@ private fun Project.configureCompose(db: TestDb) {
removeVolumes.set(true)
stopContainers.set(false)

waitForHealthyStateTimeout.set(Duration.ofMinutes(60))
waitForHealthyStateTimeout.set(Duration.ofMinutes(HEALTH_TIMEOUT))
}
}

Expand Down
10 changes: 10 additions & 0 deletions exposed-core/api/exposed-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -1814,6 +1814,7 @@ public final class org/jetbrains/exposed/sql/SchemaUtils {
public static synthetic fun dropSchema$default (Lorg/jetbrains/exposed/sql/SchemaUtils;[Lorg/jetbrains/exposed/sql/Schema;ZZILjava/lang/Object;)V
public final fun dropSequence ([Lorg/jetbrains/exposed/sql/Sequence;Z)V
public static synthetic fun dropSequence$default (Lorg/jetbrains/exposed/sql/SchemaUtils;[Lorg/jetbrains/exposed/sql/Sequence;ZILjava/lang/Object;)V
public final fun listDatabases ()Ljava/util/List;
public final fun setSchema (Lorg/jetbrains/exposed/sql/Schema;Z)V
public static synthetic fun setSchema$default (Lorg/jetbrains/exposed/sql/SchemaUtils;Lorg/jetbrains/exposed/sql/Schema;ZILjava/lang/Object;)V
public final fun sortTablesByReferences (Ljava/lang/Iterable;)Ljava/util/List;
Expand Down Expand Up @@ -2889,7 +2890,9 @@ public final class org/jetbrains/exposed/sql/statements/StatementType : java/lan
public static final field GRANT Lorg/jetbrains/exposed/sql/statements/StatementType;
public static final field INSERT Lorg/jetbrains/exposed/sql/statements/StatementType;
public static final field OTHER Lorg/jetbrains/exposed/sql/statements/StatementType;
public static final field PRAGMA Lorg/jetbrains/exposed/sql/statements/StatementType;
public static final field SELECT Lorg/jetbrains/exposed/sql/statements/StatementType;
public static final field SHOW Lorg/jetbrains/exposed/sql/statements/StatementType;
public static final field TRUNCATE Lorg/jetbrains/exposed/sql/statements/StatementType;
public static final field UPDATE Lorg/jetbrains/exposed/sql/statements/StatementType;
public final fun getGroup ()Lorg/jetbrains/exposed/sql/statements/StatementGroup;
Expand Down Expand Up @@ -3245,6 +3248,7 @@ public abstract interface class org/jetbrains/exposed/sql/vendors/DatabaseDialec
public abstract fun getSupportsTernaryAffectedRowValues ()Z
public abstract fun getSupportsWindowFrameGroupsMode ()Z
public abstract fun isAllowedAsColumnDefault (Lorg/jetbrains/exposed/sql/Expression;)Z
public abstract fun listDatabases ()Ljava/lang/String;
public abstract fun modifyColumn (Lorg/jetbrains/exposed/sql/Column;Lorg/jetbrains/exposed/sql/ColumnDiff;)Ljava/util/List;
public abstract fun resetCaches ()V
public abstract fun resetSchemaCaches ()V
Expand Down Expand Up @@ -3284,6 +3288,7 @@ public final class org/jetbrains/exposed/sql/vendors/DatabaseDialect$DefaultImpl
public static fun getSupportsTernaryAffectedRowValues (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;)Z
public static fun getSupportsWindowFrameGroupsMode (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;)Z
public static fun isAllowedAsColumnDefault (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;Lorg/jetbrains/exposed/sql/Expression;)Z
public static fun listDatabases (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;)Ljava/lang/String;
public static fun setSchema (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;Lorg/jetbrains/exposed/sql/Schema;)Ljava/lang/String;
public static fun tableColumns (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;[Lorg/jetbrains/exposed/sql/Table;)Ljava/util/Map;
}
Expand Down Expand Up @@ -3458,6 +3463,7 @@ public class org/jetbrains/exposed/sql/vendors/H2Dialect : org/jetbrains/exposed
public fun getSupportsWindowFrameGroupsMode ()Z
public fun isAllowedAsColumnDefault (Lorg/jetbrains/exposed/sql/Expression;)Z
public final fun isSecondVersion ()Z
public fun listDatabases ()Ljava/lang/String;
public fun modifyColumn (Lorg/jetbrains/exposed/sql/Column;Lorg/jetbrains/exposed/sql/ColumnDiff;)Ljava/util/List;
public fun toString ()Ljava/lang/String;
}
Expand Down Expand Up @@ -3534,6 +3540,7 @@ public class org/jetbrains/exposed/sql/vendors/OracleDialect : org/jetbrains/exp
public fun getSupportsOnlyIdentifiersInGeneratedKeys ()Z
public fun getSupportsOrderByNullsFirstLast ()Z
public fun isAllowedAsColumnDefault (Lorg/jetbrains/exposed/sql/Expression;)Z
public fun listDatabases ()Ljava/lang/String;
public fun modifyColumn (Lorg/jetbrains/exposed/sql/Column;Lorg/jetbrains/exposed/sql/ColumnDiff;)Ljava/util/List;
public fun setSchema (Lorg/jetbrains/exposed/sql/Schema;)Ljava/lang/String;
}
Expand All @@ -3552,6 +3559,7 @@ public class org/jetbrains/exposed/sql/vendors/PostgreSQLDialect : org/jetbrains
public fun getSupportsOrderByNullsFirstLast ()Z
public fun getSupportsWindowFrameGroupsMode ()Z
public fun isAllowedAsColumnDefault (Lorg/jetbrains/exposed/sql/Expression;)Z
public fun listDatabases ()Ljava/lang/String;
public fun modifyColumn (Lorg/jetbrains/exposed/sql/Column;Lorg/jetbrains/exposed/sql/ColumnDiff;)Ljava/util/List;
public fun setSchema (Lorg/jetbrains/exposed/sql/Schema;)Ljava/lang/String;
}
Expand Down Expand Up @@ -3617,6 +3625,7 @@ public class org/jetbrains/exposed/sql/vendors/SQLiteDialect : org/jetbrains/exp
public fun getSupportsMultipleGeneratedKeys ()Z
public fun getSupportsWindowFrameGroupsMode ()Z
public fun isAllowedAsColumnDefault (Lorg/jetbrains/exposed/sql/Expression;)Z
public fun listDatabases ()Ljava/lang/String;
}

public final class org/jetbrains/exposed/sql/vendors/SQLiteDialect$Companion : org/jetbrains/exposed/sql/vendors/VendorDialect$DialectNameProvider {
Expand Down Expand Up @@ -3665,6 +3674,7 @@ public abstract class org/jetbrains/exposed/sql/vendors/VendorDialect : org/jetb
public fun getSupportsTernaryAffectedRowValues ()Z
public fun getSupportsWindowFrameGroupsMode ()Z
public fun isAllowedAsColumnDefault (Lorg/jetbrains/exposed/sql/Expression;)Z
public fun listDatabases ()Ljava/lang/String;
public fun modifyColumn (Lorg/jetbrains/exposed/sql/Column;Lorg/jetbrains/exposed/sql/ColumnDiff;)Ljava/util/List;
protected final fun quoteIdentifierWhenWrongCaseOrNecessary (Ljava/lang/String;Lorg/jetbrains/exposed/sql/Transaction;)Ljava/lang/String;
public fun resetCaches ()V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ object SchemaUtils {
}

fun sortTablesByReferences(tables: Iterable<Table>) = TableDepthGraph(tables).sorted()

fun checkCycle(vararg tables: Table) = TableDepthGraph(tables.toList()).hasCycle()

fun createStatements(vararg tables: Table): List<String> {
Expand Down Expand Up @@ -122,7 +123,9 @@ object SchemaUtils {

@Deprecated(
"Will be removed in upcoming releases. Please use overloaded version instead",
ReplaceWith("createFKey(checkNotNull(reference.foreignKey) { \"${"$"}reference does not reference anything\" })"),
ReplaceWith(
"createFKey(checkNotNull(reference.foreignKey) { \"${"$"}reference does not reference anything\" })"
),
DeprecationLevel.HIDDEN
)
fun createFKey(reference: Column<*>): List<String> {
Expand All @@ -135,9 +138,13 @@ object SchemaUtils {

fun createFKey(foreignKey: ForeignKeyConstraint): List<String> = with(foreignKey) {
val allFromColumnsBelongsToTheSameTable = from.all { it.table == fromTable }
require(allFromColumnsBelongsToTheSameTable) { "not all referencing columns of $foreignKey belong to the same table" }
require(
allFromColumnsBelongsToTheSameTable
) { "not all referencing columns of $foreignKey belong to the same table" }
val allTargetColumnsBelongToTheSameTable = target.all { it.table == targetTable }
require(allTargetColumnsBelongToTheSameTable) { "not all referenced columns of $foreignKey belong to the same table" }
require(
allTargetColumnsBelongToTheSameTable
) { "not all referenced columns of $foreignKey belong to the same table" }
require(from.size == target.size) { "$foreignKey referencing columns are not in accordance with referenced" }
require(deleteRule != null || updateRule != null) { "$foreignKey has no reference constraint actions" }
require(target.toHashSet().size == target.size) { "not all referenced columns of $foreignKey are unique" }
Expand Down Expand Up @@ -255,21 +262,25 @@ 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 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 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() }
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 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() }

redoColumns.flatMapTo(statements) { (col, changedState) -> col.modifyStatements(changedState) }

Expand Down Expand Up @@ -305,7 +316,14 @@ 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) {
continue
}

val noForeignKey = existingConstraint.targetTable != foreignKey.targetTable
val deleteRuleMismatch = foreignKey.deleteRule != existingConstraint.deleteRule
val updateRuleMismatch = foreignKey.updateRule != existingConstraint.updateRule

if (noForeignKey || deleteRuleMismatch || updateRuleMismatch) {
statements.addAll(existingConstraint.dropStatement())
statements.addAll(createFKey(foreignKey))
}
Expand Down Expand Up @@ -352,14 +370,33 @@ 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
}
}
}

/**
* Returns a list of all databases.
*
* @return A list of strings representing the names of all databases.
*/
fun listDatabases(): List<String> {
val transaction = TransactionManager.current()
return with(transaction) {
exec(currentDialect.listDatabases()) {
val result = mutableListOf<String>()
while (it.next()) {
result.add(it.getString(1).lowercase())
}
result
} ?: emptyList()
}
}

/**
* Drops databases
*
Expand All @@ -380,7 +417,8 @@ 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 @@ -424,7 +462,10 @@ 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 @@ -446,7 +487,10 @@ 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 @@ -483,12 +527,15 @@ 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:")
excessiveIndices.forEach { (triple, indices) ->
exposedLogger.warn("\t\t\t'${triple.first.tableName}'.'${triple.third}' -> ${indices.joinToString(", ") { it.indexName }}")
val indexNames = indices.joinToString(", ") { it.indexName }
exposedLogger.warn("\t\t\t'${triple.first.tableName}'.'${triple.third}' -> $indexNames")
}
exposedLogger.info("SQL Queries to remove excessive indices:")
excessiveIndices.forEach {
Expand Down Expand Up @@ -543,7 +590,9 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ open class Transaction(
@Language("sql") stmt: String,
args: Iterable<Pair<IColumnType, Any?>> = emptyList(),
explicitStatementType: StatementType? = null
) =
exec(stmt, args, explicitStatementType) { }
) = exec(stmt, args, explicitStatementType) { }

fun <T : Any> exec(
@Language("sql") stmt: String,
Expand All @@ -126,7 +125,7 @@ open class Transaction(
return exec(object : Statement<T>(type, emptyList()) {
override fun PreparedStatementApi.executeInternal(transaction: Transaction): T? {
val result = when (type) {
StatementType.SELECT, StatementType.EXEC -> executeQuery()
StatementType.SELECT, StatementType.EXEC, StatementType.SHOW, StatementType.PRAGMA -> executeQuery()
else -> {
executeUpdate()
resultSet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,6 @@ enum class StatementGroup {
enum class StatementType(val group: StatementGroup) {
INSERT(StatementGroup.DML), UPDATE(StatementGroup.DML), DELETE(StatementGroup.DML), SELECT(StatementGroup.DML),
CREATE(StatementGroup.DDL), ALTER(StatementGroup.DDL), TRUNCATE(StatementGroup.DDL), DROP(StatementGroup.DDL),
GRANT(StatementGroup.DDL), EXEC(StatementGroup.DML), OTHER(StatementGroup.DDL)
GRANT(StatementGroup.DDL), EXEC(StatementGroup.DML), OTHER(StatementGroup.DDL),
SHOW(StatementGroup.DML), PRAGMA(StatementGroup.DML)
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ abstract class DataTypeProvider {
} else {
"'$e'"
}

e is LiteralOp<*> -> "$e"
e is Function<*> -> "$e"
currentDialect is MysqlDialect -> "$e"
Expand Down Expand Up @@ -980,6 +981,8 @@ interface DatabaseDialect {

fun createDatabase(name: String) = "CREATE DATABASE IF NOT EXISTS ${name.inProperCase()}"

fun listDatabases(): String = "SHOW DATABASES"

fun dropDatabase(name: String) = "DROP DATABASE IF EXISTS ${name.inProperCase()}"

fun setSchema(schema: Schema): String = "SET SCHEMA ${schema.identifier}"
Expand Down Expand Up @@ -1221,6 +1224,7 @@ abstract class VendorDialect(
.append(" WHERE ").append(it)
.toString()
}

else -> {
exposedLogger.warn("Index creation with a filter condition is not supported in ${currentDialect.name}")
return null
Expand Down Expand Up @@ -1282,6 +1286,7 @@ abstract class VendorDialect(
columns = fieldsList, type = index.indexType, filterCondition = maybeFilterCondition
)
}

else -> {
"CREATE INDEX $quotedIndexName ON $quotedTableName $fieldsList$maybeFilterCondition"
}
Expand Down
Loading

0 comments on commit dc8009a

Please sign in to comment.