Add configuration for timestamp and soft delete columns#962
Merged
aarondl merged 2 commits intoaarondl:devfrom Sep 26, 2021
Merged
Add configuration for timestamp and soft delete columns#962aarondl merged 2 commits intoaarondl:devfrom
aarondl merged 2 commits intoaarondl:devfrom
Conversation
Owner
|
I didn't really want to accept this because sqlboiler has many issues when you don't use it's naming scheme but presumably you use this patch to completely enable a use case so accepted. Thanks for the PR! |
ryosukee
reviewed
Oct 1, 2021
| currTime := time.Now().In(boil.GetLocation()) | ||
| o.DeletedAt = null.TimeFrom(currTime) | ||
| wl := []string{"deleted_at"} | ||
| wl := []string{(or $.AutoColumns.Deleted "deleted_at")} |
There was a problem hiding this comment.
I'm sorry if i'm wrong.
I think {{ and }} should be used for Actions of template like below example. Is the formatting of this line correct?
my sample code
$ go version
go version go1.16.4 darwin/amd64
$ go run hoge.go
input
wl := []string{(or $.AutoColumns.Deleted "deleted_at")}
output
wl := []string{(or $.AutoColumns.Deleted "deleted_at")}
output with empty
wl := []string{(or $.AutoColumns.Deleted "deleted_at")}
input
wl := []string{"{{ or $.AutoColumns.Deleted "deleted_at" }}"}
output
wl := []string{"deletedAt"}
output with empty
wl := []string{"deleted_at"}package main
import (
"bytes"
"fmt"
"html/template"
)
type AutoColumns struct {
Deleted string
}
type Conf struct {
AutoColumns AutoColumns
}
func main() {
original := `wl := []string{(or $.AutoColumns.Deleted "deleted_at")}`
format(original)
changed := `wl := []string{"{{ or $.AutoColumns.Deleted "deleted_at" }}"}`
format(changed)
}
func format(templ string) {
fmt.Println("input")
fmt.Println(templ)
tpl, err := template.New("").Parse(templ)
if err != nil {
fmt.Println("new err")
fmt.Println(err)
}
autoCol := AutoColumns{Deleted: "deletedAt"}
autoColEmpty := AutoColumns{}
conf := Conf{AutoColumns: autoCol}
confEmpty := Conf{AutoColumns: autoColEmpty}
buf := &bytes.Buffer{}
if err := tpl.Execute(buf, conf); err != nil {
fmt.Println("exec err")
fmt.Println(err)
}
fmt.Println("output")
fmt.Println(string(buf.Bytes()))
buf = &bytes.Buffer{}
if err := tpl.Execute(buf, confEmpty); err != nil {
fmt.Println("exec err")
fmt.Println(err)
}
fmt.Println("output with empty")
fmt.Println(string(buf.Bytes()) + "\n")
}Actually, I'm just failing sqlboiler command at this point as showing below.
The version is SQLBoiler v4.7.1.
Error: unable to generate output: failed to format template
1037 args = queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), brandPrimaryKeyMapping)
1038 sql = "DELETE FROM `brands` WHERE `id`=?"
1039 } else {
1040 currTime := time.Now().In(boil.GetLocation())
1041 o.DeletedAt = null.TimeFrom(currTime)
>>>> wl := []string{(or $.AutoColumns.Deleted "deleted_at")}
1043 sql = fmt.Sprintf("UPDATE `brands` SET %s WHERE `id`=?",
1044 strmangle.SetParamNames("`", "`", 0, wl),
1045 )
1046 valueMapping, err := queries.BindMapping(brandType, brandMapping, append(wl, brandPrimaryKeyColumns...))
1047 if err != nil {
: 1042:22: illegal character U+0024 '$' (and 10 more errors)
ERROR: 1
make: *** [sqlboiler] Error 1
Is this template work correctly?
Collaborator
Author
There was a problem hiding this comment.
No it is not. Working on a fix now.
There was a problem hiding this comment.
I got it. Thank you for your quick reply!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Use Case
I am inheriting a codebase which has the timestamp columns as
createdAtandupdatedAt.I thought it would make sense to allow the timestamp columns to be configurable, while making this change, I thought it made sense to extend it to the
deleted_atcolumn for soft deletes.