Fixed NULLs in pk on table import

This commit is contained in:
Daylon Wilkins
2020-12-09 02:04:13 -08:00
committed by Daylon Wilkins
parent 3ee6b95e98
commit 331a5a6ab5
2 changed files with 33 additions and 0 deletions
+21
View File
@@ -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,
@@ -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
@@ -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
})