Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Regex pattern for table name and primary key, allow numbers and mixed… #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions examples/compose/vtcompose/vtcompose.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func getTableName(sqlFile string) string {
log.Fatalf("reading sqlFile file %s: %s", sqlFile, err)
}

r, _ := regexp.Compile("CREATE TABLE ([a-z_-]*) \\(")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expression should not consider dash (-) to be a valid character in a table name.

r, _ := regexp.Compile("CREATE TABLE ([`A-z_0-9]*) \\(")
rs := r.FindStringSubmatch(string(sqlFileData))
// replace all ` from table name if exists
return strings.ReplaceAll(rs[1], "`", "")
Expand All @@ -326,7 +326,8 @@ func getPrimaryKey(sqlFile string) string {
r, _ := regexp.Compile("PRIMARY KEY \\((.*)\\).*")
rs := r.FindStringSubmatch(string(sqlFileData))

return rs[1]
// replace all ` from key fields if exists
return strings.ReplaceAll(rs[1], "`", "")
}

func getKeyColumns(sqlFile string) string {
Expand Down