@@ -91,6 +91,7 @@ import (
91
91
"github.com/cockroachdb/cockroach/pkg/util/ctxgroup"
92
92
"github.com/cockroachdb/cockroach/pkg/util/hlc"
93
93
"github.com/cockroachdb/cockroach/pkg/util/json"
94
+ "github.com/cockroachdb/cockroach/pkg/util/keysutil"
94
95
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
95
96
"github.com/cockroachdb/cockroach/pkg/util/log"
96
97
"github.com/cockroachdb/cockroach/pkg/util/log/eventpb"
@@ -823,24 +824,24 @@ func TestChangefeedQuotedIdentifiersTopicName(t *testing.T) {
823
824
sqlDB := sqlutils .MakeSQLRunner (s .DB )
824
825
825
826
sqlDB .Exec (t , `CREATE TABLE mytable (
826
- id INT PRIMARY KEY,
827
+ id INT PRIMARY KEY,
827
828
"SomeField" JSONB,
828
829
"AnotherField" JSONB
829
830
)` )
830
831
831
832
sqlDB .Exec (t , `INSERT INTO mytable VALUES (
832
- 1,
833
+ 1,
833
834
'{"PropA": "value1", "prop_b": "value2"}'::jsonb,
834
835
'{"PropC": "value3", "prop_d": "value4"}'::jsonb
835
836
)` )
836
837
837
838
sqlDB .Exec (t , `INSERT INTO mytable VALUES (
838
- 2,
839
+ 2,
839
840
'{"PropA": "value5", "prop_b": "value6"}'::jsonb,
840
841
'{"PropC": "value7", "prop_d": "value8"}'::jsonb
841
842
)` )
842
843
843
- foo := feed (t , f , `CREATE CHANGEFEED WITH diff, full_table_name, on_error=pause, envelope=wrapped AS SELECT
844
+ foo := feed (t , f , `CREATE CHANGEFEED WITH diff, full_table_name, on_error=pause, envelope=wrapped AS SELECT
844
845
id,
845
846
"SomeField"->>'PropA' AS "PropA",
846
847
"SomeField"->>'prop_b' AS "PropB",
@@ -9390,22 +9391,66 @@ func TestDistSenderRangeFeedPopulatesVirtualTable(t *testing.T) {
9390
9391
defer leaktest .AfterTest (t )()
9391
9392
defer log .Scope (t ).Close (t )
9392
9393
9393
- s , cleanup := makeServer (t )
9394
- defer cleanup ()
9394
+ scanner := keysutil .MakePrettyScanner (nil , nil )
9395
9395
9396
- sqlDB := sqlutils .MakeSQLRunner (s .DB )
9397
- sqlDB .Exec (t , `CREATE TABLE tbl (a INT, b STRING);` )
9398
- sqlDB .Exec (t , `INSERT INTO tbl VALUES (1, 'one'), (2, 'two'), (3, 'three');` )
9399
- sqlDB .Exec (t , `CREATE CHANGEFEED FOR tbl INTO 'null://';` )
9396
+ observeTables := func (sqlDB * sqlutils.SQLRunner , codec keys.SQLCodec ) []int {
9397
+ rows := sqlDB .Query (t , "SELECT range_start FROM crdb_internal.active_range_feeds" )
9398
+ defer rows .Close ()
9399
+ var tableIDs []int
9400
+ for rows .Next () {
9401
+ var prettyKey string
9402
+ require .NoError (t , rows .Scan (& prettyKey ))
9403
+ key , err := scanner .Scan (prettyKey )
9404
+ require .NoError (t , err )
9405
+ _ , tableID , err := codec .DecodeTablePrefix (key )
9406
+ require .NoError (t , err )
9407
+ tableIDs = append (tableIDs , int (tableID ))
9408
+ }
9409
+ return tableIDs
9410
+ }
9411
+
9412
+ cases := []struct {
9413
+ user string
9414
+ shouldSeeTable bool
9415
+ }{
9416
+ {`feedCreator` , false },
9417
+ {`regularUser` , false },
9418
+ {`adminUser` , true },
9419
+ {`viewClusterMetadataUser` , true },
9420
+ }
9421
+
9422
+ testFn := func (t * testing.T , s TestServer , f cdctest.TestFeedFactory ) {
9423
+ sqlDB := sqlutils .MakeSQLRunner (s .DB )
9424
+
9425
+ // Creates several different tables, users, and roles for us to use.
9426
+ ChangefeedJobPermissionsTestSetup (t , s )
9400
9427
9401
- var tableID int
9402
- sqlDB .QueryRow (t , "SELECT table_id FROM crdb_internal.tables WHERE name='tbl'" ).Scan (& tableID )
9403
- tableKey := s .Codec .TablePrefix (uint32 (tableID ))
9428
+ var tableID int
9429
+ sqlDB .QueryRow (t , "SELECT table_id FROM crdb_internal.tables WHERE name = 'table_a'" ).Scan (& tableID )
9404
9430
9405
- numRangesQuery := fmt .Sprintf (
9406
- "SELECT count(*) FROM crdb_internal.active_range_feeds WHERE range_start LIKE '%s/%%'" ,
9407
- tableKey )
9408
- sqlDB .CheckQueryResultsRetry (t , numRangesQuery , [][]string {{"1" }})
9431
+ var cf cdctest.TestFeed
9432
+ asUser (t , f , `feedCreator` , func (userDB * sqlutils.SQLRunner ) {
9433
+ cf = feed (t , f , `CREATE CHANGEFEED FOR table_a;` )
9434
+ })
9435
+ defer closeFeed (t , cf )
9436
+
9437
+ for _ , c := range cases {
9438
+ testutils .SucceedsSoon (t , func () error {
9439
+ asUser (t , f , c .user , func (userDB * sqlutils.SQLRunner ) {
9440
+ tableIDs := observeTables (userDB , s .Codec )
9441
+ if c .shouldSeeTable {
9442
+ require .Containsf (t , tableIDs , tableID , "user %s should see table %d" , c .user , tableID )
9443
+ } else {
9444
+ require .Emptyf (t , tableIDs , "user %s should not see any tables" , c .user )
9445
+ }
9446
+ })
9447
+ return nil
9448
+ })
9449
+ }
9450
+
9451
+ }
9452
+
9453
+ cdcTest (t , testFn , feedTestEnterpriseSinks )
9409
9454
}
9410
9455
9411
9456
func TestChangefeedCaseInsensitiveOpts (t * testing.T ) {
@@ -12157,7 +12202,7 @@ func TestChangefeedProtobuf(t *testing.T) {
12157
12202
)` )
12158
12203
sqlDB .Exec (t , `
12159
12204
INSERT INTO pricing VALUES
12160
- (1, 'Chair', 15.75, 2.500, ARRAY['Brown', 'Black']),
12205
+ (1, 'Chair', 15.75, 2.500, ARRAY['Brown', 'Black']),
12161
12206
(2, 'Table', 20.00, 1.23456789, ARRAY['Brown', 'Black'])` )
12162
12207
12163
12208
var opts []string
0 commit comments