From f26b7bc6e0d8d4ed70c35143d38c6cff7d31e1ba Mon Sep 17 00:00:00 2001 From: Alexey Soshin Date: Sun, 14 Aug 2022 15:19:06 +0100 Subject: [PATCH 1/3] Allow defining tables with quoted names --- .../org/jetbrains/exposed/sql/Constraints.kt | 9 +++++++- .../sql/tests/shared/ddl/CreateTableTests.kt | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Constraints.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Constraints.kt index 950401f84e..0a1f4a241f 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Constraints.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Constraints.kt @@ -106,7 +106,14 @@ data class ForeignKeyConstraint( /** Name of this constraint. */ val fkName: String get() = tx.db.identifierManager.cutIfNecessaryAndQuote( - name ?: "fk_${fromTable.tableNameWithoutScheme}_${from.joinToString("_") { it.name }}__${target.joinToString("_") { it.name }}" + name ?: "fk_${ + fromTable.tableNameWithoutScheme + // Table name may contain quotes, remove those before appending + .replace( + "\"", + "" + ) + }_${from.joinToString("_") { it.name }}__${target.joinToString("_") { it.name }}" ).inProperCase() internal val foreignKeyPart: String get() = buildString { diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt index 620f4d7d7d..6fd31c5df6 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt @@ -219,6 +219,27 @@ class CreateTableTests : DatabaseTestsBase() { } } + @Test + fun createTableWithQuotes() { + val parent = object : LongIdTable("\"Parent\"") {} + val child = object : LongIdTable("\"Child\"") { + val parentId = reference( + name = "parent_id", + foreign = parent, + onUpdate = ReferenceOption.NO_ACTION, + onDelete = ReferenceOption.NO_ACTION, + ) + } + withTables(parent, child) { + val expected = listOf( + """ + CREATE TABLE IF NOT EXISTS "Child" (ID BIGINT AUTO_INCREMENT PRIMARY KEY, PARENT_ID BIGINT NOT NULL, CONSTRAINT FK_CHILD_PARENT_ID__ID FOREIGN KEY (PARENT_ID) REFERENCES "Parent"(ID)) + """.trimIndent() + ) + assertEqualCollections(child.ddl, expected) + } + } + @Test fun createTableWithExplicitForeignKeyName2() { val fkName = "MyForeignKey2" From 796995d351ab0cee555048736549316fb95fda6a Mon Sep 17 00:00:00 2001 From: Alexey Soshin Date: Sun, 14 Aug 2022 16:44:12 +0100 Subject: [PATCH 2/3] Expect different dialects to produce different case for Foreign Key in tests --- .../exposed/sql/tests/shared/ddl/CreateTableTests.kt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt index 6fd31c5df6..c97dbe319a 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt @@ -231,10 +231,14 @@ class CreateTableTests : DatabaseTestsBase() { ) } withTables(parent, child) { + // Different dialects use different mix of lowercase/uppercase in their names val expected = listOf( - """ - CREATE TABLE IF NOT EXISTS "Child" (ID BIGINT AUTO_INCREMENT PRIMARY KEY, PARENT_ID BIGINT NOT NULL, CONSTRAINT FK_CHILD_PARENT_ID__ID FOREIGN KEY (PARENT_ID) REFERENCES "Parent"(ID)) - """.trimIndent() + "CREATE TABLE " + addIfNotExistsIfSupported() + "${this.identity(child)} (" + + "${child.columns.joinToString { it.descriptionDdl(false) }}," + + " CONSTRAINT ${"fk_Child_parent_id__id".inProperCase()}" + + " FOREIGN KEY (${this.identity(child.parentId)})" + + " REFERENCES ${this.identity(parent)}(${this.identity(parent.id)})" + + ")" ) assertEqualCollections(child.ddl, expected) } From e020fe083e1642d822607dbbfa6160d3fc6de2fe Mon Sep 17 00:00:00 2001 From: Alexey Soshin Date: Wed, 24 Aug 2022 15:58:17 +0100 Subject: [PATCH 3/3] Remove unnecessary import --- .../src/main/kotlin/org/jetbrains/exposed/sql/Database.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Database.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Database.kt index 06fcdf48c3..085231bace 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Database.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Database.kt @@ -3,7 +3,6 @@ package org.jetbrains.exposed.sql import org.jetbrains.annotations.TestOnly import org.jetbrains.exposed.sql.statements.api.ExposedConnection import org.jetbrains.exposed.sql.statements.api.ExposedDatabaseMetadata -import org.jetbrains.exposed.sql.transactions.DEFAULT_ISOLATION_LEVEL import org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManager import org.jetbrains.exposed.sql.transactions.TransactionManager import org.jetbrains.exposed.sql.vendors.*