From 84016155a77ca77613cc054277fefadae3098757 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Fri, 10 Sep 2021 18:07:31 +0300 Subject: [PATCH] fix: change unique tag to create a separate unique constraint --- internal/dbtest/query_test.go | 9 +++++++++ .../dbtest/testdata/snapshots/TestQuery-mysql5-83 | 1 + .../dbtest/testdata/snapshots/TestQuery-mysql8-83 | 1 + internal/dbtest/testdata/snapshots/TestQuery-pg-83 | 1 + internal/dbtest/testdata/snapshots/TestQuery-pgx-83 | 1 + .../dbtest/testdata/snapshots/TestQuery-sqlite-83 | 1 + query_table_create.go | 11 ++++++++--- 7 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-mysql5-83 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-mysql8-83 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-pg-83 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-pgx-83 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-sqlite-83 diff --git a/internal/dbtest/query_test.go b/internal/dbtest/query_test.go index f122f4d8d..a8284fd4e 100644 --- a/internal/dbtest/query_test.go +++ b/internal/dbtest/query_test.go @@ -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})?'`) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-83 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-83 new file mode 100644 index 000000000..f392e4c7e --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-83 @@ -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`)) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-83 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-83 new file mode 100644 index 000000000..f392e4c7e --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-83 @@ -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`)) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-83 b/internal/dbtest/testdata/snapshots/TestQuery-pg-83 new file mode 100644 index 000000000..bcc8ffb73 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-83 @@ -0,0 +1 @@ +CREATE TABLE "models" ("foo" VARCHAR, "bar" VARCHAR, "hello" VARCHAR, "world" VARCHAR, UNIQUE ("foo"), UNIQUE ("bar"), CONSTRAINT "group" UNIQUE ("hello", "world")) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-83 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-83 new file mode 100644 index 000000000..bcc8ffb73 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-83 @@ -0,0 +1 @@ +CREATE TABLE "models" ("foo" VARCHAR, "bar" VARCHAR, "hello" VARCHAR, "world" VARCHAR, UNIQUE ("foo"), UNIQUE ("bar"), CONSTRAINT "group" UNIQUE ("hello", "world")) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-83 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-83 new file mode 100644 index 000000000..bcc8ffb73 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-83 @@ -0,0 +1 @@ +CREATE TABLE "models" ("foo" VARCHAR, "bar" VARCHAR, "hello" VARCHAR, "world" VARCHAR, UNIQUE ("foo"), UNIQUE ("bar"), CONSTRAINT "group" UNIQUE ("hello", "world")) diff --git a/query_table_create.go b/query_table_create.go index a5fa8e231..08c68e054 100644 --- a/query_table_create.go +++ b/query_table_create.go @@ -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 "...) @@ -204,7 +210,6 @@ func (q *CreateTableQuery) appendUniqueConstraint( b = append(b, " UNIQUE ("...) b = appendColumns(b, "", fields) b = append(b, ")"...) - return b }