forked from domonda/go-docdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgres.go
251 lines (220 loc) · 7.04 KB
/
postgres.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package docdb
import (
"context"
"github.com/domonda/go-errs"
"github.com/domonda/go-sqldb/db"
"github.com/domonda/go-types/nullable"
"github.com/domonda/go-types/uu"
)
func PostgresGetDocumentVersionIDOrNull(ctx context.Context, documentID uu.ID, version VersionTime) (id uu.NullableID, err error) {
defer errs.WrapWithFuncParams(&err, ctx, documentID, version)
err = db.QueryRow(ctx,
`select id
from docdb.document_version
where document_id = $1 and version = date_trunc('milliseconds', $2::timestamp)`,
documentID,
version.Time,
).Scan(&id)
if err != nil {
return uu.IDNull, db.ReplaceErrNoRows(err, nil)
}
return id, nil
}
func PostgresGetDocumentVersionIDs(ctx context.Context, documentID uu.ID) (ids uu.IDSlice, err error) {
defer errs.WrapWithFuncParams(&err, ctx, documentID)
return db.QueryValue[uu.IDSlice](ctx,
`select array_agg(id order by version)
from docdb.document_version
where document_id = $1`, documentID)
}
func ToRefactorVersionExists(ctx context.Context, documentID uu.ID, version VersionTime) (exists bool, err error) {
defer errs.WrapWithFuncParams(&err, ctx, documentID, version)
err = db.QueryRow(ctx,
`select exists (
select from docdb.document_version
where document_id = $1 and version = date_trunc('milliseconds', $2::timestamp)
)`,
documentID,
version.Time,
).Scan(&exists)
if err != nil {
return false, err
}
return exists, nil
}
// func VersionIDExists(ctx context.Context, documentVersionID uu.ID) (exists bool, err error) {
// defer errs.WrapWithFuncParams(&err, ctx, documentVersionID)
// err = db.QueryRow(ctx,
// `select exists(select from docdb.document_version where id = $1)`,
// documentVersionID,
// ).Scan(&exists)
// if err != nil {
// return false, err
// }
// return exists, nil
// }
func PostgresGetLatestVersion(ctx context.Context, documentID uu.ID) (version VersionTime, err error) {
defer errs.WrapWithFuncParams(&err, ctx, documentID)
err = db.QueryRow(ctx,
`select version from public.document where id = $1`,
documentID,
).Scan(&version)
if err != nil {
return VersionTime{}, err
}
return version, nil
}
func PostgresGetLatestDocumentVersionTimeAndID(ctx context.Context, documentID uu.ID) (version VersionTime, id uu.ID, err error) {
defer errs.WrapWithFuncParams(&err, ctx, documentID)
err = db.QueryRow(ctx,
`select doc.version, ver.id
from public.document as doc
inner join docdb.document_version as ver
on ver.document_id = doc.id and ver.version = doc.version
where doc.id = $1`,
documentID,
).Scan(&version, &id)
if err != nil {
return VersionTime{}, uu.IDNil, err
}
return version, id, nil
}
// // GetLatestVersionsWithIDs returns the latest version timestamps and IDs of the passed documentIDs
// func GetLatestVersionsWithIDs(ctx context.Context, documentIDs uu.IDSlice) (versions []VersionTime, ids uu.IDSlice, err error) {
// defer errs.WrapWithFuncParams(&err, ctx, documentIDs)
// versions = make([]VersionTime, 0, len(documentIDs))
// ids = make(uu.IDSlice, 0, len(documentIDs))
// err = db.QueryRows(ctx,
// `select doc.version, ver.id
// from unnest($1::uuid[]) as document_id
// inner join public.document as doc
// on doc.id = document_id::uuid
// inner join docdb.document_version as ver
// on ver.document_id = doc.id and ver.version = doc.version;`,
// documentIDs,
// ).ForEachRow(func(row sqldb.RowScanner) error {
// var (
// version VersionTime
// id uu.ID
// )
// err := row.Scan(&version, &id)
// versions = append(versions, version)
// ids = append(ids, id)
// return err
// })
// if err != nil {
// return nil, nil, err
// }
// return versions, ids, nil
// }
// PostgresInsertDocumentVersionIfMissing inserts the document information if there is no existing one
func PostgresInsertDocumentVersionIfMissing(ctx context.Context, versionInfo *VersionInfo) (versionID uu.ID, didExist bool, err error) {
defer errs.WrapWithFuncParams(&err, ctx, versionInfo)
err = db.Transaction(ctx, func(ctx context.Context) error {
err = db.QueryRow(ctx,
`select id
from docdb.document_version
where
document_id = $1
and
date_trunc('milliseconds', version) = $2`,
versionInfo.DocID,
versionInfo.Version,
).Scan(&versionID)
switch {
case errs.ReplaceErrNotFound(err, nil) != nil:
return err
case err == nil:
didExist = true
return nil
}
versionID = uu.NewID(ctx)
return PostgresInsertDocumentVersionWithFiles(ctx, versionID, versionInfo)
})
if err != nil {
return uu.IDNil, false, err
}
return versionID, didExist, nil
}
func PostgresInsertDocumentVersionWithFiles(ctx context.Context, versionID uu.ID, versionInfo *VersionInfo) (err error) {
defer errs.WrapWithFuncParams(&err, ctx, versionID, versionInfo)
return db.Transaction(ctx, func(ctx context.Context) error {
tx := db.Conn(ctx)
err = tx.Exec(
`insert into
docdb.document_version (
id,
document_id,
version,
prev_version,
commit_user_id,
commit_reason,
added_files,
removed_files,
modified_files
)
values ($1, $2, $3, $4, $5, $6, $7, $8, $9)`,
versionID, // id
versionInfo.DocID, // document_id
versionInfo.Version, // version
versionInfo.PrevVersion, // prev_version
versionInfo.CommitUserID, // commit_user_id
versionInfo.CommitReason, // commit_reason
nullable.StringArray(versionInfo.AddedFiles), // added_files
nullable.StringArray(versionInfo.RemovedFiles), // removed_files
nullable.StringArray(versionInfo.ModifiedFiles), // modified_files
)
if err != nil {
return err
}
for _, fileInfo := range versionInfo.Files {
err = tx.Exec(
`insert into
docdb.document_version_file (
document_version_id,
name,
size,
hash
)
values ($1, $2, $3, $4)`,
versionID, // document_version_id
fileInfo.Name, // name
fileInfo.Size, // size
fileInfo.Hash, // hash
)
if err != nil {
return err
}
}
return nil
})
}
func PostgresDeleteDocumentVersionByID(ctx context.Context, versionID uu.ID) (err error) {
defer errs.WrapWithFuncParams(&err, ctx, versionID)
err = db.Exec(ctx, `delete from docdb.document_version where id = $1`, versionID)
if err != nil {
return err
}
log.InfoCtx(ctx, "Deleted document version").
UUID("versionID", versionID).
Log()
return nil
}
// func DeleteVersionByTimestamp(ctx context.Context, documentID uu.ID, version VersionTime) (versionID uu.ID, err error) {
// defer errs.WrapWithFuncParams(&err, ctx, documentID, version)
// err = db.QueryRow(ctx,
// `delete from docdb.document_version
// where document_id = $1 and version = date_trunc('milliseconds', $2::timestamp)
// returning id`,
// documentID,
// version.Time,
// ).Scan(&versionID)
// if err != nil {
// return uu.IDNil, err
// }
// log.Info("Deleted document version").
// Ctx(ctx).
// UUID("versionID", versionID).
// Log()
// return versionID, nil
// }