Skip to content

Commit

Permalink
Merge pull request #1395 from 090809/feat-version-checking
Browse files Browse the repository at this point in the history
feat: add version checking flags
  • Loading branch information
stephenafamo authored Jun 27, 2024
2 parents 77666a3 + 095b846 commit 8c10c06
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 18 deletions.
47 changes: 30 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Table of Contents
* [Configuration](#configuration)
* [Initial Generation](#initial-generation)
* [Regeneration](#regeneration)
* [Controlling Version](#controlling-version)
* [Controlling Generation](#controlling-generation)
* [Aliases](#aliases)
* [Types](#types)
Expand Down Expand Up @@ -372,23 +373,24 @@ blacklist = ["migrations", "addresses.name", "*.secret_col"]
You can also pass in these top level configuration values if you would prefer
not to pass them through the command line or environment variables:

| Name | Defaults |
| ------------------- | --------- |
| pkgname | "models" |
| output | "models" |
| tag | [] |
| debug | false |
| add-global-variants | false |
| add-panic-variants | false |
| add-enum-types | false |
| enum-null-prefix | "Null" |
| no-context | false |
| no-hooks | false |
| no-tests | false |
| no-auto-timestamps | false |
| no-rows-affected | false |
| no-driver-templates | false |
| tag-ignore | [] |
| Name | Defaults |
|---------------------------|----------|
| pkgname | "models" |
| output | "models" |
| tag | [] |
| debug | false |
| add-global-variants | false |
| add-panic-variants | false |
| add-enum-types | false |
| enum-null-prefix | "Null" |
| no-context | false |
| no-hooks | false |
| no-tests | false |
| no-auto-timestamps | false |
| no-rows-affected | false |
| no-driver-templates | false |
| tag-ignore | [] |
| strict-verify-mod-version | false |

##### Full Example

Expand Down Expand Up @@ -464,6 +466,7 @@ Flags:
--tag-ignore strings List of column names that should have tags values set to '-' (ignored during parsing)
--templates strings A templates directory, overrides the embedded template folders in sqlboiler
--version Print the version
--strict-verify-mod-version Prevent code generation, if project version of sqlboiler not match with executable
--wipe Delete the output folder (rm -rf) before generation to ensure sanity
```

Expand Down Expand Up @@ -512,6 +515,16 @@ the same source. And the intention is to always regenerate from a pure state.
The only reason the `--wipe` flag isn't defaulted to on is because we don't
like programs that `rm -rf` things on the filesystem without being asked to.

#### Controlling Version

When sqlboiler is used on a regular basis, sometimes problems arise on the
developers' side that the version they are using does not match the version
specified in the project.

Sqlboiler will warn, if version in project and executable mismatch.
Sqlboiler can also fail to prevent code generation, when
`--strict-verify-mod-version` flag (or aliased version in toml) is enabled.

#### Controlling Generation

The templates get executed in a specific way each time. There's a variety of
Expand Down
57 changes: 57 additions & 0 deletions boilingcore/boilingcore.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ func New(config *Config) (*State, error) {
)
}

if err := s.verifyModVersion(); err != nil {
if s.Config.StrictVerifyModVersion {
fmt.Printf("Error: %s\n", err.Error())
os.Exit(1)
} else {
fmt.Printf("Warn: %s\n", err.Error())
}
}

s.Driver = drivers.GetDriver(config.DriverName)
s.initInflections()

Expand Down Expand Up @@ -694,3 +703,51 @@ func denormalizeSlashes(path string) string {
path = strings.ReplaceAll(path, `\`, `/`)
return path
}

func (s *State) verifyModVersion() error {
dir, err := os.Getwd()
if err != nil {
return fmt.Errorf("could not get working directory: %v", err)
}
var path string
for dir != "/" && dir != "." {
resolvedPath := filepath.Join(dir, "go.mod")

_, err := os.Stat(resolvedPath)
if os.IsNotExist(err) {
dir = filepath.Dir(dir)
} else {
path = resolvedPath
break
}
}
if path == "" {
return fmt.Errorf(fmt.Sprintf("could not find go.mod in any parent directory"))
}

gomodbytes, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf(fmt.Sprintf("could not read go.mod: %v", err))
}

re, err := regexp.Compile(`github\.com\/volatiletech\/sqlboiler\/v4 v(\d*\.\d*\.\d*)`)
if err != nil {
return fmt.Errorf(fmt.Sprintf("failed to parse regexp: %v", err))
}

match := re.FindSubmatch(gomodbytes)
if len(match) == 0 {
return fmt.Errorf(fmt.Sprintf("could not find sqlboiler version in go.mod"))
}
if string(match[1]) != s.Config.Version {
return fmt.Errorf(
"\tsqlboiler version in go.mod (%s) does not match executable version (%s)."+
"\n\tYou can update it with:"+
"\n\tgo get github.com/volatiletech/sqlboiler/v4",
string(match[0]),
s.Config.Version,
)
}

return nil
}
2 changes: 2 additions & 0 deletions boilingcore/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ type Config struct {
Inflections Inflections `toml:"inflections,omitempty" json:"inflections,omitempty"`
ForeignKeys []drivers.ForeignKey `toml:"foreign_keys,omitempty" json:"foreign_keys,omitempty" `

StrictVerifyModVersion bool `toml:"strict_verify_mod_version,omitempty" json:"strict_verify_mod_version"`

Version string `toml:"version" json:"version"`
}

Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func main() {
rootCmd.PersistentFlags().StringP("struct-tag-casing", "", "snake", "Decides the casing for go structure tag names. camel, title or snake (default snake)")
rootCmd.PersistentFlags().StringP("relation-tag", "r", "-", "Relationship struct tag name")
rootCmd.PersistentFlags().StringSliceP("tag-ignore", "", nil, "List of column names that should have tags values set to '-' (ignored during parsing)")
rootCmd.PersistentFlags().BoolP("strict-verify-mod-version", "", false, "Prevent code generation, if project version of sqlboiler not match with executable")

// hide flags not recommended for use
rootCmd.PersistentFlags().MarkHidden("replace")
Expand Down Expand Up @@ -198,7 +199,8 @@ func preRun(cmd *cobra.Command, args []string) error {
SingularExact: viper.GetStringMapString("inflections.singular_exact"),
Irregular: viper.GetStringMapString("inflections.irregular"),
},
ForeignKeys: boilingcore.ConvertForeignKeys(viper.Get("foreign_keys")),
ForeignKeys: boilingcore.ConvertForeignKeys(viper.Get("foreign_keys")),
StrictVerifyModVersion: viper.GetBool("strict-verify-mod-version"),

Version: sqlBoilerVersion,
}
Expand Down

0 comments on commit 8c10c06

Please sign in to comment.