@@ -79,14 +79,16 @@ func TestFirstUpgrade(t *testing.T) {
79
79
"USE test" ,
80
80
"CREATE TABLE foo (i INT PRIMARY KEY, j INT, INDEX idx(j))" ,
81
81
)
82
-
82
+ // Select a timestamp for when the tables were created.
83
+ startTime := testServer .Clock ().Now ()
83
84
// Corrupt the table descriptor in an unrecoverable manner. We are not able to automatically repair this
84
85
// descriptor.
85
86
tbl := desctestutils .TestingGetPublicTableDescriptor (kvDB , keys .SystemSQLCodec , "test" , "foo" )
86
87
descKey := catalogkeys .MakeDescMetadataKey (keys .SystemSQLCodec , tbl .GetID ())
87
88
require .NoError (t , kvDB .Txn (ctx , func (ctx context.Context , txn * kv.Txn ) error {
88
89
mut := tabledesc .NewBuilder (tbl .TableDesc ()).BuildExistingMutableTable ()
89
90
mut .NextIndexID = 1
91
+ mut .MaybeIncrementVersion ()
90
92
return txn .Put (ctx , descKey , mut .DescriptorProto ())
91
93
}))
92
94
@@ -106,6 +108,8 @@ func TestFirstUpgrade(t *testing.T) {
106
108
require .NoError (t , kvDB .Txn (ctx , func (ctx context.Context , txn * kv.Txn ) error {
107
109
mut := tabledesc .NewBuilder (tbl .TableDesc ()).BuildExistingMutableTable ()
108
110
mut .ModificationTime = hlc.Timestamp {}
111
+ mut .Indexes [0 ].NotVisible = true
112
+ mut .Version += 2 // Increment by 2 because we wrote a version above.
109
113
return txn .Put (ctx , descKey , mut .DescriptorProto ())
110
114
}))
111
115
@@ -127,8 +131,9 @@ func TestFirstUpgrade(t *testing.T) {
127
131
// the MVCC timestamp.
128
132
require .False (t , readDescFromStorage ().GetModificationTime ().IsEmpty ())
129
133
changes := readDescFromStorage ().GetPostDeserializationChanges ()
130
- require .Equal (t , changes .Len (), 1 )
134
+ require .Equal (t , 2 , changes .Len ())
131
135
require .True (t , changes .Contains (catalog .SetModTimeToMVCCTimestamp ))
136
+ require .True (t , changes .Contains (catalog .SetIndexInvisibility ))
132
137
133
138
// Wait long enough for precondition check to see the unbroken table descriptor.
134
139
execStmts (t , "CREATE DATABASE test3" )
@@ -137,18 +142,18 @@ func TestFirstUpgrade(t *testing.T) {
137
142
// Upgrade the cluster version.
138
143
tdb .Exec (t , qUpgrade )
139
144
140
- // The table descriptor protobuf should still have the modification time set;
141
- // the only post-deserialization change should be SetModTimeToMVCCTimestamp.
145
+ // Run a schema change on the bar table.
146
+ tdb .Exec (t , "ALTER TABLE foo ADD COLUMN z INT" )
147
+
148
+ // The only post-deserialization change should be SetModTimeToMVCCTimestamp,
149
+ // because the MVCC timestamp gets on each write.
142
150
require .False (t , readDescFromStorage ().GetModificationTime ().IsEmpty ())
143
151
changes = readDescFromStorage ().GetPostDeserializationChanges ()
144
- if v1 .Equal (clusterversion .V25_4 .Version ()) {
145
- // In 25.4, we do a one-time rewrite of all descriptors, so there should be
146
- // no changes here. In later versions, there should be one change.
147
- require .Equal (t , 0 , changes .Len ())
148
- } else {
149
- require .Equal (t , 1 , changes .Len ())
150
- require .True (t , changes .Contains (catalog .SetModTimeToMVCCTimestamp ))
151
- }
152
+ require .Equal (t , 1 , changes .Len ())
153
+ require .True (t , changes .Contains (catalog .SetModTimeToMVCCTimestamp ))
154
+ // Validate we can read bar before this upgrade. The lease manager should see
155
+ // distinct version numbers in the history. Otherwise, it can panic.
156
+ tdb .Exec (t , fmt .Sprintf ("SELECT * FROM foo AS OF SYSTEM TIME '%s'" , startTime .AsOfSystemTime ()))
152
157
}
153
158
154
159
// TestFirstUpgradeRepair tests the correct repair behavior of upgrade
@@ -241,12 +246,14 @@ func TestFirstUpgradeRepair(t *testing.T) {
241
246
ConstraintID : tbl .NextConstraintID ,
242
247
}}
243
248
tbl .NextConstraintID ++
249
+ tbl .MaybeIncrementVersion ()
244
250
b .Put (catalogkeys .MakeDescMetadataKey (codec , tbl .GetID ()), tbl .DescriptorProto ())
245
251
fn := funcdesc .NewBuilder (fooFn .FuncDesc ()).BuildExistingMutableFunction ()
246
252
fn .DependedOnBy = []descpb.FunctionDescriptor_Reference {{
247
253
ID : 123456789 ,
248
254
ColumnIDs : []descpb.ColumnID {1 },
249
255
}}
256
+ fn .MaybeIncrementVersion ()
250
257
b .Put (catalogkeys .MakeDescMetadataKey (codec , fn .GetID ()), fn .DescriptorProto ())
251
258
return txn .Run (ctx , b )
252
259
}))
@@ -340,18 +347,34 @@ func TestFirstUpgradeRepair(t *testing.T) {
340
347
tdb .CheckQueryResults (t , qDetectRepairableCorruption , [][]string {{"0" }})
341
348
close (upgradeCompleted )
342
349
require .NoError (t , grp .Wait ())
350
+ expectedVersionBump := 1
351
+ // Upgrades from before 25.4 will round-trip all descriptors once, so we
352
+ // expect two version bumps. Once for the repair and the next for the
353
+ // round trip.
354
+ if v0 .Less (clusterversion .V25_4 .Version ()) {
355
+ expectedVersionBump += 1
356
+ }
343
357
// Assert that a version upgrade is reflected for repaired descriptors (stricly one version upgrade).
344
358
for _ , d := range corruptDescs {
345
359
descId := d .GetID ()
346
360
desc := readDescFromStorage (descId )
347
- require .Equalf (t , descOldVersionMap [descId ]+ 1 , desc .GetVersion (), desc .GetName ())
361
+ require .Equalf (t , descOldVersionMap [descId ]+ descpb . DescriptorVersion ( expectedVersionBump ) , desc .GetVersion (), desc .GetName ())
348
362
}
349
363
350
364
// Assert that no version upgrade is reflected for non-repaired descriptors.
365
+ expectedVersionBump = 0
366
+ // Upgrading from before 25.4 all non-database descriptors will be round tripped.
367
+ if v0 .Less (clusterversion .V25_4 .Version ()) {
368
+ expectedVersionBump += 1
369
+ }
351
370
for _ , d := range nonCorruptDescs {
352
371
descId := d .GetID ()
353
372
desc := readDescFromStorage (descId )
354
- require .Equalf (t , descOldVersionMap [descId ], desc .GetVersion (), desc .GetName ())
373
+ if desc .DescriptorType () != catalog .Database {
374
+ require .Equalf (t , descOldVersionMap [descId ]+ descpb .DescriptorVersion (expectedVersionBump ), desc .GetVersion (), desc .GetName ())
375
+ } else {
376
+ require .Equalf (t , descOldVersionMap [descId ], desc .GetVersion (), desc .GetName ())
377
+ }
355
378
}
356
379
357
380
// Check that the repaired table and function are OK.
0 commit comments