sqlboiler/templates/17_auto_timestamps.tpl
Patrick O'brien 96d40fcfe4 Add automatic timestamps for created_at/updated_at
* Update and Insert
* Add --no-auto-timestamps flag
2016-08-29 00:12:37 +10:00

56 lines
1.5 KiB
Smarty

{{- define "timestamp_insert_helper" -}}
{{- if eq .NoAutoTimestamps false -}}
{{- $colNames := .Table.Columns | columnNames -}}
{{if containsAny $colNames "created_at" "updated_at"}}
loc := boil.GetLocation()
currTime := time.Time{}
if loc != nil {
currTime = time.Now().In(boil.GetLocation())
} else {
currTime = time.Now()
}
{{range $ind, $col := .Table.Columns}}
{{- if eq $col.Name "created_at" -}}
{{- if $col.Nullable}}
o.CreatedAt.Time = currTime
o.CreatedAt.Valid = true
{{- else}}
o.CreatedAt = currTime
{{- end -}}
{{- end -}}
{{- if eq $col.Name "updated_at" -}}
{{- if $col.Nullable}}
o.UpdatedAt.Time = currTime
o.UpdatedAt.Valid = true
{{- else}}
o.UpdatedAt = currTime
{{- end -}}
{{- end -}}
{{end}}
{{end}}
{{- end}}
{{- end -}}
{{- define "timestamp_update_helper" -}}
{{- if eq .NoAutoTimestamps false -}}
{{- $colNames := .Table.Columns | columnNames -}}
{{if containsAny $colNames "updated_at"}}
loc := boil.GetLocation()
currTime := time.Time{}
if loc != nil {
currTime = time.Now().In(boil.GetLocation())
} else {
currTime = time.Now()
}
{{range $ind, $col := .Table.Columns}}
{{- if eq $col.Name "updated_at" -}}
{{- if $col.Nullable}}
o.UpdatedAt.Time = currTime
o.UpdatedAt.Valid = true
{{- else}}
o.UpdatedAt = currTime
{{- end -}}
{{- end -}}
{{end}}
{{end}}
{{- end}}
{{end -}}