-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpublish_pg_tpl.go
73 lines (72 loc) · 2.05 KB
/
publish_pg_tpl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package gontentful
const pgPublishTemplate = `
{{ range $itemidx, $item := .Rows }}
INSERT INTO {{ $.SchemaName }}.{{ $.TableName }} (
_id,
_sys_id,
{{- range $k, $v := .FieldColumns }}
{{ $v }},
{{- end }}
_locale,
_status,
_version,
_created_at,
_created_by,
_updated_at,
_updated_by,
_published_at,
_published_by
) VALUES (
'{{ .SysID }}_{{ .Locale }}',
'{{ .SysID }}',
{{- range $k, $v := .FieldColumns }}
{{ $item.GetFieldValue $v }},
{{- end }}
'{{ .Locale }}',
'{{ .Status }}',
{{ .Version }},
to_timestamp('{{ .CreatedAt }}','YYYY-MM-DDThh24:mi:ssZ'),
'{{ if not .CreatedBy }}sync{{ else }}{{ .CreatedBy }}{{ end }}',
to_timestamp('{{ .UpdatedAt }}','YYYY-MM-DDThh24:mi:ssZ'),
'{{ if not .UpdatedBy }}sync{{ else }}{{ .UpdatedBy }}{{ end }}',
{{ if .PublishedAt }}to_timestamp('{{ .PublishedAt }}','YYYY-MM-DDThh24:mi:ssZ'){{ else }}NULL{{ end }},
{{ if and .PublishedAt .PublishedBy }}'{{ .PublishedBy }}'{{ else }}NULL{{ end }}
)
ON CONFLICT (_id) DO UPDATE
SET
{{- range $k, $v := .FieldColumns }}
{{ $v }} = EXCLUDED.{{ $v }},
{{- end }}
_locale = EXCLUDED._locale,
_status = EXCLUDED._status,
_version = EXCLUDED._version,
_updated_at = EXCLUDED._updated_at,
_updated_by = EXCLUDED._updated_by,
_published_at = EXCLUDED._published_at,
_published_by = EXCLUDED._published_by
;
{{- end -}}
{{ range $tblidx, $tbl := .DeletedConTables }}
{{ range $rowidx, $row := $tbl.Rows }}
DELETE FROM {{ $.SchemaName }}.{{ $tbl.TableName }} WHERE {{ index $tbl.Columns 0 }} = {{ (index $row 0) }};
{{- end -}}
{{- end -}}
{{ range $tblidx, $tbl := .ConTables }}
{{ $prevId := "" }}
{{ range $rowidx, $row := $tbl.Rows }}
{{if ne $prevId (index $row 0) -}}
DELETE FROM {{ $.SchemaName }}.{{ $tbl.TableName }} WHERE {{ index $tbl.Columns 0 }} = {{ (index $row 0) }};
{{ end -}}
{{ $prevId = (index $row 0) -}}
INSERT INTO {{ $.SchemaName }}.{{ $tbl.TableName }} (
{{- range $k, $v := $tbl.Columns }}
{{- if $k -}},{{- end -}}{{ $v }}
{{- end }}
) VALUES (
{{- range $k, $v := $row }}
{{- if $k -}},{{- end -}}{{ $v }}
{{- end -}}
);
{{- end -}}
{{- end -}}
`