Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow defining tables with quoted names #1562

Merged
merged 4 commits into from
Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,31 @@ 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) {
// Different dialects use different mix of lowercase/uppercase in their names
val expected = listOf(
"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)
}
}

@Test
fun createTableWithExplicitForeignKeyName2() {
val fkName = "MyForeignKey2"
Expand Down