From 104d0e57cdd3f149b1c1f5704ff7379c5e92c3be Mon Sep 17 00:00:00 2001 From: Aaron L Date: Sat, 9 Jul 2016 09:53:21 -0700 Subject: [PATCH] Add nullability to foreign keys. - Remove some debug output. - Add some parallel --- bdb/column_test.go | 2 ++ bdb/interface.go | 20 ++++++++++++++++++++ bdb/interface_test.go | 33 +++++++++++++++++++++++++++++++-- bdb/keys.go | 5 +++-- bdb/relationships_test.go | 7 +------ 5 files changed, 57 insertions(+), 10 deletions(-) diff --git a/bdb/column_test.go b/bdb/column_test.go index 567baad..bf8498c 100644 --- a/bdb/column_test.go +++ b/bdb/column_test.go @@ -53,6 +53,8 @@ func TestFilterColumnsByDefault(t *testing.T) { } func TestDefaultValues(t *testing.T) { + t.Parallel() + c := Column{} c.Default = `\x12345678` diff --git a/bdb/interface.go b/bdb/interface.go index cc96b14..d3895ba 100644 --- a/bdb/interface.go +++ b/bdb/interface.go @@ -52,6 +52,7 @@ func Tables(db Interface, names ...string) ([]Table, error) { } setIsJoinTable(&t) + setForeignKeyNullability(&t) tables = append(tables, t) } @@ -82,3 +83,22 @@ func setIsJoinTable(t *Table) { t.IsJoinTable = true } + +func setForeignKeyNullability(t *Table) { + for i, fkey := range t.FKeys { + + 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].IsNullable + } +} diff --git a/bdb/interface_test.go b/bdb/interface_test.go index c71d6c8..1a5c4a3 100644 --- a/bdb/interface_test.go +++ b/bdb/interface_test.go @@ -14,7 +14,7 @@ func (t testInterface) TableNames() ([]string, error) { func (t testInterface) Columns(tableName string) ([]Column, error) { return []Column{ Column{Name: "col1", Type: "character varying"}, - Column{Name: "col2", Type: "character varying"}, + Column{Name: "col2", Type: "character varying", IsNullable: true}, }, nil } @@ -64,7 +64,7 @@ func TestTables(t *testing.T) { expectCols := []Column{ Column{Name: "col1", Type: "string"}, - Column{Name: "col2", Type: "string"}, + Column{Name: "col2", Type: "string", IsNullable: true}, } if !reflect.DeepEqual(tables[0].Columns, expectCols) { @@ -88,6 +88,7 @@ func TestTables(t *testing.T) { Column: "col2", ForeignTable: "table3", ForeignColumn: "col3", + Nullable: true, }, } @@ -132,3 +133,31 @@ 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", IsNullable: true}, + }, + FKeys: []ForeignKey{ + { + Column: "col1", + }, + { + Column: "col2", + }, + }, + } + + setForeignKeyNullability(table) + + if table.FKeys[0].Nullable { + t.Error("should not be nullable") + } + if !table.FKeys[1].Nullable { + t.Error("should be nullable") + } +} diff --git a/bdb/keys.go b/bdb/keys.go index 8729f70..c8a3346 100644 --- a/bdb/keys.go +++ b/bdb/keys.go @@ -16,8 +16,9 @@ type PrimaryKey struct { // ForeignKey represents a foreign key constraint in a database type ForeignKey struct { - Name string - Column string + Name string + Column string + Nullable bool ForeignTable string ForeignColumn string diff --git a/bdb/relationships_test.go b/bdb/relationships_test.go index f510d87..1c0ca3b 100644 --- a/bdb/relationships_test.go +++ b/bdb/relationships_test.go @@ -1,10 +1,6 @@ package bdb -import ( - "testing" - - "github.com/davecgh/go-spew/spew" -) +import "testing" func TestToManyRelationships(t *testing.T) { t.Parallel() @@ -35,7 +31,6 @@ func TestToManyRelationships(t *testing.T) { } relationships := ToManyRelationships("users", tables) - spew.Dump(relationships) if len(relationships) != 4 { t.Error("wrong # of relationships:", len(relationships)) }