Skip to content

Commit

Permalink
fix: EXPOSED-662 SchemaUtils.listTables() closes connection to db
Browse files Browse the repository at this point in the history
For some unclear reason, the changes made [here](008faf3) were causing the database connection to close. This fixes that by adding a new function `tableNamesForAllSchemas()` and invoking that in `allTablesNames()`.
  • Loading branch information
joc-a committed Dec 10, 2024
1 parent 6bbceb4 commit 9d49b3e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions exposed-core/api/exposed-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -3574,6 +3574,7 @@ public abstract class org/jetbrains/exposed/sql/statements/api/ExposedDatabaseMe
public abstract fun sequences ()Ljava/util/List;
public abstract fun tableConstraints (Ljava/util/List;)Ljava/util/Map;
public abstract fun tableNamesByCurrentSchema (Ljava/util/Map;)Lorg/jetbrains/exposed/sql/vendors/SchemaMetadata;
public abstract fun tableNamesForAllSchemas ()Ljava/util/List;
}

public abstract class org/jetbrains/exposed/sql/statements/api/ExposedSavepoint {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ abstract class ExposedDatabaseMetadata(val database: String) {
*/
abstract fun tableNamesByCurrentSchema(tableNamesCache: Map<String, List<String>>?): SchemaMetadata

abstract fun tableNamesForAllSchemas(): List<String>

/** Returns a map with the [ColumnMetadata] of all the defined columns in each of the specified [tables]. */
abstract fun columns(vararg tables: Table): Map<Table, List<ColumnMetadata>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,15 @@ abstract class VendorDialect(
}

protected fun getAllTableNamesCache(): Map<String, List<String>> {
val connection = TransactionManager.current().connection
if (_allTableNames == null) {
_allTableNames = connection.metadata { tableNames }
_allTableNames = TransactionManager.current().connection.metadata { tableNames }
}
return _allTableNames!!
}

private fun getAllSchemaNamesCache(): List<String> {
val connection = TransactionManager.current().connection
if (_allSchemaNames == null) {
_allSchemaNames = connection.metadata { schemaNames }
_allSchemaNames = TransactionManager.current().connection.metadata { schemaNames }
}
return _allSchemaNames!!
}
Expand All @@ -53,7 +51,7 @@ abstract class VendorDialect(

/** Returns a list with the names of all the defined tables with schema prefixes if the database supports it. */
override fun allTablesNames(): List<String> = TransactionManager.current().connection.metadata {
getAllTableNamesCache().flatMap { it.value }
tableNamesForAllSchemas()
}

override fun tableExists(table: Table): Boolean {
Expand Down
1 change: 1 addition & 0 deletions exposed-jdbc/api/exposed-jdbc.api
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public final class org/jetbrains/exposed/sql/statements/jdbc/JdbcDatabaseMetadat
public fun sequences ()Ljava/util/List;
public fun tableConstraints (Ljava/util/List;)Ljava/util/Map;
public fun tableNamesByCurrentSchema (Ljava/util/Map;)Lorg/jetbrains/exposed/sql/vendors/SchemaMetadata;
public fun tableNamesForAllSchemas ()Ljava/util/List;
}

public final class org/jetbrains/exposed/sql/statements/jdbc/JdbcDatabaseMetadataImpl$Companion {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ class JdbcDatabaseMetadataImpl(database: String, val metadata: DatabaseMetaData)
return SchemaMetadata(currentSchema!!, tablesInSchema)
}

override fun tableNamesForAllSchemas(): List<String> =
schemaNames.flatMap { tableNamesFor(it) }

private fun ResultSet.extractColumns(): List<ColumnMetadata> {
val result = mutableListOf<ColumnMetadata>()
while (next()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.jetbrains.exposed.sql.tests.shared.Category
import org.jetbrains.exposed.sql.tests.shared.Item
import org.jetbrains.exposed.sql.tests.shared.assertEqualCollections
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.assertFalse
import org.jetbrains.exposed.sql.tests.shared.assertTrue
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.vendors.MysqlDialect
Expand Down Expand Up @@ -666,4 +667,23 @@ class CreateTableTests : DatabaseTestsBase() {
}
}
}

@Test
fun testListTablesDoesNotCloseDatabaseConnection() {
val tester = object : IntIdTable("tester") {
val int = integer("intColumn")
}
withDb { testDb ->
val defaultSchemaName = when (currentDialectTest) {
is SQLServerDialect -> "dbo"
is OracleDialect -> testDb.user
is MysqlDialect -> testDb.db!!.name
else -> "public"
}
assertTrue(SchemaUtils.listTables().none { it.equals("$defaultSchemaName.${OneTable.tableName}", ignoreCase = true) })
}
withDb {
assertFalse(tester.exists())
}
}
}

0 comments on commit 9d49b3e

Please sign in to comment.