Skip to content

Commit

Permalink
omit empties
Browse files Browse the repository at this point in the history
  • Loading branch information
covrom committed Dec 9, 2020
1 parent 5692e00 commit a3ebe34
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
8 changes: 6 additions & 2 deletions drivers/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,18 @@ ORDER BY oid`)
if err != nil {
return errors.WithStack(err)
}
rt := constraintReferenceTable.String
prt := (*string)(nil)
if constraintReferenceTable.Valid && constraintReferenceTable.String != "" {
prt = &constraintReferenceTable.String
}

constraint := &schema.Constraint{
Name: constraintName,
Type: convertConstraintType(constraintType),
Def: constraintDef,
Table: &table.Name,
Columns: arrayRemoveNull(constraintColumnNames),
ReferenceTable: &rt,
ReferenceTable: prt,
ReferenceColumns: arrayRemoveNull(constraintReferenceColumnNames),
Comment: constraintComment.String,
}
Expand Down
27 changes: 20 additions & 7 deletions schema/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ type YamlSchema struct {
type YamlTable struct {
Type string `yaml:"type,omitempty"`
Columns map[string]*YamlColumn `yaml:"columns"`
Indexes map[string]*YamlIndex `yaml:"indexes"`
Constraints map[string]*YamlConstraint `yaml:"constraints"`
Relations map[string]*YamlRelation `yaml:"relations"` // key = parent table
Indexes map[string]*YamlIndex `yaml:"indexes,omitempty"`
Constraints map[string]*YamlConstraint `yaml:"constraints,omitempty"`
Relations map[string]*YamlRelation `yaml:"relations,omitempty"` // key = parent table
Def string `yaml:"def,omitempty"`
}

Expand All @@ -31,9 +31,9 @@ type YamlConstraint struct {
Type string `yaml:"type,omitempty"`
Check string `json:"check,omitempty"`
OnDelete string `json:"onDelete,omitempty"`
ReferenceTable *string `yaml:"referenceTable,omitempty"`
ReferenceTable string `yaml:"referenceTable,omitempty"`
Columns []string `yaml:"columns,flow"`
ReferenceColumns []string `yaml:"referenceColumns,flow"`
ReferenceColumns []string `yaml:"referenceColumns,flow,omitempty"`
}

type YamlIndex struct {
Expand Down Expand Up @@ -112,14 +112,27 @@ func (s *Schema) MarshalYAML() ([]byte, error) {
}
}
for _, cs := range t.Constraints {
yt.Constraints[cs.Name] = &YamlConstraint{
if cs.Type == TypePK {
// present as 'pk: true' in columns
continue
}
if cs.Type == TypeFK {
// present in relations
continue
}
ycs := &YamlConstraint{
Type: cs.Type,
Check: cs.Check,
OnDelete: cs.OnDelete,
ReferenceTable: cs.ReferenceTable,
Columns: cs.Columns,
ReferenceColumns: cs.ReferenceColumns,
}
if cs.ReferenceTable != nil {
ycs.ReferenceTable = *cs.ReferenceTable
} else {
ycs.ReferenceColumns = nil
}
yt.Constraints[cs.Name] = ycs
}
for _, r := range s.Relations {
if r.Table.Name != t.Name {
Expand Down

0 comments on commit a3ebe34

Please sign in to comment.