Skip to content

Commit

Permalink
Merge pull request #43 from nixys/fix/33
Browse files Browse the repository at this point in the history
fix(#33): Search for "generated" columns is to greedy
  • Loading branch information
borisershov authored Sep 4, 2024
2 parents dfa5e33 + 7c0d1b4 commit 918d376
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion modules/anonymizers/mysql/dh.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mysql_anonymize
import (
"bytes"
"fmt"
"regexp"
"strings"

"github.com/nixys/nxs-data-anonymizer/misc"
Expand Down Expand Up @@ -54,14 +55,24 @@ func dhCreateTableFieldName(usrCtx any, deferred, token []byte) ([]byte, error)
return append(deferred, token...), nil
}

// checkGenerated returns true when specified type is `generated`
// See: https://dev.mysql.com/blog-archive/generated-columns-in-mysql-5-7-5 for details
func checkGenerated(t string) bool {
if strings.Contains(t, "AS") == true {
b, _ := regexp.Match("^([A-Z]+)((\\([0-9]+\\) )| )(GENERATED ALWAYS AS|AS)", []byte(t))
return b
}
return false
}

func dhCreateTableColumnAdd(usrCtx any, deferred, token []byte) ([]byte, error) {

uctx := usrCtx.(*userCtx)

traw := strings.TrimSpace(string(deferred))
trawUpper := strings.ToUpper(traw)

if strings.Contains(trawUpper, " GENERATED ") == false {
if checkGenerated(trawUpper) == false {

i := strings.IndexAny(strings.TrimSpace(trawUpper), " (,")
if i != -1 {
Expand Down

0 comments on commit 918d376

Please sign in to comment.