From 7cdf44376b00525c69e9a2bbc4fe934f629a76dd Mon Sep 17 00:00:00 2001 From: Aaron L Date: Tue, 12 Jul 2016 08:09:26 -0700 Subject: [PATCH] Add even more nullable things for FKeys. --- bdb/interface.go | 26 +++++++++++--------------- bdb/interface_test.go | 41 ++++++++++++++++++++++++++++------------- bdb/keys.go | 5 +++-- 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/bdb/interface.go b/bdb/interface.go index af02bf8..3ab738d 100644 --- a/bdb/interface.go +++ b/bdb/interface.go @@ -52,11 +52,15 @@ func Tables(db Interface, names ...string) ([]Table, error) { } setIsJoinTable(&t) - setForeignKeyNullability(&t) tables = append(tables, t) } + // Relationships have a dependency on foreign key nullability. + for i := range tables { + tbl := &tables[i] + setForeignKeyNullability(tbl, tables) + } for i := range tables { tbl := &tables[i] setRelationships(tbl, tables) @@ -89,22 +93,14 @@ func setIsJoinTable(t *Table) { t.IsJoinTable = true } -func setForeignKeyNullability(t *Table) { +func setForeignKeyNullability(t *Table, tables []Table) { for i, fkey := range t.FKeys { + localColumn := t.GetColumn(fkey.Column) + foreignTable := GetTable(tables, fkey.ForeignTable) + foreignColumn := foreignTable.GetColumn(fkey.ForeignColumn) - found := -1 - for j, col := range t.Columns { - if col.Name == fkey.Column { - found = j - break - } - } - - if found < 0 { - panic("could not find foreign key column in table") - } - - t.FKeys[i].Nullable = t.Columns[found].Nullable + t.FKeys[i].Nullable = localColumn.Nullable + t.FKeys[i].ForeignColumnNullable = foreignColumn.Nullable } } diff --git a/bdb/interface_test.go b/bdb/interface_test.go index 71ea60a..d8fae8b 100644 --- a/bdb/interface_test.go +++ b/bdb/interface_test.go @@ -128,27 +128,42 @@ func TestSetIsJoinTable(t *testing.T) { func TestSetForeignKeyNullability(t *testing.T) { t.Parallel() - table := &Table{ - Columns: []Column{ - Column{Name: "col1", Type: "string"}, - Column{Name: "col2", Type: "string", Nullable: true}, - }, - FKeys: []ForeignKey{ - { - Column: "col1", + tables := []Table{ + Table{ + Name: "one", + Columns: []Column{ + Column{Name: "id1", Type: "string", Nullable: false}, + Column{Name: "id2", Type: "string", Nullable: true}, }, - { - Column: "col2", + }, + Table{ + Name: "other", + Columns: []Column{ + Column{Name: "one_id_1", Type: "string", Nullable: false}, + Column{Name: "one_id_2", Type: "string", Nullable: true}, + }, + FKeys: []ForeignKey{ + {Column: "one_id_1", ForeignTable: "one", ForeignColumn: "id1"}, + {Column: "one_id_2", ForeignTable: "one", ForeignColumn: "id2"}, }, }, } - setForeignKeyNullability(table) + setForeignKeyNullability(&tables[0], tables) + setForeignKeyNullability(&tables[1], tables) - if table.FKeys[0].Nullable { + first := tables[1].FKeys[0] + second := tables[1].FKeys[1] + if first.Nullable { t.Error("should not be nullable") } - if !table.FKeys[1].Nullable { + if first.ForeignColumnNullable { + t.Error("should be nullable") + } + if !second.Nullable { + t.Error("should be nullable") + } + if !second.ForeignColumnNullable { t.Error("should be nullable") } } diff --git a/bdb/keys.go b/bdb/keys.go index b5ff526..d3c672a 100644 --- a/bdb/keys.go +++ b/bdb/keys.go @@ -20,8 +20,9 @@ type ForeignKey struct { Column string Nullable bool - ForeignTable string - ForeignColumn string + ForeignTable string + ForeignColumn string + ForeignColumnNullable bool } // SQLColumnDef formats a column name and type like an SQL column definition.