From 0be697ede10e4df063db62472eb7fd4cb68decb2 Mon Sep 17 00:00:00 2001 From: "Roman A. Grigorovich" Date: Wed, 22 Nov 2023 14:51:56 +0300 Subject: [PATCH] no-relationship-funcs flag added --- README.md | 38 ++-- boilingcore/boilingcore.go | 53 +++--- boilingcore/config.go | 47 ++--- boilingcore/output.go | 2 +- boilingcore/templates.go | 25 +-- go.mod | 1 + go.sum | 2 + importers/imports.go | 6 +- main.go | 54 +++--- templates/main/04_relationship_to_one.go.tpl | 2 + .../main/05_relationship_one_to_one.go.tpl | 2 + templates/main/06_relationship_to_many.go.tpl | 2 + .../main/07_relationship_to_one_eager.go.tpl | 14 +- .../08_relationship_one_to_one_eager.go.tpl | 14 +- .../main/09_relationship_to_many_eager.go.tpl | 14 +- .../main/10_relationship_to_one_setops.go.tpl | 2 + .../11_relationship_one_to_one_setops.go.tpl | 2 + .../12_relationship_to_many_setops.go.tpl | 2 + templates/test/relationship_one_to_one.go.tpl | 2 + .../relationship_one_to_one_setops.go.tpl | 2 + templates/test/relationship_to_many.go.tpl | 2 + .../test/relationship_to_many_setops.go.tpl | 2 + templates/test/relationship_to_one.go.tpl | 2 + .../test/relationship_to_one_setops.go.tpl | 2 + .../singleton/boil_relationship_test.go.tpl | 169 ++++++++++++++++++ .../test/singleton/boil_suites_test.go.tpl | 166 ----------------- 26 files changed, 338 insertions(+), 291 deletions(-) create mode 100644 templates/test/singleton/boil_relationship_test.go.tpl diff --git a/README.md b/README.md index d9507a216..8337ba936 100644 --- a/README.md +++ b/README.md @@ -372,23 +372,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-relationship-funcs | false | +| no-tests | false | +| no-auto-timestamps | false | +| no-rows-affected | false | +| no-driver-templates | false | +| tag-ignore | [] | ##### Full Example @@ -454,6 +455,7 @@ Flags: --no-context Disable context.Context usage in the generated code --no-driver-templates Disable parsing of templates defined by the database driver --no-hooks Disable hooks feature for your models + --no-relationship-funcs Disable relationship loaders and setters feature for your models --no-rows-affected Disable rows affected in the generated API --no-tests Disable generated go test files -o, --output string The name of the folder to output to (default "models") @@ -1404,6 +1406,8 @@ type CoolObject struct { Helper methods will be generated for every to one and to many relationship structure you have defined in your database by using foreign keys. +You can disable this feature with "no-relationship-funcs" flag. + We attach these helpers directly to your model struct, for example: ```go diff --git a/boilingcore/boilingcore.go b/boilingcore/boilingcore.go index 86325f548..87b34edf9 100644 --- a/boilingcore/boilingcore.go +++ b/boilingcore/boilingcore.go @@ -132,31 +132,32 @@ func New(config *Config) (*State, error) { // state given. func (s *State) Run() error { data := &templateData{ - Tables: s.Tables, - Aliases: s.Config.Aliases, - DriverName: s.Config.DriverName, - PkgName: s.Config.PkgName, - AddGlobal: s.Config.AddGlobal, - AddPanic: s.Config.AddPanic, - AddSoftDeletes: s.Config.AddSoftDeletes, - AddEnumTypes: s.Config.AddEnumTypes, - EnumNullPrefix: s.Config.EnumNullPrefix, - NoContext: s.Config.NoContext, - NoHooks: s.Config.NoHooks, - NoAutoTimestamps: s.Config.NoAutoTimestamps, - NoRowsAffected: s.Config.NoRowsAffected, - NoDriverTemplates: s.Config.NoDriverTemplates, - NoBackReferencing: s.Config.NoBackReferencing, - AlwaysWrapErrors: s.Config.AlwaysWrapErrors, - StructTagCasing: s.Config.StructTagCasing, - TagIgnore: make(map[string]struct{}), - Tags: s.Config.Tags, - RelationTag: s.Config.RelationTag, - Dialect: s.Dialect, - Schema: s.Schema, - LQ: strmangle.QuoteCharacter(s.Dialect.LQ), - RQ: strmangle.QuoteCharacter(s.Dialect.RQ), - OutputDirDepth: s.Config.OutputDirDepth(), + Tables: s.Tables, + Aliases: s.Config.Aliases, + DriverName: s.Config.DriverName, + PkgName: s.Config.PkgName, + AddGlobal: s.Config.AddGlobal, + AddPanic: s.Config.AddPanic, + AddSoftDeletes: s.Config.AddSoftDeletes, + AddEnumTypes: s.Config.AddEnumTypes, + EnumNullPrefix: s.Config.EnumNullPrefix, + NoContext: s.Config.NoContext, + NoHooks: s.Config.NoHooks, + NoRelationshipFuncs: s.Config.NoRelationshipFuncs, + NoAutoTimestamps: s.Config.NoAutoTimestamps, + NoRowsAffected: s.Config.NoRowsAffected, + NoDriverTemplates: s.Config.NoDriverTemplates, + NoBackReferencing: s.Config.NoBackReferencing, + AlwaysWrapErrors: s.Config.AlwaysWrapErrors, + StructTagCasing: s.Config.StructTagCasing, + TagIgnore: make(map[string]struct{}), + Tags: s.Config.Tags, + RelationTag: s.Config.RelationTag, + Dialect: s.Dialect, + Schema: s.Schema, + LQ: strmangle.QuoteCharacter(s.Dialect.LQ), + RQ: strmangle.QuoteCharacter(s.Dialect.RQ), + OutputDirDepth: s.Config.OutputDirDepth(), DBTypes: make(once), StringFuncs: templateStringMappers, @@ -395,7 +396,6 @@ func findTemplates(root, base string) (map[string]templateLoader, error) { templates[relative] = fileLoader(path) return nil }) - if err != nil { return nil, err } @@ -451,7 +451,6 @@ func (s *State) mergeEnumImports() { // and performs them. func (s *State) processTypeReplacements() error { for _, r := range s.Config.TypeReplaces { - for i := range s.Tables { t := s.Tables[i] diff --git a/boilingcore/config.go b/boilingcore/config.go index 54f78b4b4..cec527cea 100644 --- a/boilingcore/config.go +++ b/boilingcore/config.go @@ -19,29 +19,30 @@ type Config struct { DriverName string `toml:"driver_name,omitempty" json:"driver_name,omitempty"` DriverConfig drivers.Config `toml:"driver_config,omitempty" json:"driver_config,omitempty"` - PkgName string `toml:"pkg_name,omitempty" json:"pkg_name,omitempty"` - OutFolder string `toml:"out_folder,omitempty" json:"out_folder,omitempty"` - TemplateDirs []string `toml:"template_dirs,omitempty" json:"template_dirs,omitempty"` - Tags []string `toml:"tags,omitempty" json:"tags,omitempty"` - Replacements []string `toml:"replacements,omitempty" json:"replacements,omitempty"` - Debug bool `toml:"debug,omitempty" json:"debug,omitempty"` - AddGlobal bool `toml:"add_global,omitempty" json:"add_global,omitempty"` - AddPanic bool `toml:"add_panic,omitempty" json:"add_panic,omitempty"` - AddSoftDeletes bool `toml:"add_soft_deletes,omitempty" json:"add_soft_deletes,omitempty"` - AddEnumTypes bool `toml:"add_enum_types,omitempty" json:"add_enum_types,omitempty"` - EnumNullPrefix string `toml:"enum_null_prefix,omitempty" json:"enum_null_prefix,omitempty"` - NoContext bool `toml:"no_context,omitempty" json:"no_context,omitempty"` - NoTests bool `toml:"no_tests,omitempty" json:"no_tests,omitempty"` - NoHooks bool `toml:"no_hooks,omitempty" json:"no_hooks,omitempty"` - NoAutoTimestamps bool `toml:"no_auto_timestamps,omitempty" json:"no_auto_timestamps,omitempty"` - NoRowsAffected bool `toml:"no_rows_affected,omitempty" json:"no_rows_affected,omitempty"` - NoDriverTemplates bool `toml:"no_driver_templates,omitempty" json:"no_driver_templates,omitempty"` - NoBackReferencing bool `toml:"no_back_reference,omitempty" json:"no_back_reference,omitempty"` - AlwaysWrapErrors bool `toml:"always_wrap_errors,omitempty" json:"always_wrap_errors,omitempty"` - Wipe bool `toml:"wipe,omitempty" json:"wipe,omitempty"` - StructTagCasing string `toml:"struct_tag_casing,omitempty" json:"struct_tag_casing,omitempty"` - RelationTag string `toml:"relation_tag,omitempty" json:"relation_tag,omitempty"` - TagIgnore []string `toml:"tag_ignore,omitempty" json:"tag_ignore,omitempty"` + PkgName string `toml:"pkg_name,omitempty" json:"pkg_name,omitempty"` + OutFolder string `toml:"out_folder,omitempty" json:"out_folder,omitempty"` + TemplateDirs []string `toml:"template_dirs,omitempty" json:"template_dirs,omitempty"` + Tags []string `toml:"tags,omitempty" json:"tags,omitempty"` + Replacements []string `toml:"replacements,omitempty" json:"replacements,omitempty"` + Debug bool `toml:"debug,omitempty" json:"debug,omitempty"` + AddGlobal bool `toml:"add_global,omitempty" json:"add_global,omitempty"` + AddPanic bool `toml:"add_panic,omitempty" json:"add_panic,omitempty"` + AddSoftDeletes bool `toml:"add_soft_deletes,omitempty" json:"add_soft_deletes,omitempty"` + AddEnumTypes bool `toml:"add_enum_types,omitempty" json:"add_enum_types,omitempty"` + EnumNullPrefix string `toml:"enum_null_prefix,omitempty" json:"enum_null_prefix,omitempty"` + NoContext bool `toml:"no_context,omitempty" json:"no_context,omitempty"` + NoTests bool `toml:"no_tests,omitempty" json:"no_tests,omitempty"` + NoHooks bool `toml:"no_hooks,omitempty" json:"no_hooks,omitempty"` + NoRelationshipFuncs bool `toml:"no_relationship_funcs,omitempty" json:"no_relationship_funcs,omitempty"` + NoAutoTimestamps bool `toml:"no_auto_timestamps,omitempty" json:"no_auto_timestamps,omitempty"` + NoRowsAffected bool `toml:"no_rows_affected,omitempty" json:"no_rows_affected,omitempty"` + NoDriverTemplates bool `toml:"no_driver_templates,omitempty" json:"no_driver_templates,omitempty"` + NoBackReferencing bool `toml:"no_back_reference,omitempty" json:"no_back_reference,omitempty"` + AlwaysWrapErrors bool `toml:"always_wrap_errors,omitempty" json:"always_wrap_errors,omitempty"` + Wipe bool `toml:"wipe,omitempty" json:"wipe,omitempty"` + StructTagCasing string `toml:"struct_tag_casing,omitempty" json:"struct_tag_casing,omitempty"` + RelationTag string `toml:"relation_tag,omitempty" json:"relation_tag,omitempty"` + TagIgnore []string `toml:"tag_ignore,omitempty" json:"tag_ignore,omitempty"` Imports importers.Collection `toml:"imports,omitempty" json:"imports,omitempty"` diff --git a/boilingcore/output.go b/boilingcore/output.go index c7ce10943..78ba978e0 100644 --- a/boilingcore/output.go +++ b/boilingcore/output.go @@ -246,7 +246,7 @@ func writeFile(outFolder string, fileName string, input *bytes.Buffer, format bo } path := filepath.Join(outFolder, fileName) - if err := testHarnessWriteFile(path, byt, 0664); err != nil { + if err := testHarnessWriteFile(path, byt, 0o664); err != nil { return errors.Wrapf(err, "failed to write output file %s", path) } diff --git a/boilingcore/templates.go b/boilingcore/templates.go index 958c59e91..0b07e8f87 100644 --- a/boilingcore/templates.go +++ b/boilingcore/templates.go @@ -38,18 +38,19 @@ type templateData struct { RQ string // Control various generation features - AddGlobal bool - AddPanic bool - AddSoftDeletes bool - AddEnumTypes bool - EnumNullPrefix string - NoContext bool - NoHooks bool - NoAutoTimestamps bool - NoRowsAffected bool - NoDriverTemplates bool - NoBackReferencing bool - AlwaysWrapErrors bool + AddGlobal bool + AddPanic bool + AddSoftDeletes bool + AddEnumTypes bool + EnumNullPrefix string + NoContext bool + NoHooks bool + NoRelationshipFuncs bool + NoAutoTimestamps bool + NoRowsAffected bool + NoDriverTemplates bool + NoBackReferencing bool + AlwaysWrapErrors bool // Tags control which tags are added to the struct Tags []string diff --git a/go.mod b/go.mod index 7b0b76439..48c5d6e46 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/huandu/xstrings v1.3.2 // indirect github.com/imdario/mergo v0.3.13 // indirect github.com/inconshreveable/mousetrap v1.0.1 // indirect + github.com/kat-co/vala v0.0.0-20170210184112-42e1d8b61f12 github.com/lib/pq v1.10.6 github.com/microsoft/go-mssqldb v0.17.0 github.com/mitchellh/copystructure v1.2.0 // indirect diff --git a/go.sum b/go.sum index 282d58112..e227f681f 100644 --- a/go.sum +++ b/go.sum @@ -298,6 +298,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1 github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= +github.com/kat-co/vala v0.0.0-20170210184112-42e1d8b61f12 h1:DQVOxR9qdYEybJUr/c7ku34r3PfajaMYXZwgDM7KuSk= +github.com/kat-co/vala v0.0.0-20170210184112-42e1d8b61f12/go.mod h1:u9MdXq/QageOOSGp7qG4XAQsYUMP+V5zEel/Vrl6OOc= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= diff --git a/importers/imports.go b/importers/imports.go index adbfa74ed..671fae816 100644 --- a/importers/imports.go +++ b/importers/imports.go @@ -156,7 +156,6 @@ func MapFromInterface(intf interface{}) (Map, error) { m[name] = s return nil }) - if err != nil { return nil, err } @@ -282,6 +281,11 @@ func NewDefaultImports() Collection { `"testing"`, }, }, + "boil_relationship_test": { + Standard: List{ + `"testing"`, + }, + }, } return col diff --git a/main.go b/main.go index 4f4840b8a..d605b9d71 100644 --- a/main.go +++ b/main.go @@ -97,6 +97,7 @@ func main() { rootCmd.PersistentFlags().BoolP("no-context", "", false, "Disable context.Context usage in the generated code") rootCmd.PersistentFlags().BoolP("no-tests", "", false, "Disable generated go test files") rootCmd.PersistentFlags().BoolP("no-hooks", "", false, "Disable hooks feature for your models") + rootCmd.PersistentFlags().BoolP("no-relationship-funcs", "", false, "Disable relationship loaders and setters feature for your models") rootCmd.PersistentFlags().BoolP("no-rows-affected", "", false, "Disable rows affected in the generated API") rootCmd.PersistentFlags().BoolP("no-auto-timestamps", "", false, "Disable automatic timestamps for created_at/updated_at") rootCmd.PersistentFlags().BoolP("no-driver-templates", "", false, "Disable parsing of templates defined by the database driver") @@ -153,32 +154,33 @@ func preRun(cmd *cobra.Command, args []string) error { } cmdConfig = &boilingcore.Config{ - DriverName: driverName, - OutFolder: viper.GetString("output"), - PkgName: viper.GetString("pkgname"), - Debug: viper.GetBool("debug"), - AddGlobal: viper.GetBool("add-global-variants"), - AddPanic: viper.GetBool("add-panic-variants"), - AddSoftDeletes: viper.GetBool("add-soft-deletes"), - AddEnumTypes: viper.GetBool("add-enum-types"), - EnumNullPrefix: viper.GetString("enum-null-prefix"), - NoContext: viper.GetBool("no-context"), - NoTests: viper.GetBool("no-tests"), - NoHooks: viper.GetBool("no-hooks"), - NoRowsAffected: viper.GetBool("no-rows-affected"), - NoAutoTimestamps: viper.GetBool("no-auto-timestamps"), - NoDriverTemplates: viper.GetBool("no-driver-templates"), - NoBackReferencing: viper.GetBool("no-back-referencing"), - AlwaysWrapErrors: viper.GetBool("always-wrap-errors"), - Wipe: viper.GetBool("wipe"), - StructTagCasing: strings.ToLower(viper.GetString("struct-tag-casing")), // camel | snake | title - TagIgnore: viper.GetStringSlice("tag-ignore"), - RelationTag: viper.GetString("relation-tag"), - TemplateDirs: viper.GetStringSlice("templates"), - Tags: viper.GetStringSlice("tag"), - Replacements: viper.GetStringSlice("replace"), - Aliases: boilingcore.ConvertAliases(viper.Get("aliases")), - TypeReplaces: boilingcore.ConvertTypeReplace(viper.Get("types")), + DriverName: driverName, + OutFolder: viper.GetString("output"), + PkgName: viper.GetString("pkgname"), + Debug: viper.GetBool("debug"), + AddGlobal: viper.GetBool("add-global-variants"), + AddPanic: viper.GetBool("add-panic-variants"), + AddSoftDeletes: viper.GetBool("add-soft-deletes"), + AddEnumTypes: viper.GetBool("add-enum-types"), + EnumNullPrefix: viper.GetString("enum-null-prefix"), + NoContext: viper.GetBool("no-context"), + NoTests: viper.GetBool("no-tests"), + NoHooks: viper.GetBool("no-hooks"), + NoRelationshipFuncs: viper.GetBool("no-relationship-funcs"), + NoRowsAffected: viper.GetBool("no-rows-affected"), + NoAutoTimestamps: viper.GetBool("no-auto-timestamps"), + NoDriverTemplates: viper.GetBool("no-driver-templates"), + NoBackReferencing: viper.GetBool("no-back-referencing"), + AlwaysWrapErrors: viper.GetBool("always-wrap-errors"), + Wipe: viper.GetBool("wipe"), + StructTagCasing: strings.ToLower(viper.GetString("struct-tag-casing")), // camel | snake | title + TagIgnore: viper.GetStringSlice("tag-ignore"), + RelationTag: viper.GetString("relation-tag"), + TemplateDirs: viper.GetStringSlice("templates"), + Tags: viper.GetStringSlice("tag"), + Replacements: viper.GetStringSlice("replace"), + Aliases: boilingcore.ConvertAliases(viper.Get("aliases")), + TypeReplaces: boilingcore.ConvertTypeReplace(viper.Get("types")), AutoColumns: boilingcore.AutoColumns{ Created: viper.GetString("auto-columns.created"), Updated: viper.GetString("auto-columns.updated"), diff --git a/templates/main/04_relationship_to_one.go.tpl b/templates/main/04_relationship_to_one.go.tpl index 52eea0927..d78b2abad 100644 --- a/templates/main/04_relationship_to_one.go.tpl +++ b/templates/main/04_relationship_to_one.go.tpl @@ -1,3 +1,4 @@ +{{- if not .NoRelationshipFuncs -}} {{- if or .Table.IsJoinTable .Table.IsView -}} {{- else -}} {{- range $fkey := .Table.FKeys -}} @@ -17,3 +18,4 @@ func (o *{{$ltable.UpSingular}}) {{$rel.Foreign}}(mods ...qm.QueryMod) ({{$ftabl } {{- end -}} {{- end -}} +{{- end -}}{{/* no relationship */}} diff --git a/templates/main/05_relationship_one_to_one.go.tpl b/templates/main/05_relationship_one_to_one.go.tpl index 718516e32..c1724679d 100644 --- a/templates/main/05_relationship_one_to_one.go.tpl +++ b/templates/main/05_relationship_one_to_one.go.tpl @@ -1,3 +1,4 @@ +{{- if not .NoRelationshipFuncs -}} {{- if or .Table.IsJoinTable .Table.IsView -}} {{- else -}} {{- range $rel := .Table.ToOneRelationships -}} @@ -17,3 +18,4 @@ func (o *{{$ltable.UpSingular}}) {{$relAlias.Local}}(mods ...qm.QueryMod) ({{$ft } {{- end -}} {{- end -}} +{{- end -}}{{/* no relationship */}} diff --git a/templates/main/06_relationship_to_many.go.tpl b/templates/main/06_relationship_to_many.go.tpl index a59bbf175..2f241b128 100644 --- a/templates/main/06_relationship_to_many.go.tpl +++ b/templates/main/06_relationship_to_many.go.tpl @@ -1,3 +1,4 @@ +{{- if not .NoRelationshipFuncs -}} {{- if or .Table.IsJoinTable .Table.IsView -}} {{- else -}} {{- range $rel := .Table.ToManyRelationships -}} @@ -31,3 +32,4 @@ func (o *{{$ltable.UpSingular}}) {{$relAlias.Local}}(mods ...qm.QueryMod) {{$fta {{end -}}{{- /* range relationships */ -}} {{- end -}}{{- /* if isJoinTable */ -}} +{{- end -}}{{/* no relationship */}} diff --git a/templates/main/07_relationship_to_one_eager.go.tpl b/templates/main/07_relationship_to_one_eager.go.tpl index 68b97ca8c..72413ff77 100644 --- a/templates/main/07_relationship_to_one_eager.go.tpl +++ b/templates/main/07_relationship_to_one_eager.go.tpl @@ -1,3 +1,4 @@ +{{- if not .NoRelationshipFuncs -}} {{- if or .Table.IsJoinTable .Table.IsView -}} {{- else -}} {{- range $fkey := .Table.FKeys -}} @@ -81,12 +82,12 @@ func ({{$ltable.DownSingular}}L) Load{{$rel.Foreign}}({{if $.NoContext}}e boil.E } query := NewQuery( - qm.From(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}`), - qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, args...), - {{if and $.AddSoftDeletes $canSoftDelete -}} - qmhelper.WhereIsNull(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{or $.AutoColumns.Deleted "deleted_at"}}`), - {{- end}} - ) + qm.From(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}`), + qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, args...), + {{if and $.AddSoftDeletes $canSoftDelete -}} + qmhelper.WhereIsNull(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{or $.AutoColumns.Deleted "deleted_at"}}`), + {{- end}} + ) if mods != nil { mods.Apply(query) } @@ -169,3 +170,4 @@ func ({{$ltable.DownSingular}}L) Load{{$rel.Foreign}}({{if $.NoContext}}e boil.E } {{end -}}{{/* range */}} {{end}}{{/* join table */}} +{{- end -}}{{/* no relationship */}} diff --git a/templates/main/08_relationship_one_to_one_eager.go.tpl b/templates/main/08_relationship_one_to_one_eager.go.tpl index b6fce0f9a..382a9fa82 100644 --- a/templates/main/08_relationship_one_to_one_eager.go.tpl +++ b/templates/main/08_relationship_one_to_one_eager.go.tpl @@ -1,3 +1,4 @@ +{{- if not .NoRelationshipFuncs -}} {{- if or .Table.IsJoinTable .Table.IsView -}} {{- else -}} {{- range $rel := .Table.ToOneRelationships -}} @@ -69,12 +70,12 @@ func ({{$ltable.DownSingular}}L) Load{{$relAlias.Local}}({{if $.NoContext}}e boi } query := NewQuery( - qm.From(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}`), - qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, args...), - {{if and $.AddSoftDeletes $canSoftDelete -}} - qmhelper.WhereIsNull(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{or $.AutoColumns.Deleted "deleted_at"}}`), - {{- end}} - ) + qm.From(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}`), + qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, args...), + {{if and $.AddSoftDeletes $canSoftDelete -}} + qmhelper.WhereIsNull(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{or $.AutoColumns.Deleted "deleted_at"}}`), + {{- end}} + ) if mods != nil { mods.Apply(query) } @@ -148,3 +149,4 @@ func ({{$ltable.DownSingular}}L) Load{{$relAlias.Local}}({{if $.NoContext}}e boi } {{end -}}{{/* range */}} {{end}}{{/* join table */}} +{{- end -}}{{/* no relationship */}} diff --git a/templates/main/09_relationship_to_many_eager.go.tpl b/templates/main/09_relationship_to_many_eager.go.tpl index 56f3379e9..3361a7379 100644 --- a/templates/main/09_relationship_to_many_eager.go.tpl +++ b/templates/main/09_relationship_to_many_eager.go.tpl @@ -1,3 +1,4 @@ +{{- if not .NoRelationshipFuncs -}} {{- if or .Table.IsJoinTable .Table.IsView -}} {{- else -}} {{- range $rel := .Table.ToManyRelationships -}} @@ -83,12 +84,12 @@ func ({{$ltable.DownSingular}}L) Load{{$relAlias.Local}}({{if $.NoContext}}e boi ) {{else -}} query := NewQuery( - qm.From(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}`), - qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, args...), - {{if and $.AddSoftDeletes $canSoftDelete -}} - qmhelper.WhereIsNull(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{or $.AutoColumns.Deleted "deleted_at"}}`), - {{- end}} - ) + qm.From(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}`), + qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, args...), + {{if and $.AddSoftDeletes $canSoftDelete -}} + qmhelper.WhereIsNull(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{or $.AutoColumns.Deleted "deleted_at"}}`), + {{- end}} + ) {{end -}} if mods != nil { mods.Apply(query) @@ -210,3 +211,4 @@ func ({{$ltable.DownSingular}}L) Load{{$relAlias.Local}}({{if $.NoContext}}e boi {{end -}}{{/* range tomany */}} {{- end -}}{{/* if IsJoinTable */}} +{{- end -}}{{/* no relationship */}} diff --git a/templates/main/10_relationship_to_one_setops.go.tpl b/templates/main/10_relationship_to_one_setops.go.tpl index 50a98d7e8..8cf45ca77 100644 --- a/templates/main/10_relationship_to_one_setops.go.tpl +++ b/templates/main/10_relationship_to_one_setops.go.tpl @@ -1,3 +1,4 @@ +{{- if not .NoRelationshipFuncs -}} {{- if or .Table.IsJoinTable .Table.IsView -}} {{- else -}} {{- range $fkey := .Table.FKeys -}} @@ -228,3 +229,4 @@ func (o *{{$ltable.UpSingular}}) Remove{{$rel.Foreign}}({{if $.NoContext}}exec b {{end -}}{{/* if foreignkey nullable */}} {{- end -}}{{/* range */}} {{- end -}}{{/* join table */}} +{{- end -}}{{/* no relationship */}} diff --git a/templates/main/11_relationship_one_to_one_setops.go.tpl b/templates/main/11_relationship_one_to_one_setops.go.tpl index 462f1135d..2f07afac0 100644 --- a/templates/main/11_relationship_one_to_one_setops.go.tpl +++ b/templates/main/11_relationship_one_to_one_setops.go.tpl @@ -1,3 +1,4 @@ +{{- if not .NoRelationshipFuncs -}} {{- if or .Table.IsJoinTable .Table.IsView -}} {{- else -}} {{- range $rel := .Table.ToOneRelationships -}} @@ -202,3 +203,4 @@ func (o *{{$ltable.UpSingular}}) Remove{{$relAlias.Local}}({{if $.NoContext}}exe {{end -}}{{/* if foreignkey nullable */}} {{- end -}}{{/* range */}} {{- end -}}{{/* join table */}} +{{- end -}}{{/* no relationship */}} diff --git a/templates/main/12_relationship_to_many_setops.go.tpl b/templates/main/12_relationship_to_many_setops.go.tpl index ce77375ff..90818f78b 100644 --- a/templates/main/12_relationship_to_many_setops.go.tpl +++ b/templates/main/12_relationship_to_many_setops.go.tpl @@ -1,3 +1,4 @@ +{{- if not .NoRelationshipFuncs -}} {{- if or .Table.IsJoinTable .Table.IsView -}} {{- else -}} {{- $table := .Table -}} @@ -446,3 +447,4 @@ func remove{{$relAlias.Local}}From{{$relAlias.Foreign}}Slice(o *{{$ltable.UpSing {{- end -}}{{- /* if nullable foreign key */ -}} {{- end -}}{{- /* range relationships */ -}} {{- end -}}{{- /* if IsJoinTable */ -}} +{{- end -}}{{/* no relationship */}} diff --git a/templates/test/relationship_one_to_one.go.tpl b/templates/test/relationship_one_to_one.go.tpl index b00f40145..affddc5dd 100644 --- a/templates/test/relationship_one_to_one.go.tpl +++ b/templates/test/relationship_one_to_one.go.tpl @@ -1,3 +1,4 @@ +{{- if not .NoRelationshipFuncs -}} {{- if .Table.IsJoinTable -}} {{- else -}} {{- range $rel := .Table.ToOneRelationships -}} @@ -82,3 +83,4 @@ func test{{$ltable.UpSingular}}OneToOne{{$ftable.UpSingular}}Using{{$relAlias.Lo {{end -}}{{/* range */}} {{- end -}}{{/* join table */}} +{{- end -}}{{/* no relationship */}} diff --git a/templates/test/relationship_one_to_one_setops.go.tpl b/templates/test/relationship_one_to_one_setops.go.tpl index 832621258..d5a7d1233 100644 --- a/templates/test/relationship_one_to_one_setops.go.tpl +++ b/templates/test/relationship_one_to_one_setops.go.tpl @@ -1,3 +1,4 @@ +{{- if not .NoRelationshipFuncs -}} {{- if .Table.IsJoinTable -}} {{- else -}} {{- range $rel := .Table.ToOneRelationships -}} @@ -141,3 +142,4 @@ func test{{$ltable.UpSingular}}OneToOneRemoveOp{{$ftable.UpSingular}}Using{{$rel {{end -}}{{/* end if foreign key nullable */}} {{- end -}}{{/* range */}} {{- end -}}{{/* join table */}} +{{- end -}}{{/* no relationship */}} diff --git a/templates/test/relationship_to_many.go.tpl b/templates/test/relationship_to_many.go.tpl index 1eacd53ac..de7b91520 100644 --- a/templates/test/relationship_to_many.go.tpl +++ b/templates/test/relationship_to_many.go.tpl @@ -1,3 +1,4 @@ +{{- if not .NoRelationshipFuncs -}} {{- if .Table.IsJoinTable -}} {{- else -}} {{- $table := .Table }} @@ -115,3 +116,4 @@ func test{{$ltable.UpSingular}}ToMany{{$relAlias.Local}}(t *testing.T) { {{end -}}{{- /* range */ -}} {{- end -}}{{- /* outer if join table */ -}} +{{- end -}}{{/* no relationship */}} diff --git a/templates/test/relationship_to_many_setops.go.tpl b/templates/test/relationship_to_many_setops.go.tpl index 3accbfab8..1c794c795 100644 --- a/templates/test/relationship_to_many_setops.go.tpl +++ b/templates/test/relationship_to_many_setops.go.tpl @@ -1,3 +1,4 @@ +{{- if not .NoRelationshipFuncs -}} {{- if .Table.IsJoinTable -}} {{- else -}} {{- $table := .Table -}} @@ -328,3 +329,4 @@ func test{{$ltable.UpSingular}}ToManyRemoveOp{{$relAlias.Local}}(t *testing.T) { {{end -}} {{- end -}}{{- /* range relationships */ -}} {{- end -}}{{- /* outer if join table */ -}} +{{- end -}}{{/* no relationship */}} diff --git a/templates/test/relationship_to_one.go.tpl b/templates/test/relationship_to_one.go.tpl index c3e9b7a5a..ab090b02f 100644 --- a/templates/test/relationship_to_one.go.tpl +++ b/templates/test/relationship_to_one.go.tpl @@ -1,3 +1,4 @@ +{{- if not .NoRelationshipFuncs -}} {{- if .Table.IsJoinTable -}} {{- else -}} {{- range $fkey := .Table.FKeys -}} @@ -82,3 +83,4 @@ func test{{$ltable.UpSingular}}ToOne{{$ftable.UpSingular}}Using{{$rel.Foreign}}( {{end -}}{{/* range */}} {{- end -}}{{/* join table */}} +{{- end -}}{{/* no relationship */}} diff --git a/templates/test/relationship_to_one_setops.go.tpl b/templates/test/relationship_to_one_setops.go.tpl index 9ef778787..55028367e 100644 --- a/templates/test/relationship_to_one_setops.go.tpl +++ b/templates/test/relationship_to_one_setops.go.tpl @@ -1,3 +1,4 @@ +{{- if not .NoRelationshipFuncs -}} {{- if .Table.IsJoinTable -}} {{- else -}} {{- range $fkey := .Table.FKeys -}} @@ -148,3 +149,4 @@ func test{{$ltable.UpSingular}}ToOneRemoveOp{{$ftable.UpSingular}}Using{{$rel.Fo {{end -}}{{/* end if foreign key nullable */}} {{- end -}}{{/* range */}} {{- end -}}{{/* join table */}} +{{- end -}}{{/* no relationship */}} diff --git a/templates/test/singleton/boil_relationship_test.go.tpl b/templates/test/singleton/boil_relationship_test.go.tpl new file mode 100644 index 000000000..cb75cc103 --- /dev/null +++ b/templates/test/singleton/boil_relationship_test.go.tpl @@ -0,0 +1,169 @@ +{{- if not .NoRelationshipFuncs -}} + +// TestToOne tests cannot be run in parallel +// or deadlocks can occur. +func TestToOne(t *testing.T) { +{{- range .Tables}} + {{- if or .IsJoinTable .IsView -}} + {{- else -}} + {{- range $fkey := .FKeys -}} + {{- $ltable := $.Aliases.Table $fkey.Table -}} + {{- $ftable := $.Aliases.Table $fkey.ForeignTable -}} + {{- $relAlias := $ltable.Relationship $fkey.Name -}} + t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Foreign}}", test{{$ltable.UpSingular}}ToOne{{$ftable.UpSingular}}Using{{$relAlias.Foreign}}) + {{end -}}{{- /* fkey range */ -}} + {{- end -}}{{- /* if join table */ -}} +{{- end -}}{{- /* tables range */ -}} +} + +// TestOneToOne tests cannot be run in parallel +// or deadlocks can occur. +func TestOneToOne(t *testing.T) { + {{- range .Tables}} + {{- if or .IsJoinTable .IsView -}} + {{- else -}} + {{- range $rel := .ToOneRelationships -}} + {{- $ltable := $.Aliases.Table $rel.Table -}} + {{- $ftable := $.Aliases.Table $rel.ForeignTable -}} + {{- $relAlias := $ftable.Relationship $rel.Name -}} + t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Local}}", test{{$ltable.UpSingular}}OneToOne{{$ftable.UpSingular}}Using{{$relAlias.Local}}) + {{end -}}{{- /* range */ -}} + {{- end -}}{{- /* outer if join table */ -}} + {{- end -}}{{- /* outer tables range */ -}} +} + +// TestToMany tests cannot be run in parallel +// or deadlocks can occur. +func TestToMany(t *testing.T) { + {{- range .Tables}} + {{- if or .IsJoinTable .IsView -}} + {{- else -}} + {{- range $rel := .ToManyRelationships -}} + {{- $ltable := $.Aliases.Table $rel.Table -}} + {{- $relAlias := $.Aliases.ManyRelationship $rel.ForeignTable $rel.Name $rel.JoinTable $rel.JoinLocalFKeyName -}} + t.Run("{{$ltable.UpSingular}}To{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToMany{{$relAlias.Local}}) + {{end -}}{{- /* range */ -}} + {{- end -}}{{- /* outer if join table */ -}} + {{- end -}}{{- /* outer tables range */ -}} +} + +// TestToOneSet tests cannot be run in parallel +// or deadlocks can occur. +func TestToOneSet(t *testing.T) { +{{- range .Tables}} + {{- if or .IsJoinTable .IsView -}} + {{- else -}} + {{- range $fkey := .FKeys -}} + {{- $ltable := $.Aliases.Table $fkey.Table -}} + {{- $ftable := $.Aliases.Table $fkey.ForeignTable -}} + {{- $relAlias := $ltable.Relationship $fkey.Name -}} + t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToOneSetOp{{$ftable.UpSingular}}Using{{$relAlias.Foreign}}) + {{end -}}{{- /* fkey range */ -}} + {{- end -}}{{- /* if join table */ -}} +{{- end -}}{{- /* tables range */ -}} +} + +// TestToOneRemove tests cannot be run in parallel +// or deadlocks can occur. +func TestToOneRemove(t *testing.T) { +{{- range .Tables}} + {{- if or .IsJoinTable .IsView -}} + {{- else -}} + {{- range $fkey := .FKeys -}} + {{- if $fkey.Nullable -}} + {{- $ltable := $.Aliases.Table $fkey.Table -}} + {{- $ftable := $.Aliases.Table $fkey.ForeignTable -}} + {{- $relAlias := $ltable.Relationship $fkey.Name -}} + t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToOneRemoveOp{{$ftable.UpSingular}}Using{{$relAlias.Foreign}}) + {{end -}}{{- /* if foreign key nullable */ -}} + {{- end -}}{{- /* fkey range */ -}} + {{- end -}}{{- /* if join table */ -}} +{{- end -}}{{- /* tables range */ -}} +} + +// TestOneToOneSet tests cannot be run in parallel +// or deadlocks can occur. +func TestOneToOneSet(t *testing.T) { + {{- range .Tables}} + {{- if or .IsJoinTable .IsView -}} + {{- else -}} + {{- range $rel := .ToOneRelationships -}} + {{- $ltable := $.Aliases.Table $rel.Table -}} + {{- $ftable := $.Aliases.Table $rel.ForeignTable -}} + {{- $relAlias := $ftable.Relationship $rel.Name -}} + t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Local}}", test{{$ltable.UpSingular}}OneToOneSetOp{{$ftable.UpSingular}}Using{{$relAlias.Local}}) + {{end -}}{{- /* range to one relationships */ -}} + {{- end -}}{{- /* outer if join table */ -}} + {{- end -}}{{- /* outer tables range */ -}} +} + +// TestOneToOneRemove tests cannot be run in parallel +// or deadlocks can occur. +func TestOneToOneRemove(t *testing.T) { + {{- range .Tables}} + {{- if or .IsJoinTable .IsView -}} + {{- else -}} + {{- range $rel := .ToOneRelationships -}} + {{- if $rel.ForeignColumnNullable -}} + {{- $ltable := $.Aliases.Table $rel.Table -}} + {{- $ftable := $.Aliases.Table $rel.ForeignTable -}} + {{- $relAlias := $ftable.Relationship $rel.Name -}} + t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Local}}", test{{$ltable.UpSingular}}OneToOneRemoveOp{{$ftable.UpSingular}}Using{{$relAlias.Local}}) + {{end -}}{{- /* if foreign column nullable */ -}} + {{- end -}}{{- /* range */ -}} + {{- end -}}{{- /* outer if join table */ -}} + {{- end -}}{{- /* outer tables range */ -}} +} + +// TestToManyAdd tests cannot be run in parallel +// or deadlocks can occur. +func TestToManyAdd(t *testing.T) { + {{- range .Tables}} + {{- if or .IsJoinTable .IsView -}} + {{- else -}} + {{- range $rel := .ToManyRelationships -}} + {{- $ltable := $.Aliases.Table $rel.Table -}} + {{- $relAlias := $.Aliases.ManyRelationship $rel.ForeignTable $rel.Name $rel.JoinTable $rel.JoinLocalFKeyName -}} + t.Run("{{$ltable.UpSingular}}To{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToManyAddOp{{$relAlias.Local}}) + {{end -}}{{- /* range */ -}} + {{- end -}}{{- /* outer if join table */ -}} + {{- end -}}{{- /* outer tables range */ -}} +} + +// TestToManySet tests cannot be run in parallel +// or deadlocks can occur. +func TestToManySet(t *testing.T) { + {{- range .Tables}} + {{- if or .IsJoinTable .IsView -}} + {{- else -}} + {{- range $rel := .ToManyRelationships -}} + {{- if not (or $rel.ForeignColumnNullable $rel.ToJoinTable)}} + {{- else -}} + {{- $ltable := $.Aliases.Table $rel.Table -}} + {{- $relAlias := $.Aliases.ManyRelationship $rel.ForeignTable $rel.Name $rel.JoinTable $rel.JoinLocalFKeyName -}} + t.Run("{{$ltable.UpSingular}}To{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToManySetOp{{$relAlias.Local}}) + {{end -}}{{- /* if foreign column nullable */ -}} + {{- end -}}{{- /* range */ -}} + {{- end -}}{{- /* outer if join table */ -}} + {{- end -}}{{- /* outer tables range */ -}} +} + +// TestToManyRemove tests cannot be run in parallel +// or deadlocks can occur. +func TestToManyRemove(t *testing.T) { + {{- range .Tables}} + {{- if or .IsJoinTable .IsView -}} + {{- else -}} + {{- range $rel := .ToManyRelationships -}} + {{- if not (or $rel.ForeignColumnNullable $rel.ToJoinTable)}} + {{- else -}} + {{- $ltable := $.Aliases.Table $rel.Table -}} + {{- $relAlias := $.Aliases.ManyRelationship $rel.ForeignTable $rel.Name $rel.JoinTable $rel.JoinLocalFKeyName -}} + t.Run("{{$ltable.UpSingular}}To{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToManyRemoveOp{{$relAlias.Local}}) + {{end -}}{{- /* if foreign column nullable */ -}} + {{- end -}}{{- /* range */ -}} + {{- end -}}{{- /* outer if join table */ -}} + {{- end -}}{{- /* outer tables range */ -}} +} + +{{- end -}}{{/* no relationship */}} diff --git a/templates/test/singleton/boil_suites_test.go.tpl b/templates/test/singleton/boil_suites_test.go.tpl index cdaea8508..f60abe76c 100644 --- a/templates/test/singleton/boil_suites_test.go.tpl +++ b/templates/test/singleton/boil_suites_test.go.tpl @@ -165,172 +165,6 @@ func TestInsert(t *testing.T) { {{- end -}} } -// TestToOne tests cannot be run in parallel -// or deadlocks can occur. -func TestToOne(t *testing.T) { -{{- range .Tables}} - {{- if or .IsJoinTable .IsView -}} - {{- else -}} - {{- range $fkey := .FKeys -}} - {{- $ltable := $.Aliases.Table $fkey.Table -}} - {{- $ftable := $.Aliases.Table $fkey.ForeignTable -}} - {{- $relAlias := $ltable.Relationship $fkey.Name -}} - t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Foreign}}", test{{$ltable.UpSingular}}ToOne{{$ftable.UpSingular}}Using{{$relAlias.Foreign}}) - {{end -}}{{- /* fkey range */ -}} - {{- end -}}{{- /* if join table */ -}} -{{- end -}}{{- /* tables range */ -}} -} - -// TestOneToOne tests cannot be run in parallel -// or deadlocks can occur. -func TestOneToOne(t *testing.T) { - {{- range .Tables}} - {{- if or .IsJoinTable .IsView -}} - {{- else -}} - {{- range $rel := .ToOneRelationships -}} - {{- $ltable := $.Aliases.Table $rel.Table -}} - {{- $ftable := $.Aliases.Table $rel.ForeignTable -}} - {{- $relAlias := $ftable.Relationship $rel.Name -}} - t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Local}}", test{{$ltable.UpSingular}}OneToOne{{$ftable.UpSingular}}Using{{$relAlias.Local}}) - {{end -}}{{- /* range */ -}} - {{- end -}}{{- /* outer if join table */ -}} - {{- end -}}{{- /* outer tables range */ -}} -} - -// TestToMany tests cannot be run in parallel -// or deadlocks can occur. -func TestToMany(t *testing.T) { - {{- range .Tables}} - {{- if or .IsJoinTable .IsView -}} - {{- else -}} - {{- range $rel := .ToManyRelationships -}} - {{- $ltable := $.Aliases.Table $rel.Table -}} - {{- $relAlias := $.Aliases.ManyRelationship $rel.ForeignTable $rel.Name $rel.JoinTable $rel.JoinLocalFKeyName -}} - t.Run("{{$ltable.UpSingular}}To{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToMany{{$relAlias.Local}}) - {{end -}}{{- /* range */ -}} - {{- end -}}{{- /* outer if join table */ -}} - {{- end -}}{{- /* outer tables range */ -}} -} - -// TestToOneSet tests cannot be run in parallel -// or deadlocks can occur. -func TestToOneSet(t *testing.T) { -{{- range .Tables}} - {{- if or .IsJoinTable .IsView -}} - {{- else -}} - {{- range $fkey := .FKeys -}} - {{- $ltable := $.Aliases.Table $fkey.Table -}} - {{- $ftable := $.Aliases.Table $fkey.ForeignTable -}} - {{- $relAlias := $ltable.Relationship $fkey.Name -}} - t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToOneSetOp{{$ftable.UpSingular}}Using{{$relAlias.Foreign}}) - {{end -}}{{- /* fkey range */ -}} - {{- end -}}{{- /* if join table */ -}} -{{- end -}}{{- /* tables range */ -}} -} - -// TestToOneRemove tests cannot be run in parallel -// or deadlocks can occur. -func TestToOneRemove(t *testing.T) { -{{- range .Tables}} - {{- if or .IsJoinTable .IsView -}} - {{- else -}} - {{- range $fkey := .FKeys -}} - {{- if $fkey.Nullable -}} - {{- $ltable := $.Aliases.Table $fkey.Table -}} - {{- $ftable := $.Aliases.Table $fkey.ForeignTable -}} - {{- $relAlias := $ltable.Relationship $fkey.Name -}} - t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToOneRemoveOp{{$ftable.UpSingular}}Using{{$relAlias.Foreign}}) - {{end -}}{{- /* if foreign key nullable */ -}} - {{- end -}}{{- /* fkey range */ -}} - {{- end -}}{{- /* if join table */ -}} -{{- end -}}{{- /* tables range */ -}} -} - -// TestOneToOneSet tests cannot be run in parallel -// or deadlocks can occur. -func TestOneToOneSet(t *testing.T) { - {{- range .Tables}} - {{- if or .IsJoinTable .IsView -}} - {{- else -}} - {{- range $rel := .ToOneRelationships -}} - {{- $ltable := $.Aliases.Table $rel.Table -}} - {{- $ftable := $.Aliases.Table $rel.ForeignTable -}} - {{- $relAlias := $ftable.Relationship $rel.Name -}} - t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Local}}", test{{$ltable.UpSingular}}OneToOneSetOp{{$ftable.UpSingular}}Using{{$relAlias.Local}}) - {{end -}}{{- /* range to one relationships */ -}} - {{- end -}}{{- /* outer if join table */ -}} - {{- end -}}{{- /* outer tables range */ -}} -} - -// TestOneToOneRemove tests cannot be run in parallel -// or deadlocks can occur. -func TestOneToOneRemove(t *testing.T) { - {{- range .Tables}} - {{- if or .IsJoinTable .IsView -}} - {{- else -}} - {{- range $rel := .ToOneRelationships -}} - {{- if $rel.ForeignColumnNullable -}} - {{- $ltable := $.Aliases.Table $rel.Table -}} - {{- $ftable := $.Aliases.Table $rel.ForeignTable -}} - {{- $relAlias := $ftable.Relationship $rel.Name -}} - t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Local}}", test{{$ltable.UpSingular}}OneToOneRemoveOp{{$ftable.UpSingular}}Using{{$relAlias.Local}}) - {{end -}}{{- /* if foreign column nullable */ -}} - {{- end -}}{{- /* range */ -}} - {{- end -}}{{- /* outer if join table */ -}} - {{- end -}}{{- /* outer tables range */ -}} -} - -// TestToManyAdd tests cannot be run in parallel -// or deadlocks can occur. -func TestToManyAdd(t *testing.T) { - {{- range .Tables}} - {{- if or .IsJoinTable .IsView -}} - {{- else -}} - {{- range $rel := .ToManyRelationships -}} - {{- $ltable := $.Aliases.Table $rel.Table -}} - {{- $relAlias := $.Aliases.ManyRelationship $rel.ForeignTable $rel.Name $rel.JoinTable $rel.JoinLocalFKeyName -}} - t.Run("{{$ltable.UpSingular}}To{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToManyAddOp{{$relAlias.Local}}) - {{end -}}{{- /* range */ -}} - {{- end -}}{{- /* outer if join table */ -}} - {{- end -}}{{- /* outer tables range */ -}} -} - -// TestToManySet tests cannot be run in parallel -// or deadlocks can occur. -func TestToManySet(t *testing.T) { - {{- range .Tables}} - {{- if or .IsJoinTable .IsView -}} - {{- else -}} - {{- range $rel := .ToManyRelationships -}} - {{- if not (or $rel.ForeignColumnNullable $rel.ToJoinTable)}} - {{- else -}} - {{- $ltable := $.Aliases.Table $rel.Table -}} - {{- $relAlias := $.Aliases.ManyRelationship $rel.ForeignTable $rel.Name $rel.JoinTable $rel.JoinLocalFKeyName -}} - t.Run("{{$ltable.UpSingular}}To{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToManySetOp{{$relAlias.Local}}) - {{end -}}{{- /* if foreign column nullable */ -}} - {{- end -}}{{- /* range */ -}} - {{- end -}}{{- /* outer if join table */ -}} - {{- end -}}{{- /* outer tables range */ -}} -} - -// TestToManyRemove tests cannot be run in parallel -// or deadlocks can occur. -func TestToManyRemove(t *testing.T) { - {{- range .Tables}} - {{- if or .IsJoinTable .IsView -}} - {{- else -}} - {{- range $rel := .ToManyRelationships -}} - {{- if not (or $rel.ForeignColumnNullable $rel.ToJoinTable)}} - {{- else -}} - {{- $ltable := $.Aliases.Table $rel.Table -}} - {{- $relAlias := $.Aliases.ManyRelationship $rel.ForeignTable $rel.Name $rel.JoinTable $rel.JoinLocalFKeyName -}} - t.Run("{{$ltable.UpSingular}}To{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToManyRemoveOp{{$relAlias.Local}}) - {{end -}}{{- /* if foreign column nullable */ -}} - {{- end -}}{{- /* range */ -}} - {{- end -}}{{- /* outer if join table */ -}} - {{- end -}}{{- /* outer tables range */ -}} -} - func TestReload(t *testing.T) { {{- range .Tables}} {{- if or .IsJoinTable .IsView -}}