@@ -1011,3 +1011,56 @@ func applyTargetShards(ts *trafficSwitcher, targetShards []string) error {
1011
1011
}
1012
1012
return nil
1013
1013
}
1014
+
1015
+ // validateEmptyTables checks if all specified tables in the keyspace are empty across all shards.
1016
+ // It queries each shard's primary tablet and if any non-empty table is found, it returns an error
1017
+ // containing a list of non-empty tables.
1018
+ func validateEmptyTables (ctx context.Context , ts * topo.Server , tmc tmclient.TabletManagerClient , keyspace string , tables []string ) error {
1019
+ shards , err := ts .GetServingShards (ctx , keyspace )
1020
+ if err != nil {
1021
+ return err
1022
+ }
1023
+ if len (shards ) == 0 {
1024
+ return fmt .Errorf ("keyspace %s has no shards" , keyspace )
1025
+ }
1026
+
1027
+ isFaultyTable := map [string ]bool {}
1028
+ for _ , shard := range shards {
1029
+ primary := shard .PrimaryAlias
1030
+ if primary == nil {
1031
+ return fmt .Errorf ("shard does not have a primary: %v" , shard .ShardName ())
1032
+ }
1033
+
1034
+ ti , err := ts .GetTablet (ctx , primary )
1035
+ if err != nil {
1036
+ return err
1037
+ }
1038
+
1039
+ var selectQueries []string
1040
+ for _ , t := range tables {
1041
+ selectQueries = append (selectQueries , fmt .Sprintf ("(select '%s' from %s limit 1)" , t , t ))
1042
+ }
1043
+ query := strings .Join (selectQueries , "union all" )
1044
+
1045
+ res , err := tmc .ExecuteFetchAsDba (ctx , ti .Tablet , true , & tabletmanagerdatapb.ExecuteFetchAsDbaRequest {
1046
+ Query : []byte (query ),
1047
+ MaxRows : uint64 (len (tables )),
1048
+ })
1049
+ if err != nil {
1050
+ return err
1051
+ }
1052
+ for _ , row := range res .Rows {
1053
+ isFaultyTable [string (row .Values )] = true
1054
+ }
1055
+ }
1056
+
1057
+ var faultyTables []string
1058
+ for table := range isFaultyTable {
1059
+ faultyTables = append (faultyTables , table )
1060
+ }
1061
+
1062
+ if len (faultyTables ) > 0 {
1063
+ return fmt .Errorf ("target keyspace contains following non-empty table(s): %s" , strings .Join (faultyTables , ", " ))
1064
+ }
1065
+ return nil
1066
+ }
0 commit comments