@@ -31,17 +31,32 @@ import (
3131 {{- range $edge := $node .Edges }}
3232 {{/* if the edge has our custom annotation applied */ }}
3333 {{- if $annotation := $edge .Annotations.OPENLANE_CASCADE }}
34+ {{/* schemas that opt out of history tracking have no history table to purge */ }}
35+ {{- $trackedHistory := or ( not $edge .Type.Annotations.History ) (eq $edge .Type.Annotations.History.Exclude false) }}
3436 {{/* use the client to delete records where the edge schema has a field (provided by the annotation) containing the ID provided by the func */ }}
3537 {{/* special case for child fields */ }}
3638 {{- if eq $annotation .Field " Child" }}
3739 if exists, err := FromContext(ctx). {{ $edge .Type.Name }}.Query ().Where ({{ $edge .Type.Name | lower }}.HasParentWith ({{ $edge .Type.Name | lower }}.ID (id))).Exist (ctx); err == nil && exists {
40+ {{- if $trackedHistory }}
41+ {{/* the history rows are matched by a sub-select on the records being removed, so this has to run first */ }}
42+ if err := Purge{{ $edge .Type.Name }}History(ctx, {{ $edge .Type.Name | lower }}.HasParentWith ({{ $edge .Type.Name | lower }}.ID (id))); err != nil {
43+ return err
44+ }
45+ {{- end }}
46+
3847 if organizationCount, err := FromContext(ctx). {{ $edge .Type.Name }}.Delete ().Where ({{ $edge .Type.Name | lower }}.HasParentWith ({{ $edge .Type.Name | lower }}.ID (id))).Exec (ctx); err != nil {
3948 logx.FromContext (ctx).Error ().Err (err).Int (" count" , {{ $edge .Type.Name | lower }}Count).Msg (" error deleting child {{ $edge.Type.Name | lower }}" )
4049 return err
4150 }
4251 }
4352 {{- else if hasSuffix $annotation .Field " ID" }}
4453 if exists, err := FromContext(ctx). {{ $edge .Type.Name }}.Query ().Where (({{ $edge .Type.Name | lower }}. {{ $annotation .Field }}(id))).Exist (ctx); err == nil && exists {
54+ {{- if $trackedHistory }}
55+ if err := Purge{{ $edge .Type.Name }}History(ctx, {{ $edge .Type.Name | lower }}. {{ $annotation .Field }}(id)); err != nil {
56+ return err
57+ }
58+ {{- end }}
59+
4560 if {{ $edge .Type.Name | lower }}Count, err := FromContext(ctx). {{ $edge .Type.Name }}.Delete ().Where ({{ $edge .Type.Name | lower }}. {{ $annotation .Field }}(id)).Exec (ctx); err != nil {
4661 logx.FromContext (ctx).Error ().Err (err).Int (" count" , {{ $edge .Type.Name | lower }}Count).Msg (" error deleting {{ $edge.Type.Name | lower }}" )
4762 return err
@@ -63,6 +78,12 @@ import (
6378 }
6479 }
6580 if exists, err := FromContext(ctx). {{ $edge .Type.Name }}.Query ().Where (({{ $edge .Type.Name | lower }}.Has {{ $annotation .Field }}With({{ $node .Name | lower }}.ID (id)))).Exist (ctx); err == nil && exists {
81+ {{- if $trackedHistory }}
82+ if err := Purge{{ $edge .Type.Name }}History(ctx, {{ $edge .Type.Name | lower }}.Has {{ $annotation .Field }}With({{ $node .Name | lower }}.ID (id))); err != nil {
83+ return err
84+ }
85+ {{- end }}
86+
6687 if {{ $edge .Type.Name | lower }}Count, err := FromContext(ctx). {{ $edge .Type.Name }}.Delete ().Where ({{ $edge .Type.Name | lower }}.Has {{ $annotation .Field }}With({{ $node .Name | lower }}.ID (id))).Exec (ctx); err != nil {
6788 logx.FromContext (ctx).Error ().Err (err).Int (" count" , {{ $edge .Type.Name | lower }}Count).Msg (" error deleting {{ $edge.Type.Name | lower }}" )
6889 return err
@@ -74,8 +95,21 @@ import (
7495 {{- if $annotation := $node .Annotations.OPENLANE_CASCADE_THROUGH }}
7596 {{- range $schema := $annotation .Schemas }}
7697 {{- $field := $schema .Through }}
98+ {{/* the through schema is named by the annotation, so its node has to be looked up to know whether it tracks history */ }}
99+ {{- $throughHistory := false }}
100+ {{- range $through := $ .Nodes }}
101+ {{- if eq $through .Name $field }}
102+ {{- $throughHistory = or ( not $through .Annotations.History ) (eq $through .Annotations.History.Exclude false) }}
103+ {{- end }}
104+ {{- end }}
77105 {{/* use the client to delete records where the edge has a field and a through schema (provided by the annotation) containing the ID provided by the func */ }}
78106 if exists, err := FromContext(ctx). {{ $field }}.Query ().Where (({{ $field | lower }}.Has {{ $schema .Field }}With({{ $schema .Field | lower }}.ID (id)))).Exist (ctx); err == nil && exists {
107+ {{- if $throughHistory }}
108+ if err := Purge{{ $field }}History(ctx, {{ $field | lower }}.Has {{ $schema .Field }}With({{ $schema .Field | lower }}.ID (id))); err != nil {
109+ return err
110+ }
111+ {{- end }}
112+
79113 if {{ $field | lower }}Count, err := FromContext(ctx). {{ $field }}.Delete ().Where ({{ $field | lower }}.Has {{ $schema .Field }}With({{ $schema .Field | lower }}.ID (id))).Exec (ctx); err != nil {
80114 logx.FromContext (ctx).Error ().Err (err).Int (" count" , {{ $field | lower }}Count).Msg (" error deleting {{ $field | lower }}" )
81115 return err
0 commit comments