Skip to content

Commit b51c195

Browse files
author
gongmengnan
committed
Update the soft delete
1. Change the CLI flag to --add-soft-delete; 2. Change the flag in delete methods to hardDelete; 3. Add test suites for soft deletes when it's enabled; 4. Add tests for table.CanSoftDelete; 5. Change randomize.Struct to ignore the deleted_at column; 6. Fix the soft delete in slice.DeleteAll.
1 parent eaa9a5c commit b51c195

21 files changed

+335
-112
lines changed

boilingcore/boilingcore.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (s *State) Run() error {
141141
PkgName: s.Config.PkgName,
142142
AddGlobal: s.Config.AddGlobal,
143143
AddPanic: s.Config.AddPanic,
144-
SoftDeletes: s.Config.SoftDeletes,
144+
AddSoftDeletes: s.Config.AddSoftDeletes,
145145
NoContext: s.Config.NoContext,
146146
NoHooks: s.Config.NoHooks,
147147
NoAutoTimestamps: s.Config.NoAutoTimestamps,

boilingcore/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type Config struct {
2222
Debug bool `toml:"debug,omitempty" json:"debug,omitempty"`
2323
AddGlobal bool `toml:"add_global,omitempty" json:"add_global,omitempty"`
2424
AddPanic bool `toml:"add_panic,omitempty" json:"add_panic,omitempty"`
25-
SoftDeletes bool `toml:"soft_deletes,omitempty" json:"soft_deletes,omitempty"`
25+
AddSoftDeletes bool `toml:"add_soft_deletes,omitempty" json:"add_soft_deletes,omitempty"`
2626
NoContext bool `toml:"no_context,omitempty" json:"no_context,omitempty"`
2727
NoTests bool `toml:"no_tests,omitempty" json:"no_tests,omitempty"`
2828
NoHooks bool `toml:"no_hooks,omitempty" json:"no_hooks,omitempty"`

boilingcore/templates.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type templateData struct {
3939
// Control various generation features
4040
AddGlobal bool
4141
AddPanic bool
42-
SoftDeletes bool
42+
AddSoftDeletes bool
4343
NoContext bool
4444
NoHooks bool
4545
NoAutoTimestamps bool

drivers/table_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,33 @@ func TestCanLastInsertID(t *testing.T) {
118118
}
119119
}
120120
}
121+
122+
func TestCanSoftDelete(t *testing.T) {
123+
t.Parallel()
124+
125+
tests := []struct {
126+
Can bool
127+
Columns []Column
128+
}{
129+
{true, []Column{
130+
{Name: "deleted_at", Type: "null.Time"},
131+
}},
132+
{false, []Column{
133+
{Name: "deleted_at", Type: "time.Time"},
134+
}},
135+
{false, []Column{
136+
{Name: "deleted_at", Type: "int"},
137+
}},
138+
{false, nil},
139+
}
140+
141+
for i, test := range tests {
142+
table := Table{
143+
Columns: test.Columns,
144+
}
145+
146+
if got := table.CanSoftDelete(); got != test.Can {
147+
t.Errorf("%d) wrong: %t", i, got)
148+
}
149+
}
150+
}

main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func main() {
104104
rootCmd.PersistentFlags().BoolP("no-driver-templates", "", false, "Disable parsing of templates defined by the database driver")
105105
rootCmd.PersistentFlags().BoolP("add-global-variants", "", false, "Enable generation for global variants")
106106
rootCmd.PersistentFlags().BoolP("add-panic-variants", "", false, "Enable generation for panic variants")
107-
rootCmd.PersistentFlags().BoolP("soft-deletes", "", false, "Enable soft deletion by updating deleted_at timestamp")
107+
rootCmd.PersistentFlags().BoolP("add-soft-deletes", "", false, "Enable soft deletion by updating deleted_at timestamp")
108108
rootCmd.PersistentFlags().BoolP("version", "", false, "Print the version")
109109
rootCmd.PersistentFlags().BoolP("wipe", "", false, "Delete the output folder (rm -rf) before generation to ensure sanity")
110110
rootCmd.PersistentFlags().StringP("struct-tag-casing", "", "snake", "Decides the casing for go structure tag names. camel, title or snake (default snake)")
@@ -170,7 +170,7 @@ func preRun(cmd *cobra.Command, args []string) error {
170170
Debug: viper.GetBool("debug"),
171171
AddGlobal: viper.GetBool("add-global-variants"),
172172
AddPanic: viper.GetBool("add-panic-variants"),
173-
SoftDeletes: viper.GetBool("soft-deletes"),
173+
AddSoftDeletes: viper.GetBool("add-soft-deletes"),
174174
NoContext: viper.GetBool("no-context"),
175175
NoTests: viper.GetBool("no-tests"),
176176
NoHooks: viper.GetBool("no-hooks"),

randomize/randomize.go

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func Struct(s *Seed, str interface{}, colTypes map[string]string, canBeNull bool
6060
copyBlacklist := make([]string, len(blacklist))
6161
copy(copyBlacklist, blacklist)
6262
blacklist = copyBlacklist
63+
blacklist = append(blacklist, "deleted_at")
6364

6465
sort.Strings(blacklist)
6566

templatebin/bindata.go

+42-42
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/04_relationship_to_one.go.tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
func (o *{{$ltable.UpSingular}}) {{$rel.Foreign}}(mods ...qm.QueryMod) ({{$ftable.DownSingular}}Query) {
1010
queryMods := []qm.QueryMod{
1111
qm.Where("{{$fkey.ForeignColumn | $.Quotes}} = ?", o.{{$ltable.Column $fkey.Column}}),
12-
{{if and $.SoftDeletes $canSoftDelete -}}
12+
{{if and $.AddSoftDeletes $canSoftDelete -}}
1313
qmhelper.WhereIsNull("deleted_at"),
1414
{{- end}}
1515
}

templates/05_relationship_one_to_one.go.tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
func (o *{{$ltable.UpSingular}}) {{$relAlias.Local}}(mods ...qm.QueryMod) ({{$ftable.DownSingular}}Query) {
1010
queryMods := []qm.QueryMod{
1111
qm.Where("{{$rel.ForeignColumn | $.Quotes}} = ?", o.{{$ltable.Column $rel.Column}}),
12-
{{if and $.SoftDeletes $canSoftDelete -}}
12+
{{if and $.AddSoftDeletes $canSoftDelete -}}
1313
qmhelper.WhereIsNull("deleted_at"),
1414
{{- end}}
1515
}

templates/06_relationship_to_many.go.tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (o *{{$ltable.UpSingular}}) {{$relAlias.Local}}(mods ...qm.QueryMod) {{$fta
2323
{{else -}}
2424
queryMods = append(queryMods,
2525
qm.Where("{{$schemaForeignTable}}.{{$rel.ForeignColumn | $.Quotes}}=?", o.{{$ltable.Column $rel.Column}}),
26-
{{if and $.SoftDeletes $canSoftDelete -}}
26+
{{if and $.AddSoftDeletes $canSoftDelete -}}
2727
qmhelper.WhereIsNull("{{$schemaForeignTable}}.{{"deleted_at" | $.Quotes}}"),
2828
{{- end}}
2929
)

templates/07_relationship_to_one_eager.go.tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func ({{$ltable.DownSingular}}L) Load{{$rel.Foreign}}({{if $.NoContext}}e boil.E
6767
query := NewQuery(
6868
qm.From(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}`),
6969
qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, args...),
70-
{{if and $.SoftDeletes $canSoftDelete -}}
70+
{{if and $.AddSoftDeletes $canSoftDelete -}}
7171
qmhelper.WhereIsNull(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.deleted_at`),
7272
{{- end}}
7373
)

templates/08_relationship_one_to_one_eager.go.tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func ({{$ltable.DownSingular}}L) Load{{$relAlias.Local}}({{if $.NoContext}}e boi
5555
query := NewQuery(
5656
qm.From(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}`),
5757
qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, args...),
58-
{{if and $.SoftDeletes $canSoftDelete -}}
58+
{{if and $.AddSoftDeletes $canSoftDelete -}}
5959
qmhelper.WhereIsNull(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.deleted_at`),
6060
{{- end}}
6161
)

templates/09_relationship_to_many_eager.go.tpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ func ({{$ltable.DownSingular}}L) Load{{$relAlias.Local}}({{if $.NoContext}}e boi
6060
qm.From("{{$schemaForeignTable}}"),
6161
qm.InnerJoin("{{$schemaJoinTable}} as {{id 0 | $.Quotes}} on {{$schemaForeignTable}}.{{.ForeignColumn | $.Quotes}} = {{id 0 | $.Quotes}}.{{.JoinForeignColumn | $.Quotes}}"),
6262
qm.WhereIn("{{id 0 | $.Quotes}}.{{.JoinLocalColumn | $.Quotes}} in ?", args...),
63-
{{if and $.SoftDeletes $canSoftDelete -}}
63+
{{if and $.AddSoftDeletes $canSoftDelete -}}
6464
qmhelper.WhereIsNull("{{$schemaForeignTable}}.{{"deleted_at" | $.Quotes}}"),
6565
{{- end}}
6666
)
6767
{{else -}}
6868
query := NewQuery(
6969
qm.From(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}`),
7070
qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, args...),
71-
{{if and $.SoftDeletes $canSoftDelete -}}
71+
{{if and $.AddSoftDeletes $canSoftDelete -}}
7272
qmhelper.WhereIsNull(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.deleted_at`),
7373
{{- end}}
7474
)

templates/13_all.go.tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{{- $canSoftDelete := .Table.CanSoftDelete }}
44
// {{$alias.UpPlural}} retrieves all the records using an executor.
55
func {{$alias.UpPlural}}(mods ...qm.QueryMod) {{$alias.DownSingular}}Query {
6-
{{if and .SoftDeletes $canSoftDelete -}}
6+
{{if and .AddSoftDeletes $canSoftDelete -}}
77
mods = append(mods, qm.From("{{$schemaTable}}"), qmhelper.WhereIsNull("{{$schemaTable}}.{{"deleted_at" | $.Quotes}}"))
88
{{else -}}
99
mods = append(mods, qm.From("{{$schemaTable}}"))

templates/14_find.go.tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func Find{{$alias.UpSingular}}({{if .NoContext}}exec boil.Executor{{else}}ctx co
4747
sel = strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, selectCols), ",")
4848
}
4949
query := fmt.Sprintf(
50-
"select %s from {{.Table.Name | .SchemaTable}} where {{if .Dialect.UseIndexPlaceholders}}{{whereClause .LQ .RQ 1 .Table.PKey.Columns}}{{else}}{{whereClause .LQ .RQ 0 .Table.PKey.Columns}}{{end}}{{if and .SoftDeletes $canSoftDelete}} and {{"deleted_at" | $.Quotes}} is null{{end}}", sel,
50+
"select %s from {{.Table.Name | .SchemaTable}} where {{if .Dialect.UseIndexPlaceholders}}{{whereClause .LQ .RQ 1 .Table.PKey.Columns}}{{else}}{{whereClause .LQ .RQ 0 .Table.PKey.Columns}}{{end}}{{if and .AddSoftDeletes $canSoftDelete}} and {{"deleted_at" | $.Quotes}} is null{{end}}", sel,
5151
)
5252

5353
q := queries.Raw(query, {{$pkNames | join ", "}})

0 commit comments

Comments
 (0)