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