@@ -387,10 +387,13 @@ func readSchemaGrants(db *DBConnection, d *schema.ResourceData) error {
387387
388388func readTableGrants (db * DBConnection , d * schema.ResourceData ) error {
389389 log .Printf ("[DEBUG] Reading table grants" )
390+
390391 var entityName , query string
391392 _ , isUser := d .GetOk (grantUserAttr )
392393 _ , isGroup := d .GetOk (grantGroupAttr )
393394
395+ databaseName := getDatabaseName (db , d )
396+
394397 if isUser {
395398 entityName = d .Get (grantUserAttr ).(string )
396399 query = `
@@ -451,14 +454,15 @@ func readTableGrants(db *DBConnection, d *schema.ResourceData) error {
451454 AND p.identity_name = $2
452455 AND p.identity_type = 'role'
453456 WHERE t.schema_name = $3
457+ and t.database_name = $4
454458 GROUP BY t.table_name
455459`
456460 }
457461
458462 schemaName := d .Get (grantSchemaAttr ).(string )
459463 objects := d .Get (grantObjectsAttr ).(* schema.Set )
460464 queryArgs := []interface {}{
461- pq .Array (grantObjectTypesCodes ["table" ]), entityName , schemaName ,
465+ pq .Array (grantObjectTypesCodes ["table" ]), entityName , schemaName , databaseName ,
462466 }
463467
464468 if isGrantToPublic (d ) {
@@ -549,6 +553,8 @@ func readCallableGrants(db *DBConnection, d *schema.ResourceData) error {
549553 schemaName := d .Get (grantSchemaAttr ).(string )
550554 objectType := d .Get (grantObjectTypeAttr ).(string )
551555
556+ databaseName := getDatabaseName (db , d )
557+
552558 if isUser {
553559 entityName = d .Get (grantUserAttr ).(string )
554560 query = `
@@ -587,14 +593,15 @@ func readCallableGrants(db *DBConnection, d *schema.ResourceData) error {
587593 JOIN svv_redshift_functions pr ON pr.function_name = p.function_name AND pr.schema_name = p.namespace_name
588594 WHERE p.namespace_name = $1
589595 AND p.identity_name = $2
590- AND p.identity_type = 'role'
596+ AND p.identity_type = 'role'
597+ AND pr.database_name = $4
591598 GROUP BY p.function_name
592599`
593600 }
594601
595602 callables := stripArgumentsFromCallablesDefinitions (d .Get (grantObjectsAttr ).(* schema.Set ))
596603 queryArgs := []interface {}{
597- schemaName , entityName , pq .Array (grantObjectTypesCodes [objectType ]),
604+ schemaName , entityName , pq .Array (grantObjectTypesCodes [objectType ]), databaseName ,
598605 }
599606
600607 if isGrantToPublic (d ) {
0 commit comments