diff --git a/pkg/clients/mysql/mysql.go b/pkg/clients/mysql/mysql.go index dbcaa28f..6a18e79b 100644 --- a/pkg/clients/mysql/mysql.go +++ b/pkg/clients/mysql/mysql.go @@ -9,7 +9,6 @@ import ( "github.com/crossplane-contrib/provider-sql/pkg/clients/xsql" "github.com/pkg/errors" - "k8s.io/utils/ptr" xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" @@ -17,7 +16,6 @@ import ( const ( errNotSupported = "%s not supported by mysql client" - errFlushPriv = "cannot flush privileges" ) type mySQLDB struct { @@ -146,31 +144,13 @@ type ExecQuery struct { ErrorValue string } -// ExecOptions parametrizes which optional statements will be executed before or after ExecQuery.Query -type ExecOptions struct { - // Flush defines whether privileges will be flushed after executing the query. Defaults to true - Flush *bool -} - -// ExecWithFlush is a wrapper function for xsql.DB.Exec() that allows the execution of optional queries before and after the provided query -func ExecWithFlush(ctx context.Context, db xsql.DB, query ExecQuery, options ExecOptions) error { - if options.Flush == nil { - options.Flush = ptr.To(true) - } - +// ExecWrapper is a wrapper function for xsql.DB.Exec() that allows the execution of optional queries before and after the provided query +func ExecWrapper(ctx context.Context, db xsql.DB, query ExecQuery) error { if err := db.Exec(ctx, xsql.Query{ String: query.Query, }); err != nil { return errors.Wrap(err, query.ErrorValue) } - if *options.Flush { - if err := db.Exec(ctx, xsql.Query{ - String: "FLUSH PRIVILEGES", - }); err != nil { - return errors.Wrap(err, errFlushPriv) - } - } - return nil } diff --git a/pkg/controller/mysql/database/reconciler.go b/pkg/controller/mysql/database/reconciler.go index 9f60e786..c7cbdf8b 100644 --- a/pkg/controller/mysql/database/reconciler.go +++ b/pkg/controller/mysql/database/reconciler.go @@ -22,7 +22,6 @@ import ( "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" - "k8s.io/utils/ptr" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" @@ -150,7 +149,7 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext query := "CREATE DATABASE " + mysql.QuoteIdentifier(meta.GetExternalName(cr)) - if err := mysql.ExecWithFlush(ctx, c.db, mysql.ExecQuery{Query: query, ErrorValue: errCreateDB}, mysql.ExecOptions{Flush: ptr.To(false)}); err != nil { + if err := mysql.ExecWrapper(ctx, c.db, mysql.ExecQuery{Query: query, ErrorValue: errCreateDB}); err != nil { return managed.ExternalCreation{}, err } @@ -170,7 +169,7 @@ func (c *external) Delete(ctx context.Context, mg resource.Managed) error { query := "DROP DATABASE IF EXISTS " + mysql.QuoteIdentifier(meta.GetExternalName(cr)) - if err := mysql.ExecWithFlush(ctx, c.db, mysql.ExecQuery{Query: query, ErrorValue: errDropDB}, mysql.ExecOptions{Flush: ptr.To(false)}); err != nil { + if err := mysql.ExecWrapper(ctx, c.db, mysql.ExecQuery{Query: query, ErrorValue: errDropDB}); err != nil { return err } diff --git a/pkg/controller/mysql/grant/reconciler.go b/pkg/controller/mysql/grant/reconciler.go index 5d5b35c3..2753e13e 100644 --- a/pkg/controller/mysql/grant/reconciler.go +++ b/pkg/controller/mysql/grant/reconciler.go @@ -261,7 +261,7 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext privileges, grantOption := getPrivilegesString(cr.Spec.ForProvider.Privileges.ToStringSlice()) query := createGrantQuery(privileges, dbname, username, table, grantOption) - if err := mysql.ExecWithFlush(ctx, c.db, mysql.ExecQuery{Query: query, ErrorValue: errCreateGrant}, mysql.ExecOptions{}); err != nil { + if err := mysql.ExecWrapper(ctx, c.db, mysql.ExecQuery{Query: query, ErrorValue: errCreateGrant}); err != nil { return managed.ExternalCreation{}, err } return managed.ExternalCreation{}, nil @@ -285,10 +285,10 @@ func (c *external) Update(ctx context.Context, mg resource.Managed) (managed.Ext sort.Strings(toRevoke) privileges, grantOption := getPrivilegesString(toRevoke) query := createRevokeQuery(privileges, dbname, username, table, grantOption) - if err := mysql.ExecWithFlush(ctx, c.db, + if err := mysql.ExecWrapper(ctx, c.db, mysql.ExecQuery{ Query: query, ErrorValue: errRevokeGrant, - }, mysql.ExecOptions{}); err != nil { + }); err != nil { return managed.ExternalUpdate{}, err } } @@ -297,10 +297,10 @@ func (c *external) Update(ctx context.Context, mg resource.Managed) (managed.Ext sort.Strings(toGrant) privileges, grantOption := getPrivilegesString(toGrant) query := createGrantQuery(privileges, dbname, username, table, grantOption) - if err := mysql.ExecWithFlush(ctx, c.db, + if err := mysql.ExecWrapper(ctx, c.db, mysql.ExecQuery{ Query: query, ErrorValue: errCreateGrant, - }, mysql.ExecOptions{}); err != nil { + }); err != nil { return managed.ExternalUpdate{}, err } } @@ -369,7 +369,7 @@ func (c *external) Delete(ctx context.Context, mg resource.Managed) error { privileges, grantOption := getPrivilegesString(cr.Spec.ForProvider.Privileges.ToStringSlice()) query := createRevokeQuery(privileges, dbname, username, table, grantOption) - if err := mysql.ExecWithFlush(ctx, c.db, mysql.ExecQuery{Query: query, ErrorValue: errRevokeGrant}, mysql.ExecOptions{}); err != nil { + if err := mysql.ExecWrapper(ctx, c.db, mysql.ExecQuery{Query: query, ErrorValue: errRevokeGrant}); err != nil { var myErr *mysqldriver.MySQLError if errors.As(err, &myErr) && myErr.Number == errCodeNoSuchGrant { // MySQL automatically deletes related grants if the user has been deleted diff --git a/pkg/controller/mysql/user/reconciler.go b/pkg/controller/mysql/user/reconciler.go index 92e7e0fe..693f5276 100644 --- a/pkg/controller/mysql/user/reconciler.go +++ b/pkg/controller/mysql/user/reconciler.go @@ -278,7 +278,7 @@ func (c *external) executeCreateUserQuery(ctx context.Context, username string, resourceOptions, ) - if err := mysql.ExecWithFlush(ctx, c.db, mysql.ExecQuery{Query: query, ErrorValue: errCreateUser}, mysql.ExecOptions{}); err != nil { + if err := mysql.ExecWrapper(ctx, c.db, mysql.ExecQuery{Query: query, ErrorValue: errCreateUser}); err != nil { return err } @@ -308,7 +308,7 @@ func (c *external) Update(ctx context.Context, mg resource.Managed) (managed.Ext mysql.QuoteValue(host), resourceOptions, ) - if err := mysql.ExecWithFlush(ctx, c.db, mysql.ExecQuery{Query: query, ErrorValue: errUpdateUser}, mysql.ExecOptions{}); err != nil { + if err := mysql.ExecWrapper(ctx, c.db, mysql.ExecQuery{Query: query, ErrorValue: errUpdateUser}); err != nil { return managed.ExternalUpdate{}, err } @@ -335,7 +335,7 @@ func (c *external) UpdatePassword(ctx context.Context, cr *v1alpha1.User, userna if pwchanged { query := fmt.Sprintf("ALTER USER %s@%s IDENTIFIED BY %s", mysql.QuoteValue(username), mysql.QuoteValue(host), mysql.QuoteValue(pw)) - if err := mysql.ExecWithFlush(ctx, c.db, mysql.ExecQuery{Query: query, ErrorValue: errUpdateUser}, mysql.ExecOptions{}); err != nil { + if err := mysql.ExecWrapper(ctx, c.db, mysql.ExecQuery{Query: query, ErrorValue: errUpdateUser}); err != nil { return managed.ConnectionDetails{}, err } @@ -356,7 +356,7 @@ func (c *external) Delete(ctx context.Context, mg resource.Managed) error { username, host := mysql.SplitUserHost(meta.GetExternalName(cr)) query := fmt.Sprintf("DROP USER IF EXISTS %s@%s", mysql.QuoteValue(username), mysql.QuoteValue(host)) - if err := mysql.ExecWithFlush(ctx, c.db, mysql.ExecQuery{Query: query, ErrorValue: errDropUser}, mysql.ExecOptions{}); err != nil { + if err := mysql.ExecWrapper(ctx, c.db, mysql.ExecQuery{Query: query, ErrorValue: errDropUser}); err != nil { return err }