dialect/sql/schema: fix bug when using multiple table CHECK constraints (#2188)

Fixed https://github.com/ent/ent/issues/2185
This commit is contained in:
Ariel Mashraki
2021-12-01 12:00:06 +02:00
committed by GitHub
parent d432d880c7
commit 475e237937
3 changed files with 5 additions and 2 deletions

View File

@@ -70,6 +70,7 @@ func TestMySQL_Create(t *testing.T) {
Options: "ENGINE = INNODB",
Check: "price > 0",
Checks: map[string]string{
"valid_age": "age > 0",
"valid_name": "name <> ''",
},
},
@@ -78,7 +79,7 @@ func TestMySQL_Create(t *testing.T) {
before: func(mock mysqlMock) {
mock.start("5.7.8")
mock.tableExists("users", false)
mock.ExpectExec(escape("CREATE TABLE IF NOT EXISTS `users`(`id` bigint AUTO_INCREMENT NOT NULL, `name` varchar(255) NULL, `age` bigint NOT NULL, `doc` json NULL, `enums` enum('a', 'b') NOT NULL, `uuid` char(36) binary NULL, `ts` timestamp NULL, `ts_default` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `datetime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `decimal` decimal(6,2) NOT NULL, `unsigned decimal` decimal(6,2) unsigned NOT NULL, PRIMARY KEY(`id`), CHECK (price > 0), CONSTRAINT `valid_name` CHECK (name <> '')) CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB")).
mock.ExpectExec(escape("CREATE TABLE IF NOT EXISTS `users`(`id` bigint AUTO_INCREMENT NOT NULL, `name` varchar(255) NULL, `age` bigint NOT NULL, `doc` json NULL, `enums` enum('a', 'b') NOT NULL, `uuid` char(36) binary NULL, `ts` timestamp NULL, `ts_default` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `datetime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `decimal` decimal(6,2) NOT NULL, `unsigned decimal` decimal(6,2) unsigned NOT NULL, PRIMARY KEY(`id`), CHECK (price > 0), CONSTRAINT `valid_age` CHECK (age > 0), CONSTRAINT `valid_name` CHECK (name <> '')) CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB")).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectCommit()
},

View File

@@ -71,6 +71,7 @@ func TestPostgres_Create(t *testing.T) {
Annotation: &entsql.Annotation{
Check: "price > 0",
Checks: map[string]string{
"valid_age": "age > 0",
"valid_name": "name <> ''",
},
},
@@ -79,7 +80,7 @@ func TestPostgres_Create(t *testing.T) {
before: func(mock pgMock) {
mock.start("120000")
mock.tableExists("users", false)
mock.ExpectExec(escape(`CREATE TABLE IF NOT EXISTS "users"("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "block_size" bigint NOT NULL DEFAULT current_setting('block_size')::bigint, "name" varchar NULL COLLATE "he_IL", "age" bigint NOT NULL, "doc" jsonb NULL, "enums" varchar NOT NULL DEFAULT 'a', "price" numeric(5,2) NOT NULL, "strings" text[] NULL, "fixed_string" varchar(100) NOT NULL, PRIMARY KEY("id"), CHECK (price > 0), CONSTRAINT "valid_name" CHECK (name <> ''))`)).
mock.ExpectExec(escape(`CREATE TABLE IF NOT EXISTS "users"("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "block_size" bigint NOT NULL DEFAULT current_setting('block_size')::bigint, "name" varchar NULL COLLATE "he_IL", "age" bigint NOT NULL, "doc" jsonb NULL, "enums" varchar NOT NULL DEFAULT 'a', "price" numeric(5,2) NOT NULL, "strings" text[] NULL, "fixed_string" varchar(100) NOT NULL, PRIMARY KEY("id"), CHECK (price > 0), CONSTRAINT "valid_age" CHECK (age > 0), CONSTRAINT "valid_name" CHECK (name <> ''))`)).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectCommit()
},

View File

@@ -558,6 +558,7 @@ func addChecks(t *sql.TableBuilder, ant *entsql.Annotation) {
}
sort.Strings(names)
for _, name := range names {
name := name
t.Checks(func(b *sql.Builder) {
b.WriteString("CONSTRAINT ").Ident(name).WriteString(" CHECK " + checkExpr(checks[name]))
})