Skip to content

Commit

Permalink
add -r option for inflection rules
Browse files Browse the repository at this point in the history
  • Loading branch information
takeshi.nakata committed Nov 19, 2019
1 parent 7078022 commit b44b390
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ https://cloud.google.com/solutions/migrating-mysql-to-spanner?hl=ja#supported_da
- interleave needs parent primary key define. so ddlm2s needs all parent create table ddls for having foreign keys table ddl.
- you need to avoid pk hotspot yourself. so, you need to use varchar(36) (UUID v4 size) for interleave root table pk.
- https://tools.ietf.org/html/rfc4122

- support https://github.com/jinzhu/inflection#register-rules with -r option(see rule_sample.yaml)
# example
```
cat sample.sql
Expand Down
62 changes: 62 additions & 0 deletions cmd/ddlm2s/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,38 @@ import (
"io/ioutil"
"os"

"github.com/jinzhu/inflection"
"github.com/nakatamixi/go-ddlm2s"
"gopkg.in/yaml.v2"
)

type InflectionRule struct {
UnCountable []string `yaml:"uncountable"`
Irregular []IregularRule `yaml:"irregular"`
Plulal []FromTo `yaml:"plulal"`
Singular []FromTo `yaml:"singular"`
}

type IregularRule struct {
Singuler string `yaml:"singular"`
Plulal string `yaml:"plulal"`
}

type FromTo struct {
From string `yaml:"from"`
To string `yaml:"to"`
}

func main() {
var (
file string
ruleFile string
debug bool
enableInterleave bool
)
flags := flag.NewFlagSet("", flag.ContinueOnError)
flags.StringVar(&file, "f", "", "sql file path")
flags.StringVar(&ruleFile, "r", "", "inflection rule file path")
flags.BoolVar(&debug, "d", false, "debug print")
flags.BoolVar(&enableInterleave, "interleave", true, "convert fk to interleave")
if err := flags.Parse(os.Args[1:]); err != nil {
Expand All @@ -30,6 +51,34 @@ func main() {
if err != nil {
panic(err)
}
if ruleFile != "" {
inflectionRule, err := readRule(ruleFile)
if err != nil {
panic(err)
}
if inflectionRule != nil {
if inflectionRule.UnCountable != nil {
for _, unc := range inflectionRule.UnCountable {
inflection.AddUncountable(unc)
}
}
if inflectionRule.Irregular != nil {
for _, irr := range inflectionRule.Irregular {
inflection.AddIrregular(irr.Singuler, irr.Plulal)
}
}
if inflectionRule.Plulal != nil {
for _, pl := range inflectionRule.Plulal {
inflection.AddPlural(pl.From, pl.To)
}
}
if inflectionRule.Singular != nil {
for _, si := range inflectionRule.Singular {
inflection.AddSingular(si.From, si.To)
}
}
}
}
ddlm2s.Convert(body, debug, enableInterleave)
}

Expand All @@ -42,3 +91,16 @@ func read(file string) (string, error) {
return body, nil

}
func readRule(ruleFile string) (*InflectionRule, error) {
data, err := ioutil.ReadFile(ruleFile)
if err != nil {
return nil, err
}
inflectionRule := InflectionRule{}
err = yaml.Unmarshal(data, &inflectionRule)
if err != nil {
return nil, err
}
return &inflectionRule, nil

}
11 changes: 11 additions & 0 deletions rule_sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
uncountable:
- fish
irregular:
- singular: person
plulal: people
plulal:
- from: "(bu)s$"
to: "${1}ses"
singular:
- from: "(bus)(es)?$"
to: "${1}"
21 changes: 21 additions & 0 deletions sample.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CREATE TABLE `users` (
`id` VARCHAR (36) NOT NULL DEFAULT '',
`name` VARCHAR (255) NOT NULL DEFAULT '',
`uid` VARCHAR (255) NOT NULL,
`created_at` DATETIME NOT NULL,
`updated_at` DATETIME NOT NULL,
PRIMARY KEY (`id`),
UNIQUE `idx_users_uid` (`uid`)
) ENGINE = InnoDB, DEFAULT CHARACTER SET = utf8mb4;

CREATE TABLE `friends` (
`id` BIGINT (20) NOT NULL AUTO_INCREMENT,
`user_id` VARCHAR (36) NOT NULL,
`to_id` VARCHAR (36) NOT NULL,
`created_at` DATETIME NOT NULL,
`updated_at` DATETIME NOT NULL,
PRIMARY KEY (`id`),
UNIQUE `idx_friends_user_id_to_id` (`user_id`, `to_id`),
CONSTRAINT `fk_friends_users_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
CONSTRAINT `fk_friends_users_2` FOREIGN KEY (`to_id`) REFERENCES `users` (`id`)
) ENGINE = InnoDB, DEFAULT CHARACTER SET = utf8mb4;

0 comments on commit b44b390

Please sign in to comment.