@@ -23,9 +23,10 @@ import (
23
23
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
24
24
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
25
25
"github.com/cockroachdb/cockroach/pkg/util/log"
26
+ "github.com/cockroachdb/cockroach/pkg/util/randutil"
26
27
"github.com/cockroachdb/cockroach/pkg/workload"
27
28
"github.com/cockroachdb/cockroach/pkg/workload/histogram"
28
- _ "github.com/cockroachdb/cockroach/pkg/workload/schemachange"
29
+ "github.com/cockroachdb/cockroach/pkg/workload/schemachange"
29
30
"github.com/stretchr/testify/require"
30
31
"golang.org/x/sync/errgroup"
31
32
)
@@ -36,6 +37,7 @@ func TestWorkload(t *testing.T) {
36
37
skip .UnderDeadlock (t , "test connections can be too slow under expensive configs" )
37
38
skip .UnderRace (t , "test connections can be too slow under expensive configs" )
38
39
40
+ rng , _ := randutil .NewTestRand ()
39
41
scope := log .Scope (t )
40
42
defer scope .Close (t )
41
43
dir := scope .GetDirectory ()
@@ -56,7 +58,8 @@ func TestWorkload(t *testing.T) {
56
58
workload.Opser
57
59
workload.Flagser
58
60
})
59
- tdb := sqlutils .MakeSQLRunner (tc .ServerConn (0 ))
61
+ db := tc .ServerConn (0 )
62
+ tdb := sqlutils .MakeSQLRunner (db )
60
63
reg := histogram .NewRegistry (20 * time .Second , m .Name )
61
64
tdb .Exec (t , "CREATE USER testuser" )
62
65
tdb .Exec (t , "CREATE DATABASE schemachange" )
@@ -70,31 +73,61 @@ func TestWorkload(t *testing.T) {
70
73
require .NoError (t , os .WriteFile (fmt .Sprintf ("%s/%s.rows" , dir , name ), []byte (sqlutils .MatrixToStr (mat )), 0666 ))
71
74
}
72
75
73
- // Grab a backup, dump the namespace and descriptor tables upon failure.
76
+ findInvalidObjects := func () {
77
+ t .Helper ()
78
+ invalidObjects , err := schemachange .ValidateInvalidObjects (ctx , db )
79
+ if err != nil {
80
+ t .Fatal (err )
81
+ }
82
+ for _ , obj := range invalidObjects {
83
+ t .Logf (
84
+ "invalid object found: id: %d, database_name: %s, schema_name: %s, obj_name: %s, error: %v" ,
85
+ obj .ID , obj .DatabaseName , obj .SchemaName , obj .ObjName , obj .Error ,
86
+ )
87
+ }
88
+ if len (invalidObjects ) > 0 {
89
+ t .Errorf ("found %d invalid objects" , len (invalidObjects ))
90
+ }
91
+ }
92
+
74
93
defer func () {
75
- if ! t .Failed () {
76
- return
94
+ // Run validation before dropping the database.
95
+ findInvalidObjects ()
96
+
97
+ // Only take a backup if the test failed.
98
+ if t .Failed () {
99
+ // Dump namespace and descriptor in their raw format. This is useful for
100
+ // processing results with some degree of scripting.
101
+ dumpRows ("namespace" , tdb .Query (t , `SELECT * FROM system.namespace` ))
102
+ dumpRows ("descriptor" , tdb .Query (t , "SELECT id, encode(descriptor, 'hex') FROM system.descriptor" ))
103
+ // Dump out a more human readable version of the above as well to allow for
104
+ // easy debugging by hand.
105
+ // NB: A LEFT JOIN is used here because not all descriptors (looking at you
106
+ // functions) have namespace entries.
107
+ dumpRows ("ns-desc-json" , tdb .Query (t , `
108
+ SELECT
109
+ "parentID",
110
+ "parentSchemaID",
111
+ descriptor.id,
112
+ name,
113
+ crdb_internal.pb_to_json('cockroach.sql.sqlbase.Descriptor', descriptor)
114
+ FROM system.descriptor
115
+ LEFT JOIN system.namespace ON namespace.id = descriptor.id
116
+ ` ))
117
+ tdb .Exec (t , "BACKUP DATABASE schemachange INTO 'nodelocal://1/backup'" )
118
+ t .Logf ("backup, tracing data, and system table dumps in %s" , dir )
119
+ }
120
+
121
+ // Drop the database and run validation again. Test DROP DATABASE behavior
122
+ // with legacy schema changer 50% of the time.
123
+ schemaChangerSetting := "on"
124
+ if rng .Float32 () < 0.5 {
125
+ schemaChangerSetting = "off"
77
126
}
78
- // Dump namespace and descriptor in their raw format. This is useful for
79
- // processing results with some degree of scripting.
80
- dumpRows ("namespace" , tdb .Query (t , `SELECT * FROM system.namespace` ))
81
- dumpRows ("descriptor" , tdb .Query (t , "SELECT id, encode(descriptor, 'hex') FROM system.descriptor" ))
82
- // Dump out a more human readable version of the above as well to allow for
83
- // easy debugging by hand.
84
- // NB: A LEFT JOIN is used here because not all descriptors (looking at you
85
- // functions) have namespace entries.
86
- dumpRows ("ns-desc-json" , tdb .Query (t , `
87
- SELECT
88
- "parentID",
89
- "parentSchemaID",
90
- descriptor.id,
91
- name,
92
- crdb_internal.pb_to_json('cockroach.sql.sqlbase.Descriptor', descriptor)
93
- FROM system.descriptor
94
- LEFT JOIN system.namespace ON namespace.id = descriptor.id
95
- ` ))
96
- tdb .Exec (t , "BACKUP DATABASE schemachange INTO 'nodelocal://1/backup'" )
97
- t .Logf ("backup, tracing data, and system table dumps in %s" , dir )
127
+ t .Logf ("running DROP with use_declarative_schema_changer = %s" , schemaChangerSetting )
128
+ tdb .Exec (t , "SET use_declarative_schema_changer = $1" , schemaChangerSetting )
129
+ tdb .Exec (t , "DROP DATABASE schemachange CASCADE" )
130
+ findInvalidObjects ()
98
131
}()
99
132
100
133
pgURL , cleanup := pgurlutils .PGUrl (t , tc .Server (0 ).AdvSQLAddr (), t .Name (), url .User ("testuser" ))
0 commit comments