From 4346dee4430764fa7c608a761b5ffd3c53650631 Mon Sep 17 00:00:00 2001 From: saperry Date: Fri, 20 Mar 2020 01:02:52 +0000 Subject: [PATCH] [fix] Add 'copy current grants' when performing ownership grant #152 (#153) --- pkg/snowflake/grant.go | 8 +++++++- pkg/snowflake/grant_test.go | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/pkg/snowflake/grant.go b/pkg/snowflake/grant.go index f15b43dbad..a2e59face5 100644 --- a/pkg/snowflake/grant.go +++ b/pkg/snowflake/grant.go @@ -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) } diff --git a/pkg/snowflake/grant_test.go b/pkg/snowflake/grant_test.go index 8082871061..02e0795219 100644 --- a/pkg/snowflake/grant_test.go +++ b/pkg/snowflake/grant_test.go @@ -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) @@ -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) { @@ -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) { @@ -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) {