Skip to content

Add configuration for timestamp and soft delete columns#962

Merged
aarondl merged 2 commits intoaarondl:devfrom
stephenafamo:timestamps
Sep 26, 2021
Merged

Add configuration for timestamp and soft delete columns#962
aarondl merged 2 commits intoaarondl:devfrom
stephenafamo:timestamps

Conversation

@stephenafamo
Copy link
Collaborator

Use Case

I am inheriting a codebase which has the timestamp columns as createdAt and updatedAt.

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_at column for soft deletes.

@aarondl aarondl merged commit 497301d into aarondl:dev Sep 26, 2021
@aarondl
Copy link
Owner

aarondl commented Sep 26, 2021

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!

currTime := time.Now().In(boil.GetLocation())
o.DeletedAt = null.TimeFrom(currTime)
wl := []string{"deleted_at"}
wl := []string{(or $.AutoColumns.Deleted "deleted_at")}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephenafamo @aarondl

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it is not. Working on a fix now.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got it. Thank you for your quick reply!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants