Skip to content

Commit

Permalink
closql--db-update-schema: Fix updating from version 9 or older
Browse files Browse the repository at this point in the history
We cannot use the object abstraction provided by closql while updating
table schemata because the functions that generate such objects assume
the latest schemata.  When updating from the second to last version,
then it can work (as it did when updating from 9 to 10), provided
columns are added first.  However, if a later version bump modifies a
relevant table, earlier update steps that use objects, are likely to
break.

Closes #625.
Closes #626.
  • Loading branch information
tarsius committed Feb 6, 2024
1 parent b16b6ec commit 3fc6c36
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions lisp/forge-db.el
Original file line number Diff line number Diff line change
Expand Up @@ -447,18 +447,25 @@
(message "Upgrading Forge database from version 9 to 10...")
(emacsql db [:alter-table pullreq :add-column slug :default nil])
(emacsql db [:alter-table issue :add-column slug :default nil])
(dolist (o (closql-entries (forge-db) nil 'forge-pullreq))
(oset o slug
(format "%s%s"
(if (and (fboundp
'forge-gitlab-repository--eieio-childp)
(forge-gitlab-repository--eieio-childp
(forge-get-repository o)))
"!"
"#")
(oref o number))))
(dolist (o (closql-entries (forge-db) nil 'forge-issue))
(oset o slug (format "#%s" (oref o number))))
(pcase-dolist (`(,id ,number ,type)
(emacsql
db
[:select [pullreq:id pullreq:number repository:class]
:from pullreq
:join repository
:on (= pullreq:repository repository:id)]))
(let ((gitlabp (memq type
(append (closql-where-class-in
'forge-gitlab-repository--eieio-childp)
nil))))
(emacsql db [:update pullreq :set (= slug $s1) :where (= id $s2)]
(format "%s%s" (if gitlabp "!" "#") number)
id)))
(pcase-dolist (`(,id ,number)
(emacsql db [:select [id number] :from issue]))
(emacsql db [:update issue :set (= slug $s1) :where (= id $s2)]
(format "#%s" number)
id))
(closql--db-set-version db (setq version 10))
(message "Upgrading Forge database from version 9 to 10...done"))
(when (= version 10)
Expand All @@ -478,13 +485,17 @@
(emacsql db [:create-table notification $S1]
(cdr (assq 'notification forge--db-table-schemata)))
(dolist (id (emacsql db [:select id :from issue :where (= state 'closed)]))
(oset (closql-get db (car id) 'forge-issue) state 'completed))
(emacsql db [:update issue :set (= state 'completed) :where (= id $s1)]
id))
(dolist (id (emacsql db [:select id :from issue :where (isnull status)]))
(oset (closql-get db (car id) 'forge-issue) status 'done))
(emacsql db [:update issue :set (= state 'done) :where (= id $s1)]
id))
(dolist (id (emacsql db [:select id :from pullreq :where (= state 'closed)]))
(oset (closql-get db (car id) 'forge-pullreq) state 'rejected))
(emacsql db [:update pullreq :set (= state 'rejected) :where (= id $s1)]
id))
(dolist (id (emacsql db [:select id :from pullreq :where (isnull status)]))
(oset (closql-get db (car id) 'forge-pullreq) status 'done))
(emacsql db [:update pullreq :set (= state 'done) :where (= id $s1)]
id))
(emacsql db [:alter-table repository :add-column issues-until :default nil])
(emacsql db [:alter-table repository :add-column pullreqs-until :default nil])
(closql--db-set-version db (setq version 12))
Expand Down

0 comments on commit 3fc6c36

Please sign in to comment.