Skip to content

Commit

Permalink
Fixed NULLs in pk on table import
Browse files Browse the repository at this point in the history
  • Loading branch information
Hydrocharged committed Dec 11, 2020
1 parent 3ee6b95 commit 331a5a6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
21 changes: 21 additions & 0 deletions bats/import-create-tables.bats
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ DELIM
"four":"c3"
}
JSON

cat <<DELIM > name-map-data.csv
one,two,three,four
0,1,2,3
DELIM

cat <<SQL > name-map-sch.sql
CREATE TABLE test (
pk int not null,
Expand Down Expand Up @@ -459,6 +461,25 @@ SQL
[ "$status" -eq 1 ]
}

@test "fail on import table creation when defined pk has a NULL value" {
cat <<DELIM > null-pk-1.csv
pk,v1
"a",1
,2
DELIM
cat <<DELIM > null-pk-2.csv
pk1,pk2,v1
0,0,0
1,,1
DELIM
run dolt table import -c --pk=pk test null-pk-1.csv
[ "$status" -eq 1 ]
[[ "$output" =~ "pk" ]]
run dolt table import -c --pk=pk1,pk2 test null-pk-2.csv
[ "$status" -eq 1 ]
[[ "$output" =~ "pk2" ]]
}

@test "table import -c infers types from data" {
cat <<DELIM > types.csv
pk,str,int,bool,float, date, time, datetime
Expand Down
12 changes: 12 additions & 0 deletions go/libraries/doltcore/mvdata/data_mover.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,18 @@ func InferSchema(ctx context.Context, root *doltdb.RootValue, rd table.TableRead
pkSet := set.NewStrSet(pks)
newCols, _ := schema.MapColCollection(infCols, func(col schema.Column) (schema.Column, error) {
col.IsPartOfPK = pkSet.Contains(col.Name)
if col.IsPartOfPK {
hasNotNull := false
for _, constraint := range col.Constraints {
if _, ok := constraint.(schema.NotNullConstraint); ok {
hasNotNull = true
break
}
}
if !hasNotNull {
col.Constraints = append(col.Constraints, schema.NotNullConstraint{})
}
}
return col, nil
})

Expand Down

0 comments on commit 331a5a6

Please sign in to comment.