-
Notifications
You must be signed in to change notification settings - Fork 2
/
sync_sql.go
84 lines (79 loc) · 2.3 KB
/
sync_sql.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
74
75
76
77
78
79
80
81
82
83
84
package bloomdb
var syncSql = `
--- 1) Move Main Table - Temp Table into version table
INSERT INTO {{.Table}}_revisions (
{{range $i, $e := .Columns}}{{$e}},{{end}}
bloom_created_at,
bloom_updated_at,
bloom_action
)
(SELECT
{{range $i, $e := .Columns}}{{$.Table}}.{{$e}},{{end}}
'{{.CreatedAt}}' AS bloom_created_at,
'{{.UpdatedAt}}' AS bloom_updated_at,
'DELETE' AS bloom_action
FROM {{.Table}}
WHERE EXISTS (
SELECT 1 FROM (
SELECT id FROM {{.Table}}
EXCEPT
SELECT id from {{.Table}}_temp) AS f
WHERE f.id = {{.Table}}.id));
--- 2) Delete Main Table - Temp Table from Main Table
---DELETE FROM {{.Table}}
---WHERE EXISTS (
--- SELECT 1 FROM (
--- SELECT id FROM {{.Table}}
--- EXCEPT
--- SELECT id from {{.Table}}_temp) AS f
--- WHERE f.id = {{.Table}}.id);
--- 3) Move Main Table INTERSECT Temp Table into version table
INSERT INTO {{.Table}}_revisions (
{{range $i, $e := .Columns}}{{$e}},{{end}}
bloom_created_at,
bloom_updated_at,
bloom_action
)
(SELECT
{{range $i, $e := .Columns}}{{$.Table}}.{{$e}},{{end}}
{{.Table}}.bloom_created_at,
'{{.UpdatedAt}}' AS bloom_updated_at,
'UPDATE' AS action
FROM {{.Table}}
WHERE EXISTS (
SELECT 1 FROM (
SELECT
id, revision
FROM {{.Table}}
EXCEPT
SELECT id, revision from {{.Table}}_revisions) AS f
WHERE f.id = {{.Table}}.id));
--- 4) Update Main Table from Temp Table
---UPDATE {{.Table}}
---SET
---{{range $i, $e := .Columns}}{{$e}} = {{$.Table}}_temp.{{$e}}{{if len $.Columns | sub 1 | eq $i | not}},{{end}}
---{{end}}
---FROM {{.Table}}_temp
---WHERE {{.Table}}_temp.id = {{.Table}}.id
---AND EXISTS (
--- SELECT 1 FROM (
--- SELECT id, revision FROM {{.Table}}
--- EXCEPT
--- SELECT id, revision FROM {{.Table}}_temp) AS f
--- WHERE f.id = {{.Table}}.id);
--- 5) Insert New records into Main Table
---INSERT INTO {{.Table}} (
---{{range $i, $e := .Columns}}{{$e}},{{end}}
---bloom_created_at
---)
---SELECT DISTINCT ON ({{.Table}}_temp.id)
---{{range $i, $e := .Columns}}{{$.Table}}_temp.{{$e}},{{end}}
---'{{.CreatedAt}}' AS bloom_created_at
---FROM {{.Table}}_temp
---WHERE EXISTS (
--- SELECT 1 FROM (
--- SELECT id FROM {{.Table}}_temp
--- EXCEPT
--- SELECT id FROM {{.Table}}) AS f
--- WHERE f.id = {{.Table}}_temp.id);
`