Repository navigation
Expand file tree
/
Copy pathforge-db.el
More file actions
433 lines (394 loc) · 12.1 KB
/
Copy pathforge-db.el
File metadata and controls
433 lines (394 loc) · 12.1 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
;;; forge-db.el --- Database implementation -*- lexical-binding: t -*-
;; Copyright (C) 2018-2020 Jonas Bernoulli
;; Author: Jonas Bernoulli <jonas@bernoul.li>
;; Maintainer: Jonas Bernoulli <jonas@bernoul.li>
;; Forge is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; Forge is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
;; License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with Forge. If not, see http://www.gnu.org/licenses.
;;; Code:
(require 'closql)
(require 'eieio)
(require 'emacsql)
(require 'emacsql-sqlite)
(defvar forge--db-table-schemata)
;; For `forge--db-maybe-update':
(declare-function forge-get-issue "forge-core")
(declare-function forge-get-pullreq "forge-core")
(declare-function forge--object-id "forge-core")
;;; Options
(defcustom forge-database-file
(expand-file-name "forge-database.sqlite" user-emacs-directory)
"The file used to store the forge database."
:package-version '(forge . "0.1.0")
:group 'forge
:type 'file)
;;; Core
(defclass forge-database (closql-database)
((object-class :initform forge-repository)))
(defconst forge--db-version 7)
(defconst forge--sqlite-available-p
(with-demoted-errors "Forge initialization: %S"
(emacsql-sqlite-ensure-binary)
t))
(defvar forge--db-connection nil
"The EmacSQL database connection.")
(defun forge-db ()
(unless (and forge--db-connection (emacsql-live-p forge--db-connection))
(make-directory (file-name-directory forge-database-file) t)
(closql-db 'forge-database 'forge--db-connection
forge-database-file t)
(let* ((db forge--db-connection)
(version (closql--db-get-version db))
(version (forge--db-maybe-update forge--db-connection version)))
(cond
((> version forge--db-version)
(emacsql-close db)
(user-error
"The Forge database was created with a newer Forge version. %s"
"You need to update the Forge package."))
((< version forge--db-version)
(emacsql-close db)
(error "BUG: The Forge database scheme changed %s"
"and there is no upgrade path")))))
forge--db-connection)
;;; Api
(defun forge-sql (sql &rest args)
(if (stringp sql)
(emacsql (forge-db) (apply #'format sql args))
(apply #'emacsql (forge-db) sql args)))
;;; Schemata
(defconst forge--db-table-schemata
'((repository
[(class :not-null)
(id :not-null :primary-key)
forge-id
forge
owner
name
apihost
githost
remote
sparse-p
created
updated
pushed
parent
description
homepage
default-branch
archived-p
fork-p
locked-p
mirror-p
private-p
issues-p
wiki-p
stars
watchers
(assignees :default eieio-unbound)
(forks :default eieio-unbound)
(issues :default eieio-unbound)
(labels :default eieio-unbound)
(revnotes :default eieio-unbound)
(pullreqs :default eieio-unbound)
selective-p
worktree
(milestones :default eieio-unbound)])
(assignee
[(repository :not-null)
(id :not-null :primary-key)
login
name
forge-id] ; Needed for Gitlab.
(:foreign-key
[repository] :references repository [id]
:on-delete :cascade))
(fork
[(parent :not-null)
(id :not-null :primary-key)
owner
name]
(:foreign-key
[parent] :references repository [id]
:on-delete :cascade))
(issue
[(class :not-null)
(id :not-null :primary-key)
repository
number
state
author
title
created
updated
closed
unread-p
locked-p
milestone
body
(assignees :default eieio-unbound)
(cards :default eieio-unbound)
(edits :default eieio-unbound)
(labels :default eieio-unbound)
(participants :default eieio-unbound)
(posts :default eieio-unbound)
(reactions :default eieio-unbound)
(timeline :default eieio-unbound)
(marks :default eieio-unbound)
note]
(:foreign-key
[repository] :references repository [id]
:on-delete :cascade))
(issue-assignee
[(issue :not-null)
(id :not-null)]
(:foreign-key
[issue] :references issue [id]
:on-delete :cascade))
(issue-label
[(issue :not-null)
(id :not-null)]
(:foreign-key
[issue] :references issue [id]
:on-delete :cascade)
(:foreign-key
[id] :references label [id]
:on-delete :cascade))
(issue-mark
[(issue :not-null)
(id :not-null)]
(:foreign-key
[issue] :references issue [id]
:on-delete :cascade)
(:foreign-key
[id] :references mark [id]
:on-delete :cascade))
(issue-post
[(class :not-null)
(id :not-null :primary-key)
issue
number
author
created
updated
body
(edits :default eieio-unbound)
(reactions :default eieio-unbound)]
(:foreign-key
[issue] :references issue [id]
:on-delete :cascade))
(label
[(repository :not-null)
(id :not-null :primary-key)
name
color
description]
(:foreign-key
[repository] :references repository [id]
:on-delete :cascade))
(mark
[;; For now this is always nil because it seems more useful to
;; share marks between repositories. We cannot omit this slot
;; though because `closql--iref' expects `id' to be the second
;; slot.
repository
(id :not-null :primary-key)
name
face
description])
(milestone
[(repository :not-null)
(id :not-null :primary-key)
number
title
created
updated
due
closed
description]
(:foreign-key
[repository] :references repository [id]
:on-delete :cascade))
(notification
[(class :not-null)
(id :not-null :primary-key)
thread-id
repository
forge
reason
unread-p
last-read
updated
title
type
topic
url]
(:foreign-key
[repository] :references repository [id]
:on-delete :cascade))
(pullreq
[(class :not-null)
(id :not-null :primary-key)
repository
number
state
author
title
created
updated
closed
merged
unread-p
locked-p
editable-p
cross-repo-p
base-ref
base-repo
head-ref
head-user
head-repo
milestone
body
(assignees :default eieio-unbound)
(cards :default eieio-unbound)
(commits :default eieio-unbound)
(edits :default eieio-unbound)
(labels :default eieio-unbound)
(participants :default eieio-unbound)
(posts :default eieio-unbound)
(reactions :default eieio-unbound)
(review-requests :default eieio-unbound)
(reviews :default eieio-unbound)
(timeline :default eieio-unbound)
(marks :default eieio-unbound)
note]
(:foreign-key
[repository] :references repository [id]
:on-delete :cascade))
(pullreq-assignee
[(pullreq :not-null)
(id :not-null)]
(:foreign-key
[pullreq] :references pullreq [id]
:on-delete :cascade))
(pullreq-label
[(pullreq :not-null)
(id :not-null)]
(:foreign-key
[pullreq] :references pullreq [id]
:on-delete :cascade)
(:foreign-key
[id] :references label [id]
:on-delete :cascade))
(pullreq-mark
[(pullreq :not-null)
(id :not-null)]
(:foreign-key
[pullreq] :references pullreq [id]
:on-delete :cascade)
(:foreign-key
[id] :references mark [id]
:on-delete :cascade))
(pullreq-post
[(class :not-null)
(id :not-null :primary-key)
pullreq
number
author
created
updated
body
(edits :default eieio-unbound)
(reactions :default eieio-unbound)]
(:foreign-key
[pullreq] :references pullreq [id]
:on-delete :cascade))
(pullreq-review-request
[(pullreq :not-null)
(id :not-null)]
(:foreign-key
[pullreq] :references pullreq [id]
:on-delete :cascade))
(revnote
[(class :not-null)
(id :not-null :primary-key)
repository
commit
file
line
author
body]
(:foreign-key
[repository] :references repository [id]
:on-delete :cascade))))
(cl-defmethod closql--db-init ((db forge-database))
(emacsql-with-transaction db
(pcase-dolist (`(,table . ,schema) forge--db-table-schemata)
(emacsql db [:create-table $i1 $S2] table schema))
(closql--db-set-version db forge--db-version)))
(defun forge--db-maybe-update (db version)
(emacsql-with-transaction db
(when (= version 2)
(message "Upgrading Forge database from version 2 to 3...")
(emacsql db [:create-table pullreq-review-request $S1]
(cdr (assq 'pullreq-review-request forge--db-table-schemata)))
(closql--db-set-version db (setq version 3))
(message "Upgrading Forge database from version 2 to 3...done"))
(when (= version 3)
(message "Upgrading Forge database from version 3 to 4...")
(emacsql db [:drop-table notification])
(pcase-dolist (`(,table . ,schema) forge--db-table-schemata)
(when (memq table '(notification
mark issue-mark pullreq-mark))
(emacsql db [:create-table $i1 $S2] table schema)))
(emacsql db [:alter-table issue :add-column marks :default $s1] 'eieio-unbound)
(emacsql db [:alter-table pullreq :add-column marks :default $s1] 'eieio-unbound)
(closql--db-set-version db (setq version 4))
(message "Upgrading Forge database from version 3 to 4...done"))
(when (= version 4)
(message "Upgrading Forge database from version 4 to 5...")
(emacsql db [:alter-table repository :add-column selective-p :default nil])
(closql--db-set-version db (setq version 5))
(message "Upgrading Forge database from version 4 to 5...done"))
(when (= version 5)
(message "Upgrading Forge database from version 5 to 6...")
(emacsql db [:alter-table repository :add-column worktree :default nil])
(closql--db-set-version db (setq version 6))
(message "Upgrading Forge database from version 5 to 6...done"))
(when (= version 6)
(message "Upgrading Forge database from version 6 to 7...")
(emacsql db [:alter-table issue :add-column note :default nil])
(emacsql db [:alter-table pullreq :add-column note :default nil])
(emacsql db [:create-table milestone $S1]
(cdr (assq 'milestone forge--db-table-schemata)))
(emacsql db [:alter-table repository :add-column milestones :default $s1]
'eieio-unbound)
(pcase-dolist (`(,repo-id ,issue-id ,milestone)
(emacsql db [:select [repository id milestone]
:from issue
:where (notnull milestone)]))
(unless (stringp milestone)
(oset (forge-get-issue issue-id) milestone
(forge--object-id repo-id (cdar milestone)))))
(pcase-dolist (`(,repo-id ,pullreq-id ,milestone)
(emacsql db [:select [repository id milestone]
:from pullreq
:where (notnull milestone)]))
(unless (stringp milestone)
(oset (forge-get-pullreq pullreq-id) milestone
(forge--object-id repo-id (cdar milestone)))))
(closql--db-set-version db (setq version 7))
(message "Upgrading Forge database from version 6 to 7...done"))
version))
;;; _
(provide 'forge-db)
;;; forge-db.el ends here