Expose more information about join tables
This commit is contained in:
parent
d154617e95
commit
59c7496521
2 changed files with 106 additions and 13 deletions
|
@ -7,7 +7,11 @@ type ToManyRelationship struct {
|
||||||
Column string
|
Column string
|
||||||
ForeignTable string
|
ForeignTable string
|
||||||
ForeignColumn string
|
ForeignColumn string
|
||||||
ToJoinTable bool
|
|
||||||
|
ToJoinTable bool
|
||||||
|
JoinTable string
|
||||||
|
JoinLocalColumn string
|
||||||
|
JoinForeignColumn string
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToManyRelationships relationship lookups
|
// ToManyRelationships relationship lookups
|
||||||
|
@ -25,16 +29,38 @@ func ToManyRelationships(table string, tables []Table) []ToManyRelationship {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
relationship := ToManyRelationship{
|
relationships = append(relationships, buildRelationship(table, f, t))
|
||||||
Column: f.ForeignColumn,
|
|
||||||
ForeignTable: t.Name,
|
|
||||||
ForeignColumn: f.Column,
|
|
||||||
ToJoinTable: t.IsJoinTable,
|
|
||||||
}
|
|
||||||
|
|
||||||
relationships = append(relationships, relationship)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return relationships
|
return relationships
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func buildRelationship(localTable string, foreignKey ForeignKey, foreignTable Table) ToManyRelationship {
|
||||||
|
if !foreignTable.IsJoinTable {
|
||||||
|
return ToManyRelationship{
|
||||||
|
Column: foreignKey.ForeignColumn,
|
||||||
|
ForeignTable: foreignTable.Name,
|
||||||
|
ForeignColumn: foreignKey.Column,
|
||||||
|
ToJoinTable: foreignTable.IsJoinTable,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
relationship := ToManyRelationship{
|
||||||
|
Column: foreignKey.ForeignColumn,
|
||||||
|
ToJoinTable: true,
|
||||||
|
JoinTable: foreignTable.Name,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, fk := range foreignTable.FKeys {
|
||||||
|
if fk.ForeignTable != localTable {
|
||||||
|
relationship.JoinForeignColumn = fk.Column
|
||||||
|
relationship.ForeignTable = fk.ForeignTable
|
||||||
|
relationship.ForeignColumn = fk.ForeignColumn
|
||||||
|
} else {
|
||||||
|
relationship.JoinLocalColumn = fk.Column
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return relationship
|
||||||
|
}
|
||||||
|
|
|
@ -1,14 +1,17 @@
|
||||||
package bdb
|
package bdb
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
)
|
||||||
|
|
||||||
func TestToManyRelationships(t *testing.T) {
|
func TestToManyRelationships(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
tables := []Table{
|
tables := []Table{
|
||||||
Table{
|
Table{
|
||||||
Name: "videos",
|
Name: "videos",
|
||||||
IsJoinTable: true,
|
|
||||||
FKeys: []ForeignKey{
|
FKeys: []ForeignKey{
|
||||||
{Name: "videos_user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
|
{Name: "videos_user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
|
||||||
{Name: "videos_contest_id_fk", Column: "contest_id", ForeignTable: "contests", ForeignColumn: "id"},
|
{Name: "videos_contest_id_fk", Column: "contest_id", ForeignTable: "contests", ForeignColumn: "id"},
|
||||||
|
@ -21,9 +24,22 @@ func TestToManyRelationships(t *testing.T) {
|
||||||
{Name: "notifications_source_id_fk", Column: "source_id", ForeignTable: "users", ForeignColumn: "id"},
|
{Name: "notifications_source_id_fk", Column: "source_id", ForeignTable: "users", ForeignColumn: "id"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Table{
|
||||||
|
Name: "users_video_tags",
|
||||||
|
IsJoinTable: true,
|
||||||
|
FKeys: []ForeignKey{
|
||||||
|
{Name: "user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
|
||||||
|
{Name: "video_id_fk", Column: "video_id", ForeignTable: "videos", ForeignColumn: "id"},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
relationships := ToManyRelationships("users", tables)
|
relationships := ToManyRelationships("users", tables)
|
||||||
|
spew.Dump(relationships)
|
||||||
|
if len(relationships) != 4 {
|
||||||
|
t.Error("wrong # of relationships:", len(relationships))
|
||||||
|
}
|
||||||
|
|
||||||
r := relationships[0]
|
r := relationships[0]
|
||||||
if r.Column != "id" {
|
if r.Column != "id" {
|
||||||
t.Error("wrong local column:", r.Column)
|
t.Error("wrong local column:", r.Column)
|
||||||
|
@ -34,7 +50,58 @@ func TestToManyRelationships(t *testing.T) {
|
||||||
if r.ForeignColumn != "user_id" {
|
if r.ForeignColumn != "user_id" {
|
||||||
t.Error("wrong foreign column:", r.ForeignColumn)
|
t.Error("wrong foreign column:", r.ForeignColumn)
|
||||||
}
|
}
|
||||||
|
if r.ToJoinTable {
|
||||||
|
t.Error("not a join table")
|
||||||
|
}
|
||||||
|
|
||||||
|
r = relationships[1]
|
||||||
|
if r.Column != "id" {
|
||||||
|
t.Error("wrong local column:", r.Column)
|
||||||
|
}
|
||||||
|
if r.ForeignTable != "notifications" {
|
||||||
|
t.Error("wrong foreign table:", r.ForeignTable)
|
||||||
|
}
|
||||||
|
if r.ForeignColumn != "user_id" {
|
||||||
|
t.Error("wrong foreign column:", r.ForeignColumn)
|
||||||
|
}
|
||||||
|
if r.ToJoinTable {
|
||||||
|
t.Error("not a join table")
|
||||||
|
}
|
||||||
|
|
||||||
|
r = relationships[2]
|
||||||
|
if r.Column != "id" {
|
||||||
|
t.Error("wrong local column:", r.Column)
|
||||||
|
}
|
||||||
|
if r.ForeignTable != "notifications" {
|
||||||
|
t.Error("wrong foreign table:", r.ForeignTable)
|
||||||
|
}
|
||||||
|
if r.ForeignColumn != "source_id" {
|
||||||
|
t.Error("wrong foreign column:", r.ForeignColumn)
|
||||||
|
}
|
||||||
|
if r.ToJoinTable {
|
||||||
|
t.Error("not a join table")
|
||||||
|
}
|
||||||
|
|
||||||
|
r = relationships[3]
|
||||||
|
if r.Column != "id" {
|
||||||
|
t.Error("wrong local column:", r.Column)
|
||||||
|
}
|
||||||
|
if r.ForeignColumn != "id" {
|
||||||
|
t.Error("wrong foreign column:", r.Column)
|
||||||
|
}
|
||||||
|
if r.ForeignTable != "videos" {
|
||||||
|
t.Error("wrong foreign table:", r.ForeignTable)
|
||||||
|
}
|
||||||
|
if r.JoinTable != "users_video_tags" {
|
||||||
|
t.Error("wrong join table:", r.ForeignTable)
|
||||||
|
}
|
||||||
|
if r.JoinLocalColumn != "user_id" {
|
||||||
|
t.Error("wrong local join column:", r.JoinLocalColumn)
|
||||||
|
}
|
||||||
|
if r.JoinForeignColumn != "video_id" {
|
||||||
|
t.Error("wrong foreign join column:", r.JoinForeignColumn)
|
||||||
|
}
|
||||||
if !r.ToJoinTable {
|
if !r.ToJoinTable {
|
||||||
t.Error("expected a join table - kind of - not really but we're faking it")
|
t.Error("expected a join table")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue