Skip to content

Commit

Permalink
Merge pull request #50714 from ajwerner/ajwerner/stop-populating-Drop…
Browse files Browse the repository at this point in the history
…JobID-in-truncate

sql: only set DropJobID on old table descriptor in TRUNCATE
  • Loading branch information
ajwerner authored Jun 29, 2020
2 parents 6318430 + a30972c commit 61b3971
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/sql/truncate.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ func (p *planner) truncateTable(
if err != nil {
return err
}
tableDesc.DropJobID = dropJobID
newTableDesc := sqlbase.NewMutableCreatedTableDescriptor(tableDesc.TableDescriptor)
newTableDesc.ReplacementOf = sqlbase.TableDescriptor_Replacement{
ID: id,
Expand Down Expand Up @@ -224,6 +223,7 @@ func (p *planner) truncateTable(
}

// Drop table.
tableDesc.DropJobID = dropJobID
if err := p.initiateDropTable(ctx, tableDesc, false /* drainName */); err != nil {
return err
}
Expand Down
51 changes: 51 additions & 0 deletions pkg/sql/truncate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package sql_test

import (
"context"
"testing"

"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/stretchr/testify/require"
)

// TestTruncateDoesNotSetDropJobOnNewTable ensures that the new version of a
// table after a TRUNCATE does not contain a DropJobID. This is a regression
// test for #50587.
func TestTruncateDoesNotSetDropJobOnNewTable(t *testing.T) {
defer leaktest.AfterTest(t)()

ctx := context.Background()
tc := testcluster.StartTestCluster(t, 1, base.TestClusterArgs{})
defer tc.Stopper().Stop(ctx)

tdb := sqlutils.MakeSQLRunner(tc.ServerConn(0))

getTableDescID := func(t *testing.T, tableName string) (id sqlbase.ID) {
tdb.QueryRow(t, `SELECT '`+tableName+`'::regclass::int`).Scan(&id)
return id
}

tdb.Exec(t, `CREATE TABLE foo (i INT PRIMARY KEY)`)
tdb.Exec(t, `INSERT INTO foo VALUES (1)`)
oldID := getTableDescID(t, "foo")
tdb.Exec(t, `TRUNCATE foo`)
newID := getTableDescID(t, "foo")
require.NotEqual(t, oldID, newID)
td, err := sqlbase.GetTableDescFromID(ctx, tc.Server(0).DB(), newID)
require.NoError(t, err)
require.Zero(t, td.DropJobID)
}

0 comments on commit 61b3971

Please sign in to comment.