@@ -10840,7 +10840,6 @@ func TestBackupRestoreClusterWithUDFs(t *testing.T) {
10840
10840
defer leaktest .AfterTest (t )()
10841
10841
defer log .Scope (t ).Close (t )
10842
10842
10843
- ctx := context .Background ()
10844
10843
tempDir , tempDirCleanupFn := testutils .TempDir (t )
10845
10844
defer tempDirCleanupFn ()
10846
10845
@@ -10857,153 +10856,75 @@ func TestBackupRestoreClusterWithUDFs(t *testing.T) {
10857
10856
testTenantOption = base .TestControlsTenantsExplicitly
10858
10857
}
10859
10858
10860
- srcCluster , srcSQLDB , srcClusterCleanupFn := backupRestoreTestSetupEmpty (
10859
+ _ , srcSQLDB , srcClusterCleanupFn := backupRestoreTestSetupEmpty (
10861
10860
t , singleNode , tempDir , InitManualReplication , base.TestClusterArgs {
10862
10861
ServerArgs : base.TestServerArgs {
10863
10862
DefaultTestTenant : testTenantOption ,
10864
10863
},
10865
10864
},
10866
10865
)
10867
- srcServer := srcCluster .ApplicationLayer (0 )
10868
10866
defer srcClusterCleanupFn ()
10869
10867
10870
- tgtCluster , tgtSQLDB , tgtClusterCleanupFn := backupRestoreTestSetupEmpty (
10868
+ _ , tgtSQLDB , tgtClusterCleanupFn := backupRestoreTestSetupEmpty (
10871
10869
t , singleNode , tempDir , InitManualReplication , base.TestClusterArgs {
10872
10870
ServerArgs : base.TestServerArgs {
10873
10871
DefaultTestTenant : testTenantOption ,
10874
10872
},
10875
10873
},
10876
10874
)
10877
- tgtServer := tgtCluster .ApplicationLayer (0 )
10878
10875
defer tgtClusterCleanupFn ()
10879
10876
10880
10877
srcSQLDB .Exec (t , `
10881
10878
CREATE DATABASE db1;
10882
10879
USE db1;
10883
10880
CREATE SCHEMA sc1;
10884
- CREATE TABLE sc1.tbl1(a INT PRIMARY KEY );
10885
- CREATE TYPE sc1.enum1 AS ENUM('Good' );
10881
+ CREATE TYPE sc1.enum1 AS ENUM('Good', 'Bad' );
10882
+ CREATE TABLE sc1.tbl1(a INT PRIMARY KEY, status sc1.enum1 );
10886
10883
CREATE SEQUENCE sc1.sq1;
10887
- CREATE FUNCTION sc1.f1(a sc1.enum1) RETURNS INT LANGUAGE SQL AS $$
10888
- SELECT a FROM sc1.tbl1;
10889
- SELECT nextval('sc1.sq1');
10884
+ CREATE FUNCTION sc1.f1(status_val sc1.enum1) RETURNS INT LANGUAGE SQL AS $$
10885
+ SELECT nextval('sc1.sq1')::INT;
10890
10886
$$;
10891
10887
` )
10892
10888
10893
- rows := srcSQLDB .QueryStr (t , `SELECT function_id FROM crdb_internal.create_function_statements WHERE function_name = 'f1'` )
10894
- require .Equal (t , 1 , len (rows ))
10895
- require .Equal (t , 1 , len (rows [0 ]))
10896
- udfID , err := strconv .Atoi (rows [0 ][0 ])
10897
- require .NoError (t , err )
10898
- err = sqltestutils .TestingDescsTxn (ctx , srcServer , func (ctx context.Context , txn isql.Txn , col * descs.Collection ) error {
10899
- dbDesc , err := col .ByNameWithLeased (txn .KV ()).Get ().Database (ctx , "db1" )
10900
- require .NoError (t , err )
10901
- require .Equal (t , 104 , int (dbDesc .GetID ()))
10902
-
10903
- scDesc , err := col .ByNameWithLeased (txn .KV ()).Get ().Schema (ctx , dbDesc , "sc1" )
10904
- require .NoError (t , err )
10905
- require .Equal (t , 106 , int (scDesc .GetID ()))
10889
+ // Add some data to exercise the sequence, function, and enum before backup
10890
+ srcSQLDB .Exec (t , `INSERT INTO sc1.tbl1 VALUES (1, 'Good'), (2, 'Bad')` )
10891
+ srcSQLDB .Exec (t , `SELECT nextval('sc1.sq1'), nextval('sc1.sq1')` )
10892
+ srcSQLDB .Exec (t , `SELECT sc1.f1('Good'::sc1.enum1), sc1.f1('Bad'::sc1.enum1)` )
10906
10893
10907
- tbName := tree .MakeTableNameWithSchema ("db1" , "sc1" , "tbl1" )
10908
- _ , tbDesc , err := descs .PrefixAndTable (ctx , col .ByNameWithLeased (txn .KV ()).Get (), & tbName )
10909
- require .NoError (t , err )
10910
- require .Equal (t , 107 , int (tbDesc .GetID ()))
10911
-
10912
- typName := tree .MakeQualifiedTypeName ("db1" , "sc1" , "enum1" )
10913
- _ , typDesc , err := descs .PrefixAndType (ctx , col .ByNameWithLeased (txn .KV ()).Get (), & typName )
10914
- require .NoError (t , err )
10915
- require .Equal (t , 108 , int (typDesc .GetID ()))
10916
-
10917
- tbName = tree .MakeTableNameWithSchema ("db1" , "sc1" , "sq1" )
10918
- _ , tbDesc , err = descs .PrefixAndTable (ctx , col .ByNameWithLeased (txn .KV ()).Get (), & tbName )
10919
- require .NoError (t , err )
10920
- require .Equal (t , 110 , int (tbDesc .GetID ()))
10921
-
10922
- fnDesc , err := col .ByIDWithLeased (txn .KV ()).WithoutNonPublic ().Get ().Function (ctx , descpb .ID (udfID ))
10923
- require .NoError (t , err )
10924
- require .Equal (t , 111 , int (fnDesc .GetID ()))
10925
- require .Equal (t , 104 , int (fnDesc .GetParentID ()))
10926
- require .Equal (t , 106 , int (fnDesc .GetParentSchemaID ()))
10927
- require .Equal (t , "SELECT a FROM db1.sc1.tbl1;\n SELECT nextval(110:::REGCLASS);" , fnDesc .GetFunctionBody ())
10928
- require .Equal (t , 100108 , int (fnDesc .GetParams ()[0 ].Type .Oid ()))
10929
- require .Equal (t , []descpb.ID {107 , 110 }, fnDesc .GetDependsOn ())
10930
- require .Equal (t , []descpb.ID {108 , 109 }, fnDesc .GetDependsOnTypes ())
10931
-
10932
- fnDef , _ := scDesc .GetFunction ("f1" )
10933
- require .Equal (t , 111 , int (fnDef .Signatures [0 ].ID ))
10934
- require .Equal (t , 100108 , int (fnDef .Signatures [0 ].ArgTypes [0 ].Oid ()))
10935
- return nil
10936
- })
10937
- require .NoError (t , err )
10894
+ var beforeBackupSeqVal int
10895
+ srcSQLDB .QueryRow (t , `SELECT last_value FROM sc1.sq1` ).Scan (& beforeBackupSeqVal )
10896
+ require .Equal (t , 4 , beforeBackupSeqVal )
10938
10897
10939
- // Bakcup the whole src cluster.
10898
+ // Backup the whole src cluster.
10940
10899
srcSQLDB .Exec (t , `BACKUP INTO $1` , localFoo )
10941
10900
10942
10901
// Restore into target cluster.
10943
10902
tgtSQLDB .Exec (t , `RESTORE FROM LATEST IN $1` , localFoo )
10944
10903
tgtSQLDB .Exec (t , `USE db1` )
10945
10904
10946
- // Verify that all IDs are correctly rewritten.
10947
- rows = tgtSQLDB .QueryStr (t , `SELECT function_id FROM crdb_internal.create_function_statements WHERE function_name = 'f1'` )
10948
- require .Equal (t , 1 , len (rows ))
10949
- require .Equal (t , 1 , len (rows [0 ]))
10950
- udfID , err = strconv .Atoi (rows [0 ][0 ])
10951
- require .NoError (t , err )
10952
-
10953
- const startingDescID = 123
10954
- err = sqltestutils .TestingDescsTxn (ctx , tgtServer , func (ctx context.Context , txn isql.Txn , col * descs.Collection ) error {
10955
- dbDesc , err := col .ByNameWithLeased (txn .KV ()).Get ().Database (ctx , "db1" )
10956
- require .NoError (t , err )
10957
- require .Equal (t , startingDescID , int (dbDesc .GetID ()))
10958
-
10959
- scDesc , err := col .ByNameWithLeased (txn .KV ()).Get ().Schema (ctx , dbDesc , "sc1" )
10960
- require .NoError (t , err )
10961
- require .Equal (t , startingDescID + 2 , int (scDesc .GetID ()))
10962
-
10963
- tbName := tree .MakeTableNameWithSchema ("db1" , "sc1" , "tbl1" )
10964
- _ , tbDesc , err := descs .PrefixAndTable (ctx , col .ByNameWithLeased (txn .KV ()).Get (), & tbName )
10965
- require .NoError (t , err )
10966
- require .Equal (t , startingDescID + 3 , int (tbDesc .GetID ()))
10967
-
10968
- typName := tree .MakeQualifiedTypeName ("db1" , "sc1" , "enum1" )
10969
- _ , typDesc , err := descs .PrefixAndType (ctx , col .ByNameWithLeased (txn .KV ()).Get (), & typName )
10970
- require .NoError (t , err )
10971
- require .Equal (t , startingDescID + 4 , int (typDesc .GetID ()))
10972
-
10973
- tbName = tree .MakeTableNameWithSchema ("db1" , "sc1" , "sq1" )
10974
- _ , tbDesc , err = descs .PrefixAndTable (ctx , col .ByNameWithLeased (txn .KV ()).Get (), & tbName )
10975
- require .NoError (t , err )
10976
- require .Equal (t , startingDescID + 6 , int (tbDesc .GetID ()))
10977
-
10978
- fnDesc , err := col .ByIDWithLeased (txn .KV ()).WithoutNonPublic ().Get ().Function (ctx , descpb .ID (udfID ))
10979
- require .NoError (t , err )
10980
- require .Equal (t , startingDescID + 7 , int (fnDesc .GetID ()))
10981
- require .Equal (t , startingDescID , int (fnDesc .GetParentID ()))
10982
- require .Equal (t , startingDescID + 2 , int (fnDesc .GetParentSchemaID ()))
10983
- // Make sure db name and IDs are rewritten in function body.
10984
- require .Equal (t , fmt .Sprintf ("SELECT a FROM db1.sc1.tbl1;\n SELECT nextval(%d:::REGCLASS);" ,
10985
- startingDescID + 6 ), fnDesc .GetFunctionBody ())
10986
-
10987
- expectedOID := 100127
10988
- dependsOn := []descpb.ID {126 , 129 }
10989
- dependsOnTypes := []descpb.ID {127 , 128 }
10990
- require .Equal (t , expectedOID , int (fnDesc .GetParams ()[0 ].Type .Oid ()))
10991
- require .Equal (t , dependsOn , fnDesc .GetDependsOn ())
10992
- require .Equal (t , dependsOnTypes , fnDesc .GetDependsOnTypes ())
10993
-
10994
- fnDef , _ := scDesc .GetFunction ("f1" )
10995
- require .Equal (t , startingDescID + 7 , int (fnDef .Signatures [0 ].ID ))
10996
- require .Equal (t , expectedOID , int (fnDef .Signatures [0 ].ArgTypes [0 ].Oid ()))
10997
- return nil
10998
- })
10999
- require .NoError (t , err )
11000
-
11001
- // Make sure function actually works on the target cluster.
11002
- rows = tgtSQLDB .QueryStr (t , `SELECT sc1.f1('Good'::sc1.enum1)` )
11003
- require .Equal (t , 1 , len (rows ))
11004
- require .Equal (t , 1 , len (rows [0 ]))
11005
- require .Equal (t , "1" , rows [0 ][0 ])
11006
- require .NoError (t , err )
10905
+ // Verify that the table data was restored correctly
10906
+ tableRows := tgtSQLDB .QueryStr (t , `SELECT a, status FROM sc1.tbl1 ORDER BY a` )
10907
+ require .Equal (t , 2 , len (tableRows ))
10908
+ require .Equal (t , []string {"1" , "Good" }, tableRows [0 ])
10909
+ require .Equal (t , []string {"2" , "Bad" }, tableRows [1 ])
10910
+
10911
+ // Verify that the sequence was restored with the correct current value
10912
+ var afterRestoreSeqVal int
10913
+ tgtSQLDB .QueryRow (t , `SELECT last_value FROM sc1.sq1` ).Scan (& afterRestoreSeqVal )
10914
+ require .Equal (t , beforeBackupSeqVal , afterRestoreSeqVal )
10915
+
10916
+ // Verify that the sequence continues to work by getting the next value
10917
+ tgtSQLDB .QueryRow (t , `SELECT nextval('sc1.sq1')` ).Scan (& afterRestoreSeqVal )
10918
+ require .Equal (t , 5 , afterRestoreSeqVal )
10919
+
10920
+ tgtSQLDB .Exec (t , `INSERT INTO sc1.tbl1 VALUES (3, 'Good')` )
10921
+ var enumTestRows int
10922
+ tgtSQLDB .QueryRow (t , `SELECT count(*) FROM sc1.tbl1 WHERE status = 'Good'` ).Scan (& enumTestRows )
10923
+ require .Equal (t , 2 , enumTestRows )
10924
+
10925
+ var funcResult int
10926
+ tgtSQLDB .QueryRow (t , `SELECT sc1.f1('Good'::sc1.enum1)` ).Scan (& funcResult )
10927
+ require .Equal (t , 6 , funcResult )
11007
10928
})
11008
10929
}
11009
10930
0 commit comments