Skip to content

Commit

Permalink
Fix: Shares can't be updated on table_grant resource (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskuchin authored Dec 16, 2021
1 parent 88d28a9 commit 6884748
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/resources/table_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func UpdateTableGrant(d *schema.ResourceData, meta interface{}) error {

// difference calculates roles/shares to add/revoke
difference := func(key string) (toAdd []string, toRevoke []string) {
old, new := d.GetChange("roles")
old, new := d.GetChange(key)
oldSet := old.(*schema.Set)
newSet := new.(*schema.Set)
toAdd = expandStringList(newSet.Difference(oldSet).List())
Expand Down
27 changes: 27 additions & 0 deletions pkg/resources/table_grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,33 @@ func TestTableGrantCreate(t *testing.T) {
r.NoError(err)
})
}

func TestTableGrantUpdate(t *testing.T) {
r := require.New(t)

// d := schema.TestResourceDataRaw(t, resources.TableGrant().Resource.Schema, in)
d := tableGrant(t, "test-db|PUBLIC|test-table|SELECT|false", map[string]interface{}{
"table_name": "test-table",
"schema_name": "PUBLIC",
"database_name": "test-db",
"privilege": "SELECT",
"roles": []interface{}{"test-role-1", "test-role-2"},
"shares": []interface{}{"test-share-1", "test-share-2"},
})
r.NotNil(d)

WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
mock.ExpectExec(`^GRANT SELECT ON TABLE "test-db"."PUBLIC"."test-table" TO ROLE "test-role-1"`).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(`^GRANT SELECT ON TABLE "test-db"."PUBLIC"."test-table" TO ROLE "test-role-2"`).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(`^GRANT SELECT ON TABLE "test-db"."PUBLIC"."test-table" TO SHARE "test-share-1"`).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(`^GRANT SELECT ON TABLE "test-db"."PUBLIC"."test-table" TO SHARE "test-share-2"`).WillReturnResult(sqlmock.NewResult(1, 1))
expectReadTableGrant(mock)

err := resources.UpdateTableGrant(d, db)
r.NoError(err)
})
}

func TestTableGrantRead(t *testing.T) {
r := require.New(t)

Expand Down

0 comments on commit 6884748

Please sign in to comment.