Skip to content

Commit

Permalink
fix: EXPOSED-625 SchemaUtils.listTables() retrieves tables for the …
Browse files Browse the repository at this point in the history
…default schema only

`SchemaUtils.listTables()` previously retrieved tables for the default schema only, and if the user created a new schema and created tables in the new schema, these were not shown in the list. This is fixed by invoking `getAllTableNamesCache()` instead of `tableNamesByCurrentSchema` in the `allTablesNames()` function.
  • Loading branch information
joc-a committed Nov 11, 2024
1 parent 212c69b commit fd0210a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,9 @@ abstract class VendorDialect(

override fun getDatabase(): String = catalog(TransactionManager.current())

/**
* Returns a list with the names of all the defined tables with schema prefixes if database supports it.
* This method always re-read data from DB.
* Using `allTablesNames` field is the preferred way.
*/
/** 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 {
tableNamesByCurrentSchema(null).tableNames
getAllTableNamesCache().flatMap { it.value }
}

override fun tableExists(table: Table): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.currentDialectTest
import org.jetbrains.exposed.sql.tests.inProperCase
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.assertTrue
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.vendors.MysqlDialect
import org.jetbrains.exposed.sql.vendors.OracleDialect
import org.jetbrains.exposed.sql.vendors.SQLServerDialect
import org.junit.Test
import java.util.*
import kotlin.test.assertFails
Expand Down Expand Up @@ -594,6 +598,16 @@ class CreateTableTests : DatabaseTestsBase() {
SchemaUtils.create(OneOneTable)
assertEquals(true, OneTable.exists())
assertEquals(true, OneOneTable.exists())

val tableNames = SchemaUtils.listTables()
val defaultSchemaName = when (currentDialectTest) {
is SQLServerDialect -> "dbo"
is OracleDialect -> testDb.user
is MysqlDialect -> testDb.db!!.name
else -> "public"
}
assertTrue(tableNames.any { it.equals("$defaultSchemaName.${OneTable.tableName}", ignoreCase = true) })
assertTrue(tableNames.any { it.equals(OneOneTable.tableName, ignoreCase = true) })
} finally {
SchemaUtils.drop(OneTable, OneOneTable)
val cascade = testDb != TestDB.SQLSERVER
Expand Down

0 comments on commit fd0210a

Please sign in to comment.