diff --git a/.gitignore b/.gitignore index 1328b5cfd6..28e71e2a0f 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ venv benchmark/perf_tools/dolt-builds/dolt benchmark/perf_tools/dolt-builds/working benchmark/perf_tools/output + +test.sh \ No newline at end of file diff --git a/go/libraries/doltcore/schema/alterschema/addpk.go b/go/libraries/doltcore/schema/alterschema/addpk.go index f8478ac968..014d80d10a 100644 --- a/go/libraries/doltcore/schema/alterschema/addpk.go +++ b/go/libraries/doltcore/schema/alterschema/addpk.go @@ -52,28 +52,29 @@ func AddPrimaryKeyToTable(ctx context.Context, table *doltdb.Table, tableName st return col }) - // Get Row Data out of Table - rowData, err := table.GetRowData(ctx) - if err != nil { - return nil, err - } - // Go through columns of new table err = newCollection.Iter(func(tag uint64, col schema.Column) (stop bool, err error) { - // Skip if they are not going to be part if primary key + // Skip if they are not part of primary key if !col.IsPartOfPK { return false, nil } + // Get Row Data out of Table + rowData, err := table.GetRowData(ctx) + if err != nil { + return true, err + } + // Go through every row err = rowData.Iter(ctx, func(key, value types.Value) (stop bool, err error) { r, err := row.FromNoms(sch, key.(types.Tuple), value.(types.Tuple)) if err != nil { return false, err } + // Check if column value is null val, ok := r.GetColVal(tag) if !ok || val == nil || val == types.NullValue { - return true, fmt.Errorf("cannot change column to NOT NULL when one or more values is NULL") + return true, fmt.Errorf("primary key cannot have NULL values") } return false, nil }) diff --git a/integration-tests/bats/primary-key-changes.bats b/integration-tests/bats/primary-key-changes.bats index f8bce650f5..ca0db7cf06 100644 --- a/integration-tests/bats/primary-key-changes.bats +++ b/integration-tests/bats/primary-key-changes.bats @@ -10,6 +10,13 @@ teardown() { teardown_common } +@test "primary-key-changes: add primary key using null values" { + dolt sql -q "create table t(pk int, val int)" + dolt sql -q "INSERT INTO t (val) VALUES (1)" + run dolt sql -q "ALTER TABLE t ADD PRIMARY KEY (pk)" + [ "$status" -eq 1 ] +} + @test "primary-key-changes: add single primary key" { dolt sql -q "create table t(pk int, val int)" run dolt sql -q "ALTER TABLE t ADD PRIMARY KEY (pk)" @@ -573,11 +580,5 @@ SQL dolt sql -q "create table t (pk int, c1 int)" dolt sql -q "insert into t values (NULL, NULL)" run dolt sql -q "alter table t add primary key(pk)" - skip "This should fail on some sort of constraint error" [ $status -eq 1 ] - - # This is the current failure mode - run dolt sql -q "update t set c1=1" - [ $status -eq 1 ] - [[ "$output" =~ "received nil" ]] || false }