mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
dialect/sql/schema: add field collation support for postgres
This commit is contained in:
committed by
Ariel Mashraki
parent
53e885cbff
commit
f7db15ccbb
@@ -284,10 +284,11 @@ func (d *MySQL) addColumn(c *Column) *sql.ColumnBuilder {
|
||||
if c.Increment {
|
||||
b.Attr("AUTO_INCREMENT")
|
||||
}
|
||||
|
||||
c.collation(b)
|
||||
c.nullable(b)
|
||||
c.defaultValue(b)
|
||||
if c.Collation != "" {
|
||||
b.Attr("COLLATE " + c.Collation)
|
||||
}
|
||||
if c.Type == field.TypeJSON {
|
||||
// Manually add a `CHECK` clause for older versions of MariaDB for validating the
|
||||
// JSON documents. This constraint is automatically included from version 10.4.3.
|
||||
|
||||
@@ -107,7 +107,7 @@ func TestMySQL_Create(t *testing.T) {
|
||||
before: func(mock mysqlMock) {
|
||||
mock.start("5.7.33")
|
||||
mock.tableExists("users", false)
|
||||
mock.ExpectExec(escape("CREATE TABLE IF NOT EXISTS `users`(`id` bigint AUTO_INCREMENT NOT NULL, `name` varchar(255) NULL, `address` varchar(255) COLLATE utf8_unicode_ci NULL, `age` bigint NOT NULL, `doc` json NULL, `enums` enum('a', 'b') NOT NULL, `uuid` char(36) binary NULL, `datetime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `decimal` decimal(6,2) NOT NULL, PRIMARY KEY(`id`)) 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, `address` varchar(255) NULL COLLATE utf8_unicode_ci, `age` bigint NOT NULL, `doc` json NULL, `enums` enum('a', 'b') NOT NULL, `uuid` char(36) binary NULL, `datetime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `decimal` decimal(6,2) NOT NULL, PRIMARY KEY(`id`)) CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB")).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
mock.ExpectCommit()
|
||||
},
|
||||
|
||||
@@ -7,6 +7,7 @@ package schema
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
@@ -359,6 +360,9 @@ func (d *Postgres) addColumn(c *Column) *sql.ColumnBuilder {
|
||||
}
|
||||
c.nullable(b)
|
||||
c.defaultValue(b)
|
||||
if c.Collation != "" {
|
||||
b.Attr("COLLATE " + strconv.Quote(c.Collation))
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
@@ -530,7 +534,6 @@ func arrayType(t string) bool {
|
||||
// foreignKeys populates the tables foreign keys using the information_schema tables
|
||||
func (d *Postgres) foreignKeys(ctx context.Context, tx dialect.Tx, tables []*Table) error {
|
||||
var tableLookup = make(map[string]*Table)
|
||||
// TODO: include schema
|
||||
for _, t := range tables {
|
||||
tableLookup[t.Name] = t
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ func TestPostgres_Create(t *testing.T) {
|
||||
},
|
||||
Columns: []*Column{
|
||||
{Name: "id", Type: field.TypeUUID, Default: "uuid_generate_v4()"},
|
||||
{Name: "name", Type: field.TypeString, Nullable: true},
|
||||
{Name: "name", Type: field.TypeString, Nullable: true, Collation: "he_IL"},
|
||||
{Name: "age", Type: field.TypeInt},
|
||||
{Name: "doc", Type: field.TypeJSON, Nullable: true},
|
||||
{Name: "enums", Type: field.TypeEnum, Enums: []string{"a", "b"}, Default: "a"},
|
||||
@@ -70,7 +70,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(), "name" varchar NULL, "age" bigint NOT NULL, "doc" jsonb NULL, "enums" varchar NOT NULL DEFAULT 'a', "price" numeric(5,2) NOT NULL, "strings" text[] NULL, PRIMARY KEY("id"))`)).
|
||||
mock.ExpectExec(escape(`CREATE TABLE IF NOT EXISTS "users"("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "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, PRIMARY KEY("id"))`)).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
mock.ExpectCommit()
|
||||
},
|
||||
|
||||
@@ -308,13 +308,6 @@ func (c Column) supportDefault() bool {
|
||||
}
|
||||
}
|
||||
|
||||
// collation adds `COLLATE collation_name`.
|
||||
func (c *Column) collation(b *sql.ColumnBuilder) {
|
||||
if c.Collation != "" {
|
||||
b.Attr("COLLATE " + c.Collation)
|
||||
}
|
||||
}
|
||||
|
||||
// unique adds the `UNIQUE` attribute if the column is a unique type.
|
||||
// it is exist in a different function to share the common declaration
|
||||
// between the two dialects.
|
||||
|
||||
Reference in New Issue
Block a user