Commit 114fc201 authored by Amos Wenger's avatar Amos Wenger

automigrate: support 'hades:squash'

parent 056d011f
Pipeline #10977 passed with stage
in 1 minute and 16 seconds
......@@ -48,7 +48,7 @@ func (c *Context) syncTable(conn *sqlite.Conn, ms *ModelStruct) (err error) {
numNewCols := 0
isMissingCols := false
for _, sf := range ms.StructFields {
if sf.Relationship != nil {
if !sf.IsNormal {
continue
}
numNewCols++
......@@ -121,9 +121,20 @@ func (c *Context) createTable(conn *sqlite.Conn, ms *ModelStruct) error {
query := fmt.Sprintf("CREATE TABLE %s", EscapeIdentifier(ms.TableName))
var columns []string
var pks []string
for _, sf := range ms.StructFields {
var processField func(sf *StructField) error
processField = func(sf *StructField) error {
if sf.IsSquashed {
for _, nsf := range sf.SquashedFields {
err := processField(nsf)
if err != nil {
return err
}
}
}
if !sf.IsNormal {
continue
return nil
}
var sqliteType string
......@@ -158,6 +169,14 @@ func (c *Context) createTable(conn *sqlite.Conn, ms *ModelStruct) error {
}
column := fmt.Sprintf(`%s %s%s`, EscapeIdentifier(sf.DBName), sqliteType, modifier)
columns = append(columns, column)
return nil
}
for _, sf := range ms.StructFields {
err := processField(sf)
if err != nil {
return err
}
}
if len(pks) > 0 {
......
......@@ -197,6 +197,65 @@ func Test_AutoMigrateAllValidTypes(t *testing.T) {
assert.EqualValues(t, h1.HeartRate, h2.HeartRate)
}
func Test_AutoMigrateSquash(t *testing.T) {
dbpool, err := sqlite.Open("file:memory:?mode=memory", 0, 10)
ordie(err)
defer dbpool.Close()
conn := dbpool.Get(context.Background().Done())
defer dbpool.Put(conn)
type AndroidTraits struct {
Funny bool
Wise bool
Fair bool
}
type Android struct {
ID int64
Title string
Traits AndroidTraits `hades:"squash"`
}
models := []interface{}{&Android{}}
c, err := hades.NewContext(makeConsumer(t), models...)
ordie(err)
c.Log = true
ordie(c.AutoMigrate(conn))
pti, err := c.PragmaTableInfo(conn, "androids")
ordie(err)
assert.EqualValues(t, 5, len(pti))
assert.EqualValues(t, "id", pti[0].Name)
assert.EqualValues(t, "INTEGER", pti[0].Type)
assert.True(t, pti[0].PrimaryKey)
assert.True(t, pti[0].NotNull)
assert.EqualValues(t, "title", pti[1].Name)
assert.EqualValues(t, "TEXT", pti[1].Type)
assert.False(t, pti[1].PrimaryKey)
assert.False(t, pti[1].NotNull)
assert.EqualValues(t, "funny", pti[2].Name)
assert.EqualValues(t, "BOOLEAN", pti[2].Type)
assert.False(t, pti[2].PrimaryKey)
assert.False(t, pti[2].NotNull)
assert.EqualValues(t, "wise", pti[3].Name)
assert.EqualValues(t, "BOOLEAN", pti[3].Type)
assert.False(t, pti[3].PrimaryKey)
assert.False(t, pti[3].NotNull)
assert.EqualValues(t, "fair", pti[4].Name)
assert.EqualValues(t, "BOOLEAN", pti[4].Type)
assert.False(t, pti[4].PrimaryKey)
assert.False(t, pti[4].NotNull)
}
func ordie(err error) {
if err != nil {
panic(err)
......
......@@ -45,18 +45,20 @@ type ModelStruct struct {
// StructField model field's struct definition
type StructField struct {
DBName string
Name string
Names []string
IsPrimaryKey bool
IsNormal bool
IsIgnored bool
IsScanner bool
Tag reflect.StructTag
TagSettings map[string]string
Struct reflect.StructField
IsForeignKey bool
Relationship *Relationship
DBName string
Name string
Names []string
IsPrimaryKey bool
IsNormal bool
IsIgnored bool
IsScanner bool
IsSquashed bool
SquashedFields []*StructField
Tag reflect.StructTag
TagSettings map[string]string
Struct reflect.StructField
IsForeignKey bool
Relationship *Relationship
}
// Relationship described the relationship between models
......@@ -123,6 +125,12 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
// is ignored field
if _, ok := field.TagSettings["-"]; ok {
field.IsIgnored = true
} else if _, ok := field.TagSettings["SQUASH"]; ok {
field.IsSquashed = true
nestedModelStruct := scope.ctx.NewScope(reflect.Zero(field.Struct.Type).Interface()).GetModelStruct()
for _, sf := range nestedModelStruct.StructFields {
field.SquashedFields = append(field.SquashedFields, sf)
}
} else {
if _, ok := field.TagSettings["PRIMARY_KEY"]; ok {
field.IsPrimaryKey = true
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment