Add --no-hooks feature
This commit is contained in:
parent
09cdb7a652
commit
95ff24b918
11 changed files with 32 additions and 0 deletions
|
@ -7,6 +7,7 @@ type Config struct {
|
|||
OutFolder string `toml:"out_folder"`
|
||||
BaseDir string `toml:"base_dir"`
|
||||
ExcludeTables []string `toml:"exclude"`
|
||||
NoHooks bool `toml:"no_hooks"`
|
||||
|
||||
Postgres PostgresConfig `toml:"postgres"`
|
||||
}
|
||||
|
|
2
main.go
2
main.go
|
@ -65,6 +65,7 @@ func main() {
|
|||
rootCmd.PersistentFlags().StringP("basedir", "b", "", "The base directory templates and templates_test folders are")
|
||||
rootCmd.PersistentFlags().StringSliceP("exclude", "x", nil, "Tables to be excluded from the generated package")
|
||||
rootCmd.PersistentFlags().BoolP("debug", "d", false, "Debug mode prints stack traces on error")
|
||||
rootCmd.PersistentFlags().BoolP("no-hooks", "", false, "Disable hooks feature for your models")
|
||||
|
||||
viper.SetDefault("postgres.sslmode", "require")
|
||||
viper.SetDefault("postgres.port", "5432")
|
||||
|
@ -104,6 +105,7 @@ func preRun(cmd *cobra.Command, args []string) error {
|
|||
DriverName: driverName,
|
||||
OutFolder: viper.GetString("output"),
|
||||
PkgName: viper.GetString("pkgname"),
|
||||
NoHooks: viper.GetBool("no-hooks"),
|
||||
}
|
||||
|
||||
// BUG: https://github.com/spf13/viper/issues/200
|
||||
|
|
|
@ -81,6 +81,7 @@ func (s *State) Run(includeTests bool) error {
|
|||
DriverName: s.Config.DriverName,
|
||||
UseLastInsertID: s.Driver.UseLastInsertID(),
|
||||
PkgName: s.Config.PkgName,
|
||||
NoHooks: s.Config.NoHooks,
|
||||
|
||||
StringFuncs: templateStringMappers,
|
||||
}
|
||||
|
@ -110,6 +111,7 @@ func (s *State) Run(includeTests bool) error {
|
|||
DriverName: s.Config.DriverName,
|
||||
UseLastInsertID: s.Driver.UseLastInsertID(),
|
||||
PkgName: s.Config.PkgName,
|
||||
NoHooks: s.Config.NoHooks,
|
||||
|
||||
StringFuncs: templateStringMappers,
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ type templateData struct {
|
|||
DriverName string
|
||||
UseLastInsertID bool
|
||||
PkgName string
|
||||
NoHooks bool
|
||||
|
||||
StringFuncs map[string]func(string) string
|
||||
}
|
||||
|
|
|
@ -14,7 +14,9 @@ var (
|
|||
|
||||
type (
|
||||
{{$tableNameSingular}}Slice []*{{$tableNameSingular}}
|
||||
{{if eq .NoHooks false -}}
|
||||
{{$tableNameSingular}}Hook func(*{{$tableNameSingular}}) error
|
||||
{{- end}}
|
||||
|
||||
{{$varNameSingular}}Query struct {
|
||||
*boil.Query
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
{{- if eq .NoHooks false -}}
|
||||
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
|
||||
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
|
||||
var {{$varNameSingular}}BeforeCreateHooks []{{$tableNameSingular}}Hook
|
||||
|
@ -89,3 +90,4 @@ func {{$tableNameSingular}}AddHook(hookPoint boil.HookPoint, {{$varNameSingular}
|
|||
{{$varNameSingular}}AfterUpsertHooks = append({{$varNameSingular}}AfterUpsertHooks, {{$varNameSingular}}Hook)
|
||||
}
|
||||
}
|
||||
{{- end}}
|
||||
|
|
|
@ -40,9 +40,11 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string
|
|||
)
|
||||
|
||||
var err error
|
||||
{{if eq .NoHooks false -}}
|
||||
if err := o.doBeforeCreateHooks(); err != nil {
|
||||
return err
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
ins := fmt.Sprintf(`INSERT INTO {{.Table.Name}} ("%s") VALUES (%s)`, strings.Join(wl, `","`), strmangle.Placeholders(len(wl), 1, 1))
|
||||
|
||||
|
@ -57,9 +59,11 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string
|
|||
return errors.Wrap(err, "{{.PkgName}}: unable to insert into {{.Table.Name}}")
|
||||
}
|
||||
|
||||
{{if eq .NoHooks false -}}
|
||||
if len(returnColumns) == 0 {
|
||||
return o.doAfterCreateHooks()
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
lastID, err := result.LastInsertId()
|
||||
if err != nil || lastID == 0 || len({{$varNameSingular}}AutoIncPrimaryKeys) != 1 {
|
||||
|
@ -89,5 +93,9 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string
|
|||
}
|
||||
{{end}}
|
||||
|
||||
{{if eq .NoHooks false -}}
|
||||
return o.doAfterCreateHooks()
|
||||
{{- else -}}
|
||||
return nil
|
||||
{{- end}}
|
||||
}
|
||||
|
|
|
@ -35,9 +35,11 @@ func (o *{{$tableNameSingular}}) UpdateP(exec boil.Executor, whitelist ... strin
|
|||
// Update does not automatically update the record in case of default values. Use .Reload()
|
||||
// to refresh the records.
|
||||
func (o *{{$tableNameSingular}}) Update(exec boil.Executor, whitelist ... string) error {
|
||||
{{if eq .NoHooks false -}}
|
||||
if err := o.doBeforeUpdateHooks(); err != nil {
|
||||
return err
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
var err error
|
||||
var query string
|
||||
|
@ -66,7 +68,11 @@ func (o *{{$tableNameSingular}}) Update(exec boil.Executor, whitelist ... string
|
|||
return errors.Errorf("failed to update single row, updated %d rows", r)
|
||||
}
|
||||
|
||||
{{if eq .NoHooks false -}}
|
||||
return o.doAfterUpdateHooks()
|
||||
{{- else -}}
|
||||
return nil
|
||||
{{- end}}
|
||||
}
|
||||
|
||||
// UpdateAllP updates all rows with matching column names, and panics on error.
|
||||
|
|
|
@ -48,9 +48,11 @@ func (o *{{$tableNameSingular}}) Upsert(exec boil.Executor, updateOnConflict boo
|
|||
query := generateUpsertQuery("{{.Table.Name}}", updateOnConflict, ret, update, conflict, whitelist)
|
||||
|
||||
var err error
|
||||
{{if eq .NoHooks false -}}
|
||||
if err := o.doBeforeUpsertHooks(); err != nil {
|
||||
return err
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, query)
|
||||
|
@ -71,9 +73,11 @@ func (o *{{$tableNameSingular}}) Upsert(exec boil.Executor, updateOnConflict boo
|
|||
return errors.Wrap(err, "{{.PkgName}}: unable to upsert for {{.Table.Name}}")
|
||||
}
|
||||
|
||||
{{if eq .NoHooks false -}}
|
||||
if err := o.doAfterUpsertHooks(); err != nil {
|
||||
return err
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
{{- if eq .NoHooks false -}}
|
||||
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
|
||||
{{- $tableNamePlural := .Table.Name | plural | titleCase -}}
|
||||
{{- $varNamePlural := .Table.Name | plural | camelCase -}}
|
||||
|
@ -45,3 +46,4 @@ func test{{$tableNamePlural}}Hooks(t *testing.T) {
|
|||
|
||||
{{$varNameSingular}}BeforeCreateHooks = []{{$tableNameSingular}}Hook{}
|
||||
}
|
||||
{{- end}}
|
||||
|
|
|
@ -115,6 +115,7 @@ func TestHelpers(t *testing.T) {
|
|||
{{- end -}}
|
||||
}
|
||||
|
||||
{{if eq .NoHooks false -}}
|
||||
func TestHooks(t *testing.T) {
|
||||
{{- range $index, $table := .Tables}}
|
||||
{{- if $table.IsJoinTable -}}
|
||||
|
@ -124,6 +125,7 @@ func TestHooks(t *testing.T) {
|
|||
{{end -}}
|
||||
{{- end -}}
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
func TestInsert(t *testing.T) {
|
||||
{{- range $index, $table := .Tables}}
|
||||
|
|
Loading…
Add table
Reference in a new issue