Skip to content

Commit

Permalink
[fix] Add 'copy current grants' when performing ownership grant #152 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
saperry committed Mar 20, 2020
1 parent 17c22dc commit 4346dee
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pkg/snowflake/grant.go
Expand Up @@ -138,7 +138,13 @@ func (gb *CurrentGrantBuilder) Share(n string) GrantExecutable {

// Grant returns the SQL that will grant privileges on the grant to the grantee
func (ge *CurrentGrantExecutable) Grant(p string) string {
return fmt.Sprintf(`GRANT %v ON %v %v TO %v "%v"`,
var template string
if p == `OWNERSHIP` {
template = `GRANT %v ON %v %v TO %v "%v" COPY CURRENT GRANTS`
} else {
template = `GRANT %v ON %v %v TO %v "%v"`
}
return fmt.Sprintf(template,
p, ge.grantType, ge.grantName, ge.granteeType, ge.granteeName)
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/snowflake/grant_test.go
Expand Up @@ -21,6 +21,9 @@ func TestDatabaseGrant(t *testing.T) {
s = dg.Role("bob").Revoke("USAGE")
a.Equal(`REVOKE USAGE ON DATABASE "testDB" FROM ROLE "bob"`, s)

s = dg.Role("bob").Grant("OWNERSHIP")
a.Equal(`GRANT OWNERSHIP ON DATABASE "testDB" TO ROLE "bob" COPY CURRENT GRANTS`, s)

s = dg.Share("bob").Grant("USAGE")
a.Equal(`GRANT USAGE ON DATABASE "testDB" TO SHARE "bob"`, s)

Expand All @@ -47,6 +50,9 @@ func TestSchemaGrant(t *testing.T) {

s = sg.Share("bob").Revoke("USAGE")
a.Equal(`REVOKE USAGE ON SCHEMA "test_db"."testSchema" FROM SHARE "bob"`, s)

s = sg.Role("bob").Grant("OWNERSHIP")
a.Equal(`GRANT OWNERSHIP ON SCHEMA "test_db"."testSchema" TO ROLE "bob" COPY CURRENT GRANTS`, s)
}

func TestViewGrant(t *testing.T) {
Expand All @@ -68,6 +74,9 @@ func TestViewGrant(t *testing.T) {

s = vg.Share("bob").Revoke("USAGE")
a.Equal(`REVOKE USAGE ON VIEW "test_db"."PUBLIC"."testView" FROM SHARE "bob"`, s)

s = vg.Role("bob").Grant("OWNERSHIP")
a.Equal(`GRANT OWNERSHIP ON VIEW "test_db"."PUBLIC"."testView" TO ROLE "bob" COPY CURRENT GRANTS`, s)
}

func TestWarehouseGrant(t *testing.T) {
Expand All @@ -83,6 +92,10 @@ func TestWarehouseGrant(t *testing.T) {

s = wg.Role("bob").Revoke("USAGE")
a.Equal(`REVOKE USAGE ON WAREHOUSE "test_warehouse" FROM ROLE "bob"`, s)

s = wg.Role("bob").Grant("OWNERSHIP")
a.Equal(`GRANT OWNERSHIP ON WAREHOUSE "test_warehouse" TO ROLE "bob" COPY CURRENT GRANTS`, s)

}

func TestShowGrantsOf(t *testing.T) {
Expand Down

0 comments on commit 4346dee

Please sign in to comment.