From 27e705ccc3c3e5b5607e65269fc1d9a96cbed32f Mon Sep 17 00:00:00 2001 From: Chantal Loncle <82039410+bog-walk@users.noreply.github.com> Date: Tue, 8 Aug 2023 20:52:53 -0400 Subject: [PATCH] fix: EXPOSED-135 Oracle does not use setSchema value as currentScheme The following test fails when run on Oracle: CreateIndexTests/'test possibility to create indexes when table exists in different schemas'() Fails with 'ORA-01430: column being added already exists in table' due to this statement being incorrectly generated: ALTER TABLE TEST_TABLE ADD ID NUMBER(12) NOT NULL This happens because metadata returns an empty map of existing table ColumnMetadata when columns() is invoked in JdbcDatabaseMetadataImpl. getColumns() relies on the value of currentScheme which is invariably set to return databaseName in Oracle; that is, the default user set up at connection, EXPOSEDTEST. So, even though a correct statement is executed, which changes schema in the DB, Exposed is using the default schema to query metadata. currentScheme now attempts to get a connection schema first before using databaseName. --- .../statements/jdbc/JdbcDatabaseMetadataImpl.kt | 2 +- .../sql/tests/shared/ddl/CreateIndexTests.kt | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/exposed-jdbc/src/main/kotlin/org/jetbrains/exposed/sql/statements/jdbc/JdbcDatabaseMetadataImpl.kt b/exposed-jdbc/src/main/kotlin/org/jetbrains/exposed/sql/statements/jdbc/JdbcDatabaseMetadataImpl.kt index 27bfefa0d1..9e7ebd5641 100644 --- a/exposed-jdbc/src/main/kotlin/org/jetbrains/exposed/sql/statements/jdbc/JdbcDatabaseMetadataImpl.kt +++ b/exposed-jdbc/src/main/kotlin/org/jetbrains/exposed/sql/statements/jdbc/JdbcDatabaseMetadataImpl.kt @@ -64,7 +64,7 @@ class JdbcDatabaseMetadataImpl(database: String, val metadata: DatabaseMetaData) field = try { when (databaseDialectName) { MysqlDialect.dialectName, MariaDBDialect.dialectName -> metadata.connection.catalog.orEmpty() - OracleDialect.dialectName -> databaseName + OracleDialect.dialectName -> metadata.connection.schema ?: databaseName else -> metadata.connection.schema.orEmpty() } } catch (_: Throwable) { diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateIndexTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateIndexTests.kt index 5c439c9aac..a302e426ff 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateIndexTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateIndexTests.kt @@ -69,8 +69,8 @@ class CreateIndexTests : DatabaseTestsBase() { } @Test - fun `test possibility to create indexes when table exists in different schemas`() { - val TestTable = object : Table("test_table") { + fun testCreateIndexWithTableInDifferentSchemas() { + val testTable = object : Table("test_table") { val id = integer("id").uniqueIndex() val name = varchar("name", length = 42).index("test_index") init { @@ -81,16 +81,15 @@ class CreateIndexTests : DatabaseTestsBase() { val schema2 = Schema("Schema2") withSchemas(listOf(TestDB.SQLITE, TestDB.SQLSERVER), schema1, schema2) { SchemaUtils.setSchema(schema1) - SchemaUtils.createMissingTablesAndColumns(TestTable) - assertEquals(true, TestTable.exists()) + SchemaUtils.createMissingTablesAndColumns(testTable) + assertEquals(true, testTable.exists()) SchemaUtils.setSchema(schema2) - assertEquals(false, TestTable.exists()) - SchemaUtils.createMissingTablesAndColumns(TestTable) - assertEquals(true, TestTable.exists()) + assertEquals(false, testTable.exists()) + SchemaUtils.createMissingTablesAndColumns(testTable) + assertEquals(true, testTable.exists()) } } - @Test fun testCreateAndDropPartialIndexWithPostgres() { val partialIndexTable = object : IntIdTable("PartialIndexTableTest") {