Skip to content

Commit

Permalink
Merge 'namco1992/soft-delete' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
aarondl committed Apr 27, 2020
2 parents e7037d5 + 7108e34 commit b6d3c06
Show file tree
Hide file tree
Showing 22 changed files with 403 additions and 80 deletions.
1 change: 1 addition & 0 deletions boilingcore/boilingcore.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func (s *State) Run() error {
PkgName: s.Config.PkgName,
AddGlobal: s.Config.AddGlobal,
AddPanic: s.Config.AddPanic,
AddSoftDeletes: s.Config.AddSoftDeletes,
NoContext: s.Config.NoContext,
NoHooks: s.Config.NoHooks,
NoAutoTimestamps: s.Config.NoAutoTimestamps,
Expand Down
1 change: 1 addition & 0 deletions boilingcore/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Config struct {
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"`
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"`
Expand Down
1 change: 1 addition & 0 deletions boilingcore/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type templateData struct {
// Control various generation features
AddGlobal bool
AddPanic bool
AddSoftDeletes bool
NoContext bool
NoHooks bool
NoAutoTimestamps bool
Expand Down
9 changes: 9 additions & 0 deletions drivers/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,12 @@ func (t Table) CanLastInsertID() bool {

return true
}

func (t Table) CanSoftDelete() bool {
for _, column := range t.Columns {
if column.Name == "deleted_at" && column.Type == "null.Time" {
return true
}
}
return false
}
30 changes: 30 additions & 0 deletions drivers/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,33 @@ func TestCanLastInsertID(t *testing.T) {
}
}
}

func TestCanSoftDelete(t *testing.T) {
t.Parallel()

tests := []struct {
Can bool
Columns []Column
}{
{true, []Column{
{Name: "deleted_at", Type: "null.Time"},
}},
{false, []Column{
{Name: "deleted_at", Type: "time.Time"},
}},
{false, []Column{
{Name: "deleted_at", Type: "int"},
}},
{false, nil},
}

for i, test := range tests {
table := Table{
Columns: test.Columns,
}

if got := table.CanSoftDelete(); got != test.Can {
t.Errorf("%d) wrong: %t", i, got)
}
}
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func main() {
rootCmd.PersistentFlags().BoolP("no-back-referencing", "", false, "Disable back referencing in the loaded relationship structs")
rootCmd.PersistentFlags().BoolP("add-global-variants", "", false, "Enable generation for global variants")
rootCmd.PersistentFlags().BoolP("add-panic-variants", "", false, "Enable generation for panic variants")
rootCmd.PersistentFlags().BoolP("add-soft-deletes", "", false, "Enable soft deletion by updating deleted_at timestamp")
rootCmd.PersistentFlags().BoolP("version", "", false, "Print the version")
rootCmd.PersistentFlags().BoolP("wipe", "", false, "Delete the output folder (rm -rf) before generation to ensure sanity")
rootCmd.PersistentFlags().StringP("struct-tag-casing", "", "snake", "Decides the casing for go structure tag names. camel, title or snake (default snake)")
Expand Down Expand Up @@ -170,6 +171,7 @@ func preRun(cmd *cobra.Command, args []string) error {
Debug: viper.GetBool("debug"),
AddGlobal: viper.GetBool("add-global-variants"),
AddPanic: viper.GetBool("add-panic-variants"),
AddSoftDeletes: viper.GetBool("add-soft-deletes"),
NoContext: viper.GetBool("no-context"),
NoTests: viper.GetBool("no-tests"),
NoHooks: viper.GetBool("no-hooks"),
Expand Down
1 change: 1 addition & 0 deletions randomize/randomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func Struct(s *Seed, str interface{}, colTypes map[string]string, canBeNull bool
copyBlacklist := make([]string, len(blacklist))
copy(copyBlacklist, blacklist)
blacklist = copyBlacklist
blacklist = append(blacklist, "deleted_at")

sort.Strings(blacklist)

Expand Down
84 changes: 42 additions & 42 deletions templatebin/bindata.go

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion templates/04_relationship_to_one.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
{{- range $fkey := .Table.FKeys -}}
{{- $ltable := $.Aliases.Table $fkey.Table -}}
{{- $ftable := $.Aliases.Table $fkey.ForeignTable -}}
{{- $rel := $ltable.Relationship $fkey.Name }}
{{- $rel := $ltable.Relationship $fkey.Name -}}
{{- $canSoftDelete := (getTable $.Tables $fkey.ForeignTable).CanSoftDelete }}
// {{$rel.Foreign}} pointed to by the foreign key.
func (o *{{$ltable.UpSingular}}) {{$rel.Foreign}}(mods ...qm.QueryMod) ({{$ftable.DownSingular}}Query) {
queryMods := []qm.QueryMod{
qm.Where("{{$fkey.ForeignColumn | $.Quotes}} = ?", o.{{$ltable.Column $fkey.Column}}),
{{if and $.AddSoftDeletes $canSoftDelete -}}
qmhelper.WhereIsNull("deleted_at"),
{{- end}}
}

queryMods = append(queryMods, mods...)
Expand Down
6 changes: 5 additions & 1 deletion templates/05_relationship_one_to_one.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
{{- range $rel := .Table.ToOneRelationships -}}
{{- $ltable := $.Aliases.Table $rel.Table -}}
{{- $ftable := $.Aliases.Table $rel.ForeignTable -}}
{{- $relAlias := $ftable.Relationship $rel.Name }}
{{- $relAlias := $ftable.Relationship $rel.Name -}}
{{- $canSoftDelete := (getTable $.Tables $rel.ForeignTable).CanSoftDelete }}
// {{$relAlias.Local}} pointed to by the foreign key.
func (o *{{$ltable.UpSingular}}) {{$relAlias.Local}}(mods ...qm.QueryMod) ({{$ftable.DownSingular}}Query) {
queryMods := []qm.QueryMod{
qm.Where("{{$rel.ForeignColumn | $.Quotes}} = ?", o.{{$ltable.Column $rel.Column}}),
{{if and $.AddSoftDeletes $canSoftDelete -}}
qmhelper.WhereIsNull("deleted_at"),
{{- end}}
}

queryMods = append(queryMods, mods...)
Expand Down
6 changes: 5 additions & 1 deletion templates/06_relationship_to_many.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
{{- $ltable := $.Aliases.Table $rel.Table -}}
{{- $ftable := $.Aliases.Table $rel.ForeignTable -}}
{{- $relAlias := $.Aliases.ManyRelationship $rel.ForeignTable $rel.Name $rel.JoinTable $rel.JoinLocalFKeyName -}}
{{- $schemaForeignTable := .ForeignTable | $.SchemaTable }}
{{- $schemaForeignTable := .ForeignTable | $.SchemaTable -}}
{{- $canSoftDelete := (getTable $.Tables .ForeignTable).CanSoftDelete }}
// {{$relAlias.Local}} retrieves all the {{.ForeignTable | singular}}'s {{$ftable.UpPlural}} with an executor
{{- if not (eq $relAlias.Local $ftable.UpPlural)}} via {{$rel.ForeignColumn}} column{{- end}}.
func (o *{{$ltable.UpSingular}}) {{$relAlias.Local}}(mods ...qm.QueryMod) {{$ftable.DownSingular}}Query {
Expand All @@ -22,6 +23,9 @@ func (o *{{$ltable.UpSingular}}) {{$relAlias.Local}}(mods ...qm.QueryMod) {{$fta
{{else -}}
queryMods = append(queryMods,
qm.Where("{{$schemaForeignTable}}.{{$rel.ForeignColumn | $.Quotes}}=?", o.{{$ltable.Column $rel.Column}}),
{{if and $.AddSoftDeletes $canSoftDelete -}}
qmhelper.WhereIsNull("{{$schemaForeignTable}}.{{"deleted_at" | $.Quotes}}"),
{{- end}}
)
{{end}}

Expand Down
11 changes: 9 additions & 2 deletions templates/07_relationship_to_one_eager.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
{{- $arg := printf "maybe%s" $ltable.UpSingular -}}
{{- $col := $ltable.Column $fkey.Column -}}
{{- $fcol := $ftable.Column $fkey.ForeignColumn -}}
{{- $usesPrimitives := usesPrimitives $.Tables $fkey.Table $fkey.Column $fkey.ForeignTable $fkey.ForeignColumn }}
{{- $usesPrimitives := usesPrimitives $.Tables $fkey.Table $fkey.Column $fkey.ForeignTable $fkey.ForeignColumn -}}
{{- $canSoftDelete := (getTable $.Tables $fkey.ForeignTable).CanSoftDelete }}
// Load{{$rel.Foreign}} allows an eager lookup of values, cached into the
// loaded structs of the objects. This is for an N-1 relationship.
func ({{$ltable.DownSingular}}L) Load{{$rel.Foreign}}({{if $.NoContext}}e boil.Executor{{else}}ctx context.Context, e boil.ContextExecutor{{end}}, singular bool, {{$arg}} interface{}, mods queries.Applicator) error {
Expand Down Expand Up @@ -63,7 +64,13 @@ func ({{$ltable.DownSingular}}L) Load{{$rel.Foreign}}({{if $.NoContext}}e boil.E
return nil
}

query := NewQuery(qm.From(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}`), qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, args...))
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}}.deleted_at`),
{{- end}}
)
if mods != nil {
mods.Apply(query)
}
Expand Down
11 changes: 9 additions & 2 deletions templates/08_relationship_one_to_one_eager.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
{{- $col := $ltable.Column $rel.Column -}}
{{- $fcol := $ftable.Column $rel.ForeignColumn -}}
{{- $usesPrimitives := usesPrimitives $.Tables $rel.Table $rel.Column $rel.ForeignTable $rel.ForeignColumn -}}
{{- $arg := printf "maybe%s" $ltable.UpSingular }}
{{- $arg := printf "maybe%s" $ltable.UpSingular -}}
{{- $canSoftDelete := (getTable $.Tables $rel.ForeignTable).CanSoftDelete }}
// Load{{$relAlias.Local}} allows an eager lookup of values, cached into the
// loaded structs of the objects. This is for a 1-1 relationship.
func ({{$ltable.DownSingular}}L) Load{{$relAlias.Local}}({{if $.NoContext}}e boil.Executor{{else}}ctx context.Context, e boil.ContextExecutor{{end}}, singular bool, {{$arg}} interface{}, mods queries.Applicator) error {
Expand Down Expand Up @@ -51,7 +52,13 @@ func ({{$ltable.DownSingular}}L) Load{{$relAlias.Local}}({{if $.NoContext}}e boi
return nil
}

query := NewQuery(qm.From(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}`), qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, args...))
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}}.deleted_at`),
{{- end}}
)
if mods != nil {
mods.Apply(query)
}
Expand Down
14 changes: 12 additions & 2 deletions templates/09_relationship_to_many_eager.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
{{- $fcol := $ftable.Column $rel.ForeignColumn -}}
{{- $usesPrimitives := usesPrimitives $.Tables $rel.Table $rel.Column $rel.ForeignTable $rel.ForeignColumn -}}
{{- $arg := printf "maybe%s" $ltable.UpSingular -}}
{{- $schemaForeignTable := $rel.ForeignTable | $.SchemaTable}}
{{- $schemaForeignTable := $rel.ForeignTable | $.SchemaTable -}}
{{- $canSoftDelete := (getTable $.Tables $rel.ForeignTable).CanSoftDelete }}
// Load{{$relAlias.Local}} allows an eager lookup of values, cached into the
// loaded structs of the objects. This is for a 1-M or N-M relationship.
func ({{$ltable.DownSingular}}L) Load{{$relAlias.Local}}({{if $.NoContext}}e boil.Executor{{else}}ctx context.Context, e boil.ContextExecutor{{end}}, singular bool, {{$arg}} interface{}, mods queries.Applicator) error {
Expand Down Expand Up @@ -59,9 +60,18 @@ func ({{$ltable.DownSingular}}L) Load{{$relAlias.Local}}({{if $.NoContext}}e boi
qm.From("{{$schemaForeignTable}}"),
qm.InnerJoin("{{$schemaJoinTable}} as {{id 0 | $.Quotes}} on {{$schemaForeignTable}}.{{.ForeignColumn | $.Quotes}} = {{id 0 | $.Quotes}}.{{.JoinForeignColumn | $.Quotes}}"),
qm.WhereIn("{{id 0 | $.Quotes}}.{{.JoinLocalColumn | $.Quotes}} in ?", args...),
{{if and $.AddSoftDeletes $canSoftDelete -}}
qmhelper.WhereIsNull("{{$schemaForeignTable}}.{{"deleted_at" | $.Quotes}}"),
{{- end}}
)
{{else -}}
query := NewQuery(qm.From(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}`), qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, args...))
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}}.deleted_at`),
{{- end}}
)
{{end -}}
if mods != nil {
mods.Apply(query)
Expand Down
8 changes: 7 additions & 1 deletion templates/13_all.go.tpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{{- $alias := .Aliases.Table .Table.Name}}
{{- $schemaTable := .Table.Name | .SchemaTable}}
{{- $canSoftDelete := .Table.CanSoftDelete }}
// {{$alias.UpPlural}} retrieves all the records using an executor.
func {{$alias.UpPlural}}(mods ...qm.QueryMod) {{$alias.DownSingular}}Query {
mods = append(mods, qm.From("{{.Table.Name | .SchemaTable}}"))
{{if and .AddSoftDeletes $canSoftDelete -}}
mods = append(mods, qm.From("{{$schemaTable}}"), qmhelper.WhereIsNull("{{$schemaTable}}.{{"deleted_at" | $.Quotes}}"))
{{else -}}
mods = append(mods, qm.From("{{$schemaTable}}"))
{{end -}}
return {{$alias.DownSingular}}Query{NewQuery(mods...)}
}
5 changes: 3 additions & 2 deletions templates/14_find.go.tpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{{- $alias := .Aliases.Table .Table.Name -}}
{{- $colDefs := sqlColDefinitions .Table.Columns .Table.PKey.Columns -}}
{{- $pkNames := $colDefs.Names | stringMap (aliasCols $alias) | stringMap .StringFuncs.camelCase | stringMap .StringFuncs.replaceReserved -}}
{{- $pkArgs := joinSlices " " $pkNames $colDefs.Types | join ", "}}
{{- $pkArgs := joinSlices " " $pkNames $colDefs.Types | join ", " -}}
{{- $canSoftDelete := .Table.CanSoftDelete }}
{{if .AddGlobal -}}
// Find{{$alias.UpSingular}}G retrieves a single record by ID.
func Find{{$alias.UpSingular}}G({{if not .NoContext}}ctx context.Context, {{end -}} {{$pkArgs}}, selectCols ...string) (*{{$alias.UpSingular}}, error) {
Expand Down Expand Up @@ -46,7 +47,7 @@ func Find{{$alias.UpSingular}}({{if .NoContext}}exec boil.Executor{{else}}ctx co
sel = strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, selectCols), ",")
}
query := fmt.Sprintf(
"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}}", sel,
"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,
)

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

0 comments on commit b6d3c06

Please sign in to comment.