Skip to content

Commit a72f378

Browse files
committed
Fixes #214 - Cannot create a grant with multiple privileges
1 parent 633ea61 commit a72f378

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

.goreleaser.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Visit https://goreleaser.com for documentation on how to customize this
22
# behavior.
3+
version: 2
34
before:
45
hooks:
56
# this is just an example and not a requirement for provider building/publishing

mysql/resource_grant.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ func CreateGrant(ctx context.Context, d *schema.ResourceData, meta interface{})
515515
if err != nil {
516516
return diag.Errorf("failed showing grants: %v", err)
517517
}
518-
if conflictingGrant != nil {
518+
if conflictingGrant != nil && areSameGrants(grant, conflictingGrant) {
519519
return diag.Errorf("user/role %#v already has grant %v - ", grant.GetUserOrRole(), conflictingGrant)
520520
}
521521

@@ -816,6 +816,41 @@ func getMatchingGrant(ctx context.Context, db *sql.DB, desiredGrant MySQLGrant)
816816
return result, nil
817817
}
818818

819+
func areSameGrants(grantA MySQLGrant, grantB MySQLGrant) bool {
820+
821+
// If grants do not conflict (e.g. different users or databases), they are not the same
822+
if !grantA.ConflictsWithGrant(grantB) {
823+
return false
824+
}
825+
826+
// Compare privileges
827+
grantAWithPrivileges, aHasPrivileges := grantA.(MySQLGrantWithPrivileges)
828+
grantBWithPrivileges, bHasPrivileges := grantB.(MySQLGrantWithPrivileges)
829+
if aHasPrivileges && bHasPrivileges {
830+
privilegesA := normalizePerms(grantAWithPrivileges.GetPrivileges())
831+
privilegesB := normalizePerms(grantBWithPrivileges.GetPrivileges())
832+
if !reflect.DeepEqual(privilegesA, privilegesB) {
833+
return false
834+
}
835+
}
836+
837+
// Compare roles
838+
grantAWithRoles, aHasRoles := grantA.(MySQLGrantWithRoles)
839+
grantBWithRoles, bHasRoles := grantB.(MySQLGrantWithRoles)
840+
if aHasRoles && bHasRoles {
841+
rolesA := grantAWithRoles.GetRoles()
842+
rolesB := grantBWithRoles.GetRoles()
843+
sort.Strings(rolesA)
844+
sort.Strings(rolesB)
845+
if !reflect.DeepEqual(rolesA, rolesB) {
846+
return false
847+
}
848+
}
849+
850+
// If neither privileges nor roles are present, or both match, the grants are considered the same
851+
return true
852+
}
853+
819854
var (
820855
kUserOrRoleRegex = regexp.MustCompile("['`]?([^'`]+)['`]?(?:@['`]?([^'`]+)['`]?)?")
821856
)

0 commit comments

Comments
 (0)