@@ -954,15 +954,34 @@ func UnifiedSchemaHashTest(t *testing.T, tester DatastoreTester) {
954954 })
955955 require .NoError (err )
956956
957+ // Get the schema mode from the datastore to determine expected behavior
958+ schemaModeProvider , ok := ds .(interface {
959+ SchemaModeForTesting () (options.SchemaMode , error )
960+ })
961+ require .True (ok , "datastore must implement SchemaModeForTesting() for this test" )
962+ schemaMode , err := schemaModeProvider .SchemaModeForTesting ()
963+ require .NoError (err , "failed to get schema mode from datastore" )
964+
965+ // Determine if this mode should write the unified schema hash
966+ // The hash is written in all modes that write to the unified schema
967+ shouldWriteHash := schemaMode == options .SchemaModeReadLegacyWriteBoth ||
968+ schemaMode == options .SchemaModeReadNewWriteBoth ||
969+ schemaMode == options .SchemaModeReadNewWriteNew
970+
957971 // Read the schema hash from schema_revision table
958- hash , err := readerImpl .ReadSchemaHash (ctx )
959- require .NoError (err )
960- require .NotEmpty (hash , "schema hash should not be empty" )
972+ hash , hashErr := readerImpl .ReadSchemaHash (ctx )
961973
962- // Verify the hash is correct by computing the expected hash
963- // The hash is computed from sorted definitions (matching datastore behavior)
964- expectedHash := computeExpectedSchemaHash (t , testSchemaDefinitions )
965- require .Equal (expectedHash , hash , "schema hash should match computed hash of sorted schema text" )
974+ if shouldWriteHash {
975+ // Hash MUST be present and correct
976+ require .NoError (hashErr , "schema hash should be present in mode %s" , schemaMode )
977+ require .NotEmpty (hash , "schema hash should not be empty" )
978+ expectedHash := computeExpectedSchemaHash (t , testSchemaDefinitions )
979+ require .Equal (expectedHash , hash , "schema hash should match computed hash of sorted schema text" )
980+ } else {
981+ // Hash should NOT be present in ReadLegacyWriteLegacy mode
982+ require .True (errors .Is (hashErr , datastore .ErrSchemaNotFound ),
983+ "expected ErrSchemaNotFound in mode %s, got: %v" , schemaMode , hashErr )
984+ }
966985
967986 // Update the schema
968987 updatedSchemaText , _ , err := generator .GenerateSchema (updatedSchemaDefinitions )
@@ -982,15 +1001,23 @@ func UnifiedSchemaHashTest(t *testing.T, tester DatastoreTester) {
9821001 })
9831002 require .NoError (err )
9841003
985- // Read the updated schema hash
986- updatedHash , err := hashReader .SchemaHashReaderForTesting ().ReadSchemaHash (ctx )
987- require .NoError (err )
988- require .NotEmpty (updatedHash , "updated schema hash should not be empty" )
989- require .NotEqual (hash , updatedHash , "schema hash should change after update" )
1004+ // Read the updated schema hash and verify based on mode
1005+ updatedHash , updatedHashErr := hashReader .SchemaHashReaderForTesting ().ReadSchemaHash (ctx )
9901006
991- // Verify the updated hash is correct by computing the expected hash
992- expectedUpdatedHash := computeExpectedSchemaHash (t , updatedSchemaDefinitions )
993- require .Equal (expectedUpdatedHash , updatedHash , "updated schema hash should match computed hash of sorted updated schema text" )
1007+ if shouldWriteHash {
1008+ // Hash MUST be present, correct, and different from the first hash
1009+ require .NoError (updatedHashErr , "updated schema hash should be present in mode %s" , schemaMode )
1010+ require .NotEmpty (updatedHash , "updated schema hash should not be empty" )
1011+ require .NotEqual (hash , updatedHash , "schema hash should change after update" )
1012+
1013+ // Verify the updated hash is correct
1014+ expectedUpdatedHash := computeExpectedSchemaHash (t , updatedSchemaDefinitions )
1015+ require .Equal (expectedUpdatedHash , updatedHash , "updated schema hash should match computed hash of sorted updated schema text" )
1016+ } else {
1017+ // Hash should still NOT be present in ReadLegacyWriteLegacy mode
1018+ require .True (errors .Is (updatedHashErr , datastore .ErrSchemaNotFound ),
1019+ "expected ErrSchemaNotFound after update in mode %s, got: %v" , schemaMode , updatedHashErr )
1020+ }
9941021}
9951022
9961023// UnifiedSchemaHashWatchTest tests the schema hash watcher functionality
0 commit comments