mirror of
https://github.com/ent/ent.git
synced 2026-04-28 05:30:56 +03:00
ent/schema: allow setting collation for string fields
Reviewed By: a8m Differential Revision: D17090481 fbshipit-source-id: a08768c9bac4318a91bc6a067c85d6eb022c3024
This commit is contained in:
committed by
Facebook Github Bot
parent
419753b33b
commit
4c1f28d58f
@@ -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
|
||||
|
||||
@@ -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").
|
||||
|
||||
Reference in New Issue
Block a user