ent/schema: allow setting collation for string fields

Reviewed By: a8m

Differential Revision: D17090481

fbshipit-source-id: a08768c9bac4318a91bc6a067c85d6eb022c3024
This commit is contained in:
Alex Snast
2019-08-28 04:51:27 -07:00
committed by Facebook Github Bot
parent 419753b33b
commit 4c1f28d58f
57 changed files with 955 additions and 155 deletions

View File

@@ -118,6 +118,7 @@ type Descriptor struct {
Name string // field name.
Type Type // field type.
Charset string // string charset.
Collation string // string collation.
Unique bool // unique index of field.
Nillable bool // nillable struct field.
Optional bool // nullable field in database.
@@ -244,13 +245,20 @@ func (b *stringBuilder) StructTag(s string) *stringBuilder {
return b
}
// SetCharset sets the character set attribute for character fields.
// Charset sets the character set attribute for character fields.
// For example, utf8 or utf8mb4 in MySQL.
func (b *stringBuilder) Charset(s string) *stringBuilder {
b.desc.Charset = s
return b
}
// Collation sets the collation attribute for character fields.
// For example, utf8_general_ci or utf8mb4_bin in MySQL.
func (b *stringBuilder) Collation(c string) *stringBuilder {
b.desc.Collation = c
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *stringBuilder) Descriptor() *Descriptor {
return b.desc

View File

@@ -88,6 +88,13 @@ func TestCharset(t *testing.T) {
assert.Equal(t, "utf8", fd.Charset)
}
func TestCollation(t *testing.T) {
fd := field.String("name").
Collation("utf8_general_ci").
Descriptor()
assert.Equal(t, "utf8_general_ci", fd.Collation)
}
func TestTime(t *testing.T) {
now := time.Now()
fd := field.Time("created_at").