Skip to content

Commit

Permalink
Exposed tries to create the same foreign key constraint even if one a…
Browse files Browse the repository at this point in the history
…lready exists #843
  • Loading branch information
Tapac committed Mar 23, 2020
1 parent 999fc41 commit afd0147
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private fun checkMissingIndices(vararg tables: Table): List<Index> {
val fKeyConstraints = currentDialect.columnConstraints(*tables).keys
val existingIndices = currentDialect.existingIndices(*tables)
fun List<Index>.filterFKeys() = if (isMysql)
filterNot { (it.table.tableName.inProperCase() to it.columns.singleOrNull()?.let { c -> tr.identity(c) }) in fKeyConstraints }
filterNot { it.table to it.columns.singleOrNull() in fKeyConstraints }
else
this

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ object SchemaUtils {
for (column in table.columns) {
val foreignKey = column.foreignKey
if (foreignKey != null) {
val existingConstraint = existingColumnConstraint[table.tableName.inProperCase() to identity(column)]?.firstOrNull()
val existingConstraint = existingColumnConstraint[table to column]?.firstOrNull()
if (existingConstraint == null) {
statements.addAll(createFKey(column))
} else if (existingConstraint.targetTable != foreignKey.targetTable
} else if (existingConstraint.target.table != foreignKey.target.table
|| foreignKey.deleteRule != existingConstraint.deleteRule
|| foreignKey.updateRule != existingConstraint.updateRule) {
statements.addAll(existingConstraint.dropStatement())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import java.nio.ByteBuffer
import java.util.*
import java.util.concurrent.ConcurrentHashMap

internal typealias TableAndColumnName = Pair<String, String>

/**
* Provides definitions for all the supported SQL data types.
* By default, definitions from the SQL standard are provided but if a vendor doesn't support a specific type, or it is
Expand Down Expand Up @@ -523,7 +521,7 @@ interface DatabaseDialect {
fun tableColumns(vararg tables: Table): Map<Table, List<ColumnMetadata>> = emptyMap()

/** Returns a map with the foreign key constraints of all the defined columns in each of the specified [tables]. */
fun columnConstraints(vararg tables: Table): Map<TableAndColumnName, List<ForeignKeyConstraint>> = emptyMap()
fun columnConstraints(vararg tables: Table): Map<Pair<Table, Column<*>>, List<ForeignKeyConstraint>> = emptyMap()

/** Returns a map with all the defined indices in each of the specified [tables]. */
fun existingIndices(vararg tables: Table): Map<Table, List<Index>> = emptyMap()
Expand Down Expand Up @@ -613,15 +611,15 @@ abstract class VendorDialect(
override fun tableColumns(vararg tables: Table): Map<Table, List<ColumnMetadata>> =
TransactionManager.current().connection.metadata { columns(*tables) }

override fun columnConstraints(vararg tables: Table): Map<Pair<String, String>, List<ForeignKeyConstraint>> {
val constraints = HashMap<Pair<String, String>, MutableList<ForeignKeyConstraint>>()
override fun columnConstraints(vararg tables: Table): Map<Pair<Table, Column<*>>, List<ForeignKeyConstraint>> {
val constraints = HashMap<Pair<Table, Column<*>>, MutableList<ForeignKeyConstraint>>()

val tablesToLoad = tables.filter { !columnConstraintsCache.containsKey(it.nameInDatabaseCase()) }

fillConstraintCacheForTables(tablesToLoad)
tables.forEach { table ->
columnConstraintsCache[table.nameInDatabaseCase()].orEmpty().forEach {
constraints.getOrPut(it.fromTable to it.fromColumn) { arrayListOf() }.add(it)
constraints.getOrPut(it.from.table to it.from) { arrayListOf() }.add(it)
}

}
Expand Down

0 comments on commit afd0147

Please sign in to comment.