From 210ba84bafd04ff6394e9fa12e5f284aec3c7d1a Mon Sep 17 00:00:00 2001 From: Akshith Gunasekaran Date: Fri, 18 Sep 2026 21:35:00 -0700 Subject: [PATCH] Quote schema identifiers during table creation --- identifier_quote_test.go | 43 ++++++++++++++++++++++++++++++++++++++++ table.go | 2 +- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/identifier_quote_test.go b/identifier_quote_test.go index 4300c704..21de4db8 100644 --- a/identifier_quote_test.go +++ b/identifier_quote_test.go @@ -25,6 +25,49 @@ func TestSqliteDialectEscapesIdentifierQuotes(t *testing.T) { } } +func TestSqlForCreateQuotesSchemaIdentifier(t *testing.T) { + tests := []struct { + name string + dialect Dialect + schema string + want string + }{ + { + name: "postgres", + dialect: PostgresDialect{}, + schema: `Mixed"Case`, + want: `create schema "Mixed""Case";create table ` + + `"Mixed""Case"."security_rows" ("ID" bigint) ;`, + }, + { + name: "mysql", + dialect: MySQLDialect{ + Engine: "InnoDB", + Encoding: "UTF8", + }, + schema: "Mixed`Case", + want: "create schema `Mixed``Case`;create table " + + "`Mixed``Case`.`security_rows` (`ID` bigint) engine=InnoDB charset=UTF8;", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + dbmap := &DbMap{Dialect: tc.dialect} + table := dbmap.AddTableWithNameAndSchema( + identifierIndexedRow{}, + tc.schema, + "security_rows", + ) + + got := table.SqlForCreate(false) + if got != tc.want { + t.Fatalf("SqlForCreate() = %q, want %q", got, tc.want) + } + }) + } +} + func TestSqliteQuotedTableNameCannotRewriteUpdateTarget(t *testing.T) { db, err := sql.Open("sqlite3", ":memory:") if err != nil { diff --git a/table.go b/table.go index f4b71389..9cd82081 100644 --- a/table.go +++ b/table.go @@ -175,7 +175,7 @@ func (t *TableMap) SqlForCreate(ifNotExists bool) string { } else { s.WriteString(schemaCreate) } - s.WriteString(fmt.Sprintf(" %s;", t.SchemaName)) + s.WriteString(fmt.Sprintf(" %s;", dialect.QuoteField(t.SchemaName))) } tableCreate := "create table"