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) {
|
func TestDefaultValues(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
c := Column{}
|
c := Column{}
|
||||||
|
|
||||||
c.Default = `\x12345678`
|
c.Default = `\x12345678`
|
||||||
|
|
|
@ -52,6 +52,7 @@ func Tables(db Interface, names ...string) ([]Table, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
setIsJoinTable(&t)
|
setIsJoinTable(&t)
|
||||||
|
setForeignKeyNullability(&t)
|
||||||
|
|
||||||
tables = append(tables, t)
|
tables = append(tables, t)
|
||||||
}
|
}
|
||||||
|
@ -82,3 +83,22 @@ func setIsJoinTable(t *Table) {
|
||||||
|
|
||||||
t.IsJoinTable = true
|
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) {
|
func (t testInterface) Columns(tableName string) ([]Column, error) {
|
||||||
return []Column{
|
return []Column{
|
||||||
Column{Name: "col1", Type: "character varying"},
|
Column{Name: "col1", Type: "character varying"},
|
||||||
Column{Name: "col2", Type: "character varying"},
|
Column{Name: "col2", Type: "character varying", IsNullable: true},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ func TestTables(t *testing.T) {
|
||||||
|
|
||||||
expectCols := []Column{
|
expectCols := []Column{
|
||||||
Column{Name: "col1", Type: "string"},
|
Column{Name: "col1", Type: "string"},
|
||||||
Column{Name: "col2", Type: "string"},
|
Column{Name: "col2", Type: "string", IsNullable: true},
|
||||||
}
|
}
|
||||||
|
|
||||||
if !reflect.DeepEqual(tables[0].Columns, expectCols) {
|
if !reflect.DeepEqual(tables[0].Columns, expectCols) {
|
||||||
|
@ -88,6 +88,7 @@ func TestTables(t *testing.T) {
|
||||||
Column: "col2",
|
Column: "col2",
|
||||||
ForeignTable: "table3",
|
ForeignTable: "table3",
|
||||||
ForeignColumn: "col3",
|
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
|
// ForeignKey represents a foreign key constraint in a database
|
||||||
type ForeignKey struct {
|
type ForeignKey struct {
|
||||||
Name string
|
Name string
|
||||||
Column string
|
Column string
|
||||||
|
Nullable bool
|
||||||
|
|
||||||
ForeignTable string
|
ForeignTable string
|
||||||
ForeignColumn string
|
ForeignColumn string
|
||||||
|
|
|
@ -1,10 +1,6 @@
|
||||||
package bdb
|
package bdb
|
||||||
|
|
||||||
import (
|
import "testing"
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestToManyRelationships(t *testing.T) {
|
func TestToManyRelationships(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
@ -35,7 +31,6 @@ func TestToManyRelationships(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
relationships := ToManyRelationships("users", tables)
|
relationships := ToManyRelationships("users", tables)
|
||||||
spew.Dump(relationships)
|
|
||||||
if len(relationships) != 4 {
|
if len(relationships) != 4 {
|
||||||
t.Error("wrong # of relationships:", len(relationships))
|
t.Error("wrong # of relationships:", len(relationships))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue