Skip to content

Commit

Permalink
fix: mysql用户删除
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Nov 26, 2024
1 parent ad12a83 commit e666396
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 50 deletions.
2 changes: 1 addition & 1 deletion internal/data/database_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (r databaseUserRepo) Delete(id uint) error {
if err != nil {
return err
}
_ = mysql.UserDrop(user.Username)
_ = mysql.UserDrop(user.Username, user.Host)
case biz.DatabaseTypePostgresql:
postgres, err := db.NewPostgres(server.Username, server.Password, server.Host, server.Port)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/data/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ func (r *websiteRepo) Delete(req *request.WebsiteDelete) error {
return err
}
if mysql, err := db.NewMySQL("root", rootPassword, "/tmp/mysql.sock", "unix"); err == nil {
_ = mysql.UserDrop(website.Name)
_ = mysql.UserDrop(website.Name, "localhost")
_ = mysql.DatabaseDrop(website.Name)
}
if postgres, err := db.NewPostgres("postgres", "", "127.0.0.1", 5432); err == nil {
Expand Down
96 changes: 48 additions & 48 deletions pkg/db/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,44 @@ func NewMySQL(username, password, address string, typ ...string) (*MySQL, error)
}, nil
}

func (m *MySQL) Close() error {
return m.db.Close()
func (r *MySQL) Close() error {
return r.db.Close()
}

func (m *MySQL) Ping() error {
return m.db.Ping()
func (r *MySQL) Ping() error {
return r.db.Ping()
}

func (m *MySQL) Query(query string, args ...any) (*sql.Rows, error) {
return m.db.Query(query, args...)
func (r *MySQL) Query(query string, args ...any) (*sql.Rows, error) {
return r.db.Query(query, args...)
}

func (m *MySQL) QueryRow(query string, args ...any) *sql.Row {
return m.db.QueryRow(query, args...)
func (r *MySQL) QueryRow(query string, args ...any) *sql.Row {
return r.db.QueryRow(query, args...)
}

func (m *MySQL) Exec(query string, args ...any) (sql.Result, error) {
return m.db.Exec(query, args...)
func (r *MySQL) Exec(query string, args ...any) (sql.Result, error) {
return r.db.Exec(query, args...)
}

func (m *MySQL) Prepare(query string) (*sql.Stmt, error) {
return m.db.Prepare(query)
func (r *MySQL) Prepare(query string) (*sql.Stmt, error) {
return r.db.Prepare(query)
}

func (m *MySQL) DatabaseCreate(name string) error {
_, err := m.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s", name))
m.flushPrivileges()
func (r *MySQL) DatabaseCreate(name string) error {
_, err := r.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s", name))
r.flushPrivileges()
return err
}

func (m *MySQL) DatabaseDrop(name string) error {
_, err := m.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", name))
m.flushPrivileges()
func (r *MySQL) DatabaseDrop(name string) error {
_, err := r.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", name))
r.flushPrivileges()
return err
}

func (m *MySQL) DatabaseExists(name string) (bool, error) {
rows, err := m.Query("SHOW DATABASES")
func (r *MySQL) DatabaseExists(name string) (bool, error) {
rows, err := r.Query("SHOW DATABASES")
if err != nil {
return false, err
}
Expand All @@ -92,38 +92,38 @@ func (m *MySQL) DatabaseExists(name string) (bool, error) {
return false, nil
}

func (m *MySQL) DatabaseSize(name string) (int64, error) {
func (r *MySQL) DatabaseSize(name string) (int64, error) {
var size int64
err := m.QueryRow(fmt.Sprintf("SELECT COALESCE(SUM(data_length) + SUM(index_length), 0) FROM information_schema.tables WHERE table_schema = '%s'", name)).Scan(&size)
err := r.QueryRow(fmt.Sprintf("SELECT COALESCE(SUM(data_length) + SUM(index_length), 0) FROM information_schema.tables WHERE table_schema = '%s'", name)).Scan(&size)
return size, err
}

func (m *MySQL) UserCreate(user, password, host string) error {
_, err := m.Exec(fmt.Sprintf("CREATE USER IF NOT EXISTS '%s'@'%s' IDENTIFIED BY '%s'", user, host, password))
m.flushPrivileges()
func (r *MySQL) UserCreate(user, password, host string) error {
_, err := r.Exec(fmt.Sprintf("CREATE USER IF NOT EXISTS '%s'@'%s' IDENTIFIED BY '%s'", user, host, password))
r.flushPrivileges()
return err
}

func (m *MySQL) UserDrop(user string) error {
_, err := m.Exec(fmt.Sprintf("DROP USER IF EXISTS '%s'", user))
m.flushPrivileges()
func (r *MySQL) UserDrop(user, host string) error {
_, err := r.Exec(fmt.Sprintf("DROP USER IF EXISTS '%s'@'%s'", user, host))
r.flushPrivileges()
return err
}

func (m *MySQL) UserPassword(user, password, host string) error {
_, err := m.Exec(fmt.Sprintf("ALTER USER '%s'@'%s' IDENTIFIED BY '%s'", user, host, password))
m.flushPrivileges()
func (r *MySQL) UserPassword(user, password, host string) error {
_, err := r.Exec(fmt.Sprintf("ALTER USER '%s'@'%s' IDENTIFIED BY '%s'", user, host, password))
r.flushPrivileges()
return err
}

func (m *MySQL) PrivilegesGrant(user, database, host string) error {
_, err := m.Exec(fmt.Sprintf("GRANT ALL PRIVILEGES ON %s.* TO '%s'@'%s'", database, user, host))
m.flushPrivileges()
func (r *MySQL) PrivilegesGrant(user, database, host string) error {
_, err := r.Exec(fmt.Sprintf("GRANT ALL PRIVILEGES ON %s.* TO '%s'@'%s'", database, user, host))
r.flushPrivileges()
return err
}

func (m *MySQL) UserPrivileges(user, host string) (map[string][]string, error) {
rows, err := m.Query(fmt.Sprintf("SHOW GRANTS FOR '%s'@'%s'", user, host))
func (r *MySQL) UserPrivileges(user, host string) (map[string][]string, error) {
rows, err := r.Query(fmt.Sprintf("SHOW GRANTS FOR '%s'@'%s'", user, host))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -164,14 +164,14 @@ func (m *MySQL) UserPrivileges(user, host string) (map[string][]string, error) {
return privileges, nil
}

func (m *MySQL) PrivilegesRevoke(user, database, host string) error {
_, err := m.Exec(fmt.Sprintf("REVOKE ALL PRIVILEGES ON %s.* FROM '%s'@'%s'", database, user, host))
m.flushPrivileges()
func (r *MySQL) PrivilegesRevoke(user, database, host string) error {
_, err := r.Exec(fmt.Sprintf("REVOKE ALL PRIVILEGES ON %s.* FROM '%s'@'%s'", database, user, host))
r.flushPrivileges()
return err
}

func (m *MySQL) Users() ([]types.MySQLUser, error) {
rows, err := m.Query("SELECT user, host FROM mysql.user")
func (r *MySQL) Users() ([]types.MySQLUser, error) {
rows, err := r.Query("SELECT user, host FROM mysql.user")
if err != nil {
return nil, err
}
Expand All @@ -183,7 +183,7 @@ func (m *MySQL) Users() ([]types.MySQLUser, error) {
if err := rows.Scan(&user, &host); err != nil {
continue
}
grants, err := m.userGrants(user, host)
grants, err := r.userGrants(user, host)
if err != nil {
continue
}
Expand All @@ -198,7 +198,7 @@ func (m *MySQL) Users() ([]types.MySQLUser, error) {
return users, nil
}

func (m *MySQL) Databases() ([]types.MySQLDatabase, error) {
func (r *MySQL) Databases() ([]types.MySQLDatabase, error) {
query := `
SELECT
SCHEMA_NAME,
Expand All @@ -208,7 +208,7 @@ func (m *MySQL) Databases() ([]types.MySQLDatabase, error) {
WHERE SCHEMA_NAME NOT IN ('information_schema', 'performance_schema', 'mysql', 'sys')
`

rows, err := m.Query(query)
rows, err := r.Query(query)
if err != nil {
return nil, err
}
Expand All @@ -230,8 +230,8 @@ func (m *MySQL) Databases() ([]types.MySQLDatabase, error) {
return databases, nil
}

func (m *MySQL) userGrants(user, host string) ([]string, error) {
rows, err := m.Query(fmt.Sprintf("SHOW GRANTS FOR '%s'@'%s'", user, host))
func (r *MySQL) userGrants(user, host string) ([]string, error) {
rows, err := r.Query(fmt.Sprintf("SHOW GRANTS FOR '%s'@'%s'", user, host))
if err != nil {
return nil, err
}
Expand All @@ -248,6 +248,6 @@ func (m *MySQL) userGrants(user, host string) ([]string, error) {
return grants, nil
}

func (m *MySQL) flushPrivileges() {
_, _ = m.Exec("FLUSH PRIVILEGES")
func (r *MySQL) flushPrivileges() {
_, _ = r.Exec("FLUSH PRIVILEGES")
}

0 comments on commit e666396

Please sign in to comment.