Skip to content

Commit

Permalink
fix: change unique tag to create a separate unique constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Sep 10, 2021
1 parent a4ae4af commit 8401615
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 3 deletions.
9 changes: 9 additions & 0 deletions internal/dbtest/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,15 @@ func TestQuery(t *testing.T) {
Model(model).
On("CONFLICT (id) DO UPDATE")
},
func(db *bun.DB) schema.QueryAppender {
type Model struct {
Foo string `bun:",unique"`
Bar string `bun:",unique"`
Hello string `bun:"unique:group"`
World string `bun:"unique:group"`
}
return db.NewCreateTable().Model((*Model)(nil))
},
}

timeRE := regexp.MustCompile(`'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+(\+\d{2}:\d{2})?'`)
Expand Down
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-83
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE `models` (`foo` VARCHAR(255), `bar` VARCHAR(255), `hello` VARCHAR(255), `world` VARCHAR(255), UNIQUE (`foo`), UNIQUE (`bar`), CONSTRAINT `group` UNIQUE (`hello`, `world`))
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-83
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE `models` (`foo` VARCHAR(255), `bar` VARCHAR(255), `hello` VARCHAR(255), `world` VARCHAR(255), UNIQUE (`foo`), UNIQUE (`bar`), CONSTRAINT `group` UNIQUE (`hello`, `world`))
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-83
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE "models" ("foo" VARCHAR, "bar" VARCHAR, "hello" VARCHAR, "world" VARCHAR, UNIQUE ("foo"), UNIQUE ("bar"), CONSTRAINT "group" UNIQUE ("hello", "world"))
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-83
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE "models" ("foo" VARCHAR, "bar" VARCHAR, "hello" VARCHAR, "world" VARCHAR, UNIQUE ("foo"), UNIQUE ("bar"), CONSTRAINT "group" UNIQUE ("hello", "world"))
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-83
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE "models" ("foo" VARCHAR, "bar" VARCHAR, "hello" VARCHAR, "world" VARCHAR, UNIQUE ("foo"), UNIQUE ("bar"), CONSTRAINT "group" UNIQUE ("hello", "world"))
11 changes: 8 additions & 3 deletions query_table_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,20 @@ func (q *CreateTableQuery) appendUniqueConstraints(fmter schema.Formatter, b []b
sort.Strings(keys)

for _, key := range keys {
b = q.appendUniqueConstraint(fmter, b, key, unique[key])
if key == "" {
for _, field := range unique[key] {
b = q.appendUniqueConstraint(fmter, b, key, field)
}
continue
}
b = q.appendUniqueConstraint(fmter, b, key, unique[key]...)
}

return b
}

func (q *CreateTableQuery) appendUniqueConstraint(
fmter schema.Formatter, b []byte, name string, fields []*schema.Field,
fmter schema.Formatter, b []byte, name string, fields ...*schema.Field,
) []byte {
if name != "" {
b = append(b, ", CONSTRAINT "...)
Expand All @@ -204,7 +210,6 @@ func (q *CreateTableQuery) appendUniqueConstraint(
b = append(b, " UNIQUE ("...)
b = appendColumns(b, "", fields)
b = append(b, ")"...)

return b
}

Expand Down

0 comments on commit 8401615

Please sign in to comment.