Skip to content
This repository has been archived by the owner on Aug 21, 2020. It is now read-only.

Commit

Permalink
Reduced complexity of getTags for Postgres migrator.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kugelschieber committed Mar 22, 2019
1 parent 6b74e53 commit f40da23
Showing 1 changed file with 42 additions and 39 deletions.
81 changes: 42 additions & 39 deletions postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,51 +509,54 @@ func (m *Postgres) getTags(modelName string, field *MetaField) string {
for _, tag := range field.Tags {
key := strings.ToLower(tag.Name)
value := strings.ToLower(tag.Value)
m.buildTag(tags, modelName, key, value, field, tag)
}

if key == "type" {
tags[0] = tag.Value
} else if key == "default" {
tags[1] = "DEFAULT "
return strings.Join(tags, " ")
}

if value == "nextval(seq)" {
tags[1] += "nextval('" + m.getSequenceName(modelName, field.Name) + "'::regclass)"
} else {
tags[1] += value
}
} else if value == "notnull" || value == "not null" {
tags[2] = "NOT NULL"
} else if value == "null" {
tags[2] = "NULL"
} else if key == "seq" || key == "sequence" {
m.addSequence(modelName, field.Name, value)
} else if value == "id" {
// id is a shortcut for seq + default + pk
m.addSequence(modelName, field.Name, "1,1,-,-,1")
tags[1] = "DEFAULT nextval('" + m.getSequenceName(modelName, field.Name) + "'::regclass)"
tags[3] = "PRIMARY KEY"
m.alterPrimaryKey(modelName, field.Name)
} else if value == "pk" || value == "primary key" {
tags[3] = "PRIMARY KEY"
m.alterPrimaryKey(modelName, field.Name)
} else if value == "unique" {
tags[4] = "UNIQUE"
} else if key == "fk" || key == "foreign key" {
// value must be case sensitive here
m.addForeignKey(modelName, field.Name, tag.Value)
} else {
name := ""
func (m *Postgres) buildTag(tags []string, modelName, key, value string, field *MetaField, tag MetaTag) {
if key == "type" {
tags[0] = tag.Value
} else if key == "default" {
tags[1] = "DEFAULT "

if key == "" {
name = value
} else {
name = key + ":" + value
}
if value == "nextval(seq)" {
tags[1] += "nextval('" + m.getSequenceName(modelName, field.Name) + "'::regclass)"
} else {
tags[1] += value
}
} else if value == "notnull" || value == "not null" {
tags[2] = "NOT NULL"
} else if value == "null" {
tags[2] = "NULL"
} else if key == "seq" || key == "sequence" {
m.addSequence(modelName, field.Name, value)
} else if value == "id" {
// id is a shortcut for seq + default + pk
m.addSequence(modelName, field.Name, "1,1,-,-,1")
tags[1] = "DEFAULT nextval('" + m.getSequenceName(modelName, field.Name) + "'::regclass)"
tags[3] = "PRIMARY KEY"
m.alterPrimaryKey(modelName, field.Name)
} else if value == "pk" || value == "primary key" {
tags[3] = "PRIMARY KEY"
m.alterPrimaryKey(modelName, field.Name)
} else if value == "unique" {
tags[4] = "UNIQUE"
} else if key == "fk" || key == "foreign key" {
// value must be case sensitive here
m.addForeignKey(modelName, field.Name, tag.Value)
} else {
name := ""

panic("Unknown tag '" + name + "' for model '" + modelName + "'")
if key == "" {
name = value
} else {
name = key + ":" + value
}
}

return strings.Join(tags, " ")
panic("Unknown tag '" + name + "' for model '" + modelName + "'")
}
}

func (m *Postgres) addSequence(modelName, columnName, info string) {
Expand Down

0 comments on commit f40da23

Please sign in to comment.