Skip to content

Commit

Permalink
Allow defining tables with quoted names (#1562)
Browse files Browse the repository at this point in the history
* Allow defining tables with quoted names

* Expect different dialects to produce different case for Foreign Key in tests

* Remove unnecessary import
  • Loading branch information
AlexeySoshin authored Oct 2, 2022
1 parent 070bff8 commit b4cde5b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
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

0 comments on commit b4cde5b

Please sign in to comment.