entc/gen: ignore immutable fields on Upsert<T>.UpdateNewValues

Also, for some reason, the TimeMixin.UpdateTime was an immutable field,
but this was incorrent, because the codegen just skip generating
update setters to it. Removing the Immutable modifier allows users
to set this field explicitly.
This commit is contained in:
Ariel Mashraki
2021-10-05 22:27:58 +03:00
committed by Ariel Mashraki
parent b47b46ee6d
commit 9e809635b2
36 changed files with 156 additions and 72 deletions

View File

@@ -143,7 +143,7 @@ type (
{{ end }}
// UpdateNewValues updates the fields using the new values that were set on create{{ if $.ID.UserDefined }} except the ID field{{ end }}.
// UpdateNewValues updates the mutable fields using the new values that were set on create{{ if $.ID.UserDefined }} except the ID field{{ end }}.
// Using this option is equivalent to using:
//
// client.{{ $.Name }}.Create().
@@ -159,11 +159,18 @@ type (
//
func (u *{{ $upsertOne }}) UpdateNewValues() *{{ $upsertOne }} {
u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
{{- if $.ID.UserDefined }}
{{- if or $.ID.UserDefined $.ImmutableFields }}
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
if _, exists := u.create.mutation.ID(); exists {
s.SetIgnore({{ $.Package }}.{{ $.ID.Constant }})
}
{{- if $.ID.UserDefined }}
if _, exists := u.create.mutation.ID(); exists {
s.SetIgnore({{ $.Package }}.{{ $.ID.Constant }})
}
{{- end }}
{{- range $f := $.ImmutableFields }}
if _, exists := u.create.mutation.{{ $f.MutationGet }}(); exists {
s.SetIgnore({{ $.Package }}.{{ $f.Constant }})
}
{{- end }}
}))
{{- end }}
return u
@@ -306,7 +313,7 @@ type {{ $upsertBulk }} struct {
}
// UpdateNewValues updates the fields using the new values that
// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
// client.{{ $.Name }}.Create().
@@ -322,13 +329,20 @@ type {{ $upsertBulk }} struct {
//
func (u *{{ $upsertBulk }}) UpdateNewValues() *{{ $upsertBulk }} {
u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
{{- if $.ID.UserDefined }}
{{- if or $.ID.UserDefined $.ImmutableFields }}
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
for _, b := range u.create.builders {
if _, exists := b.mutation.ID(); exists {
s.SetIgnore({{ $.Package }}.{{ $.ID.Constant }})
return
}
{{- if $.ID.UserDefined }}
if _, exists := b.mutation.ID(); exists {
s.SetIgnore({{ $.Package }}.{{ $.ID.Constant }})
return
}
{{- end }}
{{- range $f := $.ImmutableFields }}
if _, exists := b.mutation.{{ $f.MutationGet }}(); exists {
s.SetIgnore({{ $.Package }}.{{ $f.Constant }})
}
{{- end }}
}
}))
{{- end }}
@@ -429,4 +443,4 @@ func (u *{{ $upsertBulk }}) ExecX(ctx context.Context) {
}
{{ end }}
{{ end }}
{{ end }}
{{ end }}