Skip to content

Commit

Permalink
mssqldef: Quote constraint name (#429)
Browse files Browse the repository at this point in the history
Close #421
  • Loading branch information
odz authored Aug 22, 2023
1 parent 926a37e commit d1acd50
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 10 additions & 0 deletions cmd/mssqldef/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,13 @@ CreateViewWithMultiline:
CREATE VIEW v AS
SELECT
10 as N;
PercentConstraintName:
current: |
CREATE TABLE [dbo].[percent_test] (
[test%] int CONSTRAINT [DF_percent_test_test%] DEFAULT (NULL)
);
desired: |
CREATE TABLE [dbo].[percent_test] (
[test%] int CONSTRAINT [DF_percent_test_test%] DEFAULT (NULL)
);
output: ""
6 changes: 3 additions & 3 deletions database/mssql/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func buildDumpTableDDL(table string, columns []column, indexDefs []*indexDef, fo
fmt.Fprint(&queryBuilder, " NOT NULL")
}
if col.DefaultName != "" {
fmt.Fprintf(&queryBuilder, " CONSTRAINT %s DEFAULT %s", col.DefaultName, col.DefaultVal)
fmt.Fprintf(&queryBuilder, " CONSTRAINT %s DEFAULT %s", quoteName(col.DefaultName), col.DefaultVal)
}
if col.Identity != nil {
fmt.Fprintf(&queryBuilder, " IDENTITY(%s,%s)", col.Identity.SeedValue, col.Identity.IncrementValue)
Expand All @@ -124,7 +124,7 @@ func buildDumpTableDDL(table string, columns []column, indexDefs []*indexDef, fo
}
}
if col.Check != nil {
fmt.Fprintf(&queryBuilder, " CONSTRAINT [%s] CHECK", col.Check.Name)
fmt.Fprintf(&queryBuilder, " CONSTRAINT %s CHECK", quoteName(col.Check.Name))
if col.Check.NotForReplication {
fmt.Fprint(&queryBuilder, " NOT FOR REPLICATION")
}
Expand All @@ -138,7 +138,7 @@ func buildDumpTableDDL(table string, columns []column, indexDefs []*indexDef, fo
continue
}
fmt.Fprint(&queryBuilder, ",\n"+indent)
fmt.Fprintf(&queryBuilder, "CONSTRAINT [%s] PRIMARY KEY", indexDef.name)
fmt.Fprintf(&queryBuilder, "CONSTRAINT %s PRIMARY KEY", quoteName(indexDef.name))

if indexDef.indexType == "CLUSTERED" || indexDef.indexType == "NONCLUSTERED" {
fmt.Fprintf(&queryBuilder, " %s", indexDef.indexType)
Expand Down

0 comments on commit d1acd50

Please sign in to comment.