Skip to content

Commit

Permalink
Process tableName as either text or regex (#13)
Browse files Browse the repository at this point in the history
This allows the `tableName` config field to be used as either plain text, or regex, removing the need for a separate field for this purpose, and simplifying the config setup.

Fixes #8
  • Loading branch information
PeterBooker authored Mar 5, 2022
1 parent b7925de commit 668e71c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ An example config for anonymizing a WordPress database is provided at [`config.e
The config is composed of many objects in the `patterns` array:

- `patterns`: an array of objects defining what modifications should be made.
- `tableName`: the name of the table the data will be stored in (used to parse `INSERT` statements to d etermine if the query should be modified.)
- `tableNameRegex`: regex to identify the relevant tables, required for multisite compatibility e.g. `.*_comments`.
- `tableName`: the name of the table the data will be stored in (used to parse `INSERT` statements to determine if the query should be modified.). You can also use regex to identify the relevant tables, required for multisite compatibility e.g. `.*_comments`.
- `fields`: an array of objects defining modifications to individual values' fields
- `field`: a string representing the name of the field. Not currently used, but still required to work and useful for debugging.
- `position`: the 1-based index of what number column this field represents. For instance, assuming a table with 3 columns `foo`, `bar`, and `baz`, and you wished to modify the `bar` column, this value would be `2`.
Expand Down
7 changes: 3 additions & 4 deletions config.example.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"patterns": [
{
"tableName": "wp_users",
"tableName": ".*_users",
"fields": [
{
"field": "user_login",
Expand Down Expand Up @@ -42,7 +42,7 @@
]
},
{
"tableName": "wp_usermeta",
"tableName": ".*_usermeta",
"fields": [
{
"field": "meta_value",
Expand Down Expand Up @@ -95,8 +95,7 @@
]
},
{
"tableName": "wp_comments",
"tableNameRegex": ".*_comments",
"tableName": ".*_comments",
"fields": [
{
"field": "comment_author",
Expand Down
5 changes: 3 additions & 2 deletions internal/anonymize/anonymize.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,9 @@ func applyConfigToInserts(stmt *sqlparser.Insert, config config.Config) (*sqlpar
continue
}
} else {
if stmt.Table.Name.String() != pattern.TableName {
// Config is not for this table, move onto next available config
re := regexp.MustCompile(pattern.TableName)
match := re.MatchString(stmt.Table.Name.String())
if !match {
continue
}
}
Expand Down
7 changes: 3 additions & 4 deletions internal/embed/files/config.default.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"patterns": [
{
"tableName": "wp_users",
"tableName": ".*_users",
"fields": [
{
"field": "user_login",
Expand Down Expand Up @@ -42,7 +42,7 @@
]
},
{
"tableName": "wp_usermeta",
"tableName": ".*_usermeta",
"fields": [
{
"field": "meta_value",
Expand Down Expand Up @@ -95,8 +95,7 @@
]
},
{
"tableName": "wp_comments",
"tableNameRegex": ".*_comments",
"tableName": ".*_comments",
"fields": [
{
"field": "comment_author",
Expand Down

0 comments on commit 668e71c

Please sign in to comment.