Add nullability to foreign keys.
- Remove some debug output. - Add some parallel
This commit is contained in:
parent
7863284e61
commit
104d0e57cd
5 changed files with 57 additions and 10 deletions
|
@ -53,6 +53,8 @@ func TestFilterColumnsByDefault(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestDefaultValues(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
c := Column{}
|
||||
|
||||
c.Default = `\x12345678`
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue