Skip to content

Commit

Permalink
Fix FKs duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
kurotych committed Jul 14, 2022
1 parent 4a27d29 commit c8e10b3
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions planter.go
Expand Up @@ -166,6 +166,16 @@ func LoadColumnDef(db Queryer, schema, table string) ([]*Column, error) {
return cols, nil
}

func ForeignKeyExists(fks []*ForeignKey, fk *ForeignKey) bool {
for _, k := range fks {
if k.SourceTableName == fk.SourceTableName &&
k.TargetTableName == fk.TargetTableName {
return true
}
}
return false
}

// LoadForeignKeyDef load Postgres fk definition
func LoadForeignKeyDef(db Queryer, schema string, tbls []*Table, tbl *Table) ([]*ForeignKey, error) {
fkDefs, err := db.Query(fkDefSQL, schema, tbl.Name)
Expand All @@ -189,6 +199,9 @@ func LoadForeignKeyDef(db Queryer, schema string, tbls []*Table, tbl *Table) ([]
if err != nil {
return nil, err
}
if ForeignKeyExists(fks, &fk) {
continue;
}
fks = append(fks, &fk)
}
for _, fk := range fks {
Expand Down

0 comments on commit c8e10b3

Please sign in to comment.