Skip to content

Commit aef255a

Browse files
craig[bot]jeffswenson
andcommitted
Merge #158385
158385: sql: add random schema test for revert r=jeffswenson a=jeffswenson This adds a random schema test for revert to improve test coverage. Release Note: none Part of: #137924 Co-authored-by: Jeff Swenson <jeffswenson@betterthannull.com>
2 parents b467894 + e9ad663 commit aef255a

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

pkg/sql/revert_test.go

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@ package sql_test
77

88
import (
99
"context"
10+
"fmt"
1011
"testing"
1112

1213
"github.com/cockroachdb/cockroach/pkg/base"
1314
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
1415
"github.com/cockroachdb/cockroach/pkg/sql"
1516
"github.com/cockroachdb/cockroach/pkg/sql/catalog/desctestutils"
17+
"github.com/cockroachdb/cockroach/pkg/sql/randgen"
18+
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
1619
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
1720
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
1821
"github.com/cockroachdb/cockroach/pkg/util/hlc"
1922
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
2023
"github.com/cockroachdb/cockroach/pkg/util/log"
24+
"github.com/cockroachdb/cockroach/pkg/util/randutil"
2125
"github.com/stretchr/testify/require"
2226
)
2327

@@ -28,7 +32,7 @@ func TestTableRollback(t *testing.T) {
2832
s, sqlDB, kv := serverutils.StartServer(t, base.TestServerArgs{UseDatabase: "test"})
2933
defer s.Stopper().Stop(context.Background())
3034
tt := s.ApplicationLayer()
31-
codec, sv := tt.Codec(), &tt.ClusterSettings().SV
35+
codec := tt.Codec()
3236
execCfg := tt.ExecutorConfig().(sql.ExecutorConfig)
3337

3438
db := sqlutils.MakeSQLRunner(sqlDB)
@@ -61,8 +65,55 @@ func TestTableRollback(t *testing.T) {
6165

6266
predicates := kvpb.DeleteRangePredicates{StartTime: targetTime}
6367
require.NoError(t, sql.DeleteTableWithPredicate(
64-
ctx, kv, codec, sv, execCfg.DistSender, desc.GetID(), predicates, 10))
68+
ctx, kv, codec, &tt.ClusterSettings().SV, execCfg.DistSender, desc.GetID(), predicates, 10))
6569

6670
db.CheckQueryResults(t, `SELECT count(*) FROM test`, beforeNumRows)
71+
}
72+
73+
func TestRollbackRandomTable(t *testing.T) {
74+
defer leaktest.AfterTest(t)()
75+
defer log.Scope(t).Close(t)
76+
77+
ctx := context.Background()
78+
s, sqlDB, kv := serverutils.StartServer(t, base.TestServerArgs{UseDatabase: "test"})
79+
defer s.Stopper().Stop(ctx)
80+
tt := s.ApplicationLayer()
81+
82+
db := sqlutils.MakeSQLRunner(sqlDB)
83+
db.Exec(t, `CREATE DATABASE IF NOT EXISTS test`)
84+
85+
rng, _ := randutil.NewPseudoRand()
86+
tableName := "rand_table"
87+
// Virtual columns make it possible to create rows that can't be
88+
// fingerprinted. E.g. an expression like a+b may cause an integer overflow.
89+
noVirtualColumns := randgen.WithColumnFilter(func(col *tree.ColumnTableDef) bool {
90+
return !col.IsVirtual()
91+
})
92+
createStmt := randgen.RandCreateTableWithName(
93+
ctx, rng, tableName, 1,
94+
[]randgen.TableOption{noVirtualColumns})
95+
stmt := tree.SerializeForDisplay(createStmt)
96+
t.Log(stmt)
97+
98+
db.Exec(t, stmt)
99+
100+
_, err := randgen.PopulateTableWithRandData(rng, sqlDB, tableName, 100, nil)
101+
require.NoError(t, err)
102+
103+
rollbackTs := tt.Clock().Now()
104+
fingerprint := db.QueryStr(t, fmt.Sprintf("SHOW EXPERIMENTAL_FINGERPRINTS FROM TABLE test.%s", tableName))
105+
106+
_, err = randgen.PopulateTableWithRandData(rng, sqlDB, tableName, 100, nil)
107+
require.NoError(t, err)
108+
109+
codec := tt.Codec()
110+
desc := desctestutils.TestingGetPublicTableDescriptor(kv, codec, "test", tableName)
111+
predicates := kvpb.DeleteRangePredicates{StartTime: rollbackTs}
112+
113+
execCfg := tt.ExecutorConfig().(sql.ExecutorConfig)
114+
require.NoError(t, sql.DeleteTableWithPredicate(
115+
ctx, kv, codec, &tt.ClusterSettings().SV, execCfg.DistSender, desc.GetID(), predicates, 10))
67116

117+
afterFingerprint := db.QueryStr(t, fmt.Sprintf("SHOW EXPERIMENTAL_FINGERPRINTS FROM TABLE test.%s", tableName))
118+
require.Equal(t, fingerprint, afterFingerprint)
68119
}

0 commit comments

Comments
 (0)