Skip to content

Commit

Permalink
Fix for use with updated closql
Browse files Browse the repository at this point in the history
See magit/closql#8
Unfortunately this commit seems not to completely fix the package
  • Loading branch information
dalanicolai committed Jul 7, 2023
1 parent 26f426e commit 94d5272
Showing 1 changed file with 28 additions and 61 deletions.
89 changes: 28 additions & 61 deletions code-review-db.el
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,12 @@
(require 'uuidgen)
(require 'dash)

(defcustom code-review-db-database-connector 'sqlite
"The database connector."
:group 'code-review
:type 'keyword)

(defcustom code-review-db-database-file
(expand-file-name "code-review-db-file.sqlite" user-emacs-directory)
"The file used to store the `code-review' database."
:group 'code-review
:type 'file)

(declare-function code-review-db-database--eieio-childp "code-review-db.el" (obj) t)

(defclass code-review-db-buffer (closql-object)
((closql-table :initform 'buffer)
(closql-primary-key :initform 'id)
Expand Down Expand Up @@ -116,41 +109,21 @@
(buffer :closql-class code-review-db-buffer))
:abstract t)

(defclass code-review-db-database (emacsql-sqlite-connection closql-database)
((object-class :initform 'code-review-db-pullreq)))

;;; LOL, why? why did I started the database on version 7? :/
;;; damn copy and paste from `forge-db'.
(defconst code-review-db-version 8)

(defconst code-review-db--sqlite-available-p
(with-demoted-errors "Code Review initialization: %S"
(emacsql-sqlite-ensure-binary)
t))

(defvar code-review-db-connection nil
"The EmacSQL database connection.")

(defun code-review-db ()
"Start connection."
(unless (and code-review-db-connection (emacsql-live-p code-review-db-connection))
(make-directory (file-name-directory code-review-db-database-file) t)
(closql-db 'code-review-db-database 'code-review-db-connection
code-review-db-database-file t)
(let* ((db code-review-db-connection)
(version (closql--db-get-version db))
(version (code-review--db-maybe-update code-review-db-connection version)))
(cond
((> version code-review-db-version)
(emacsql-close db)
(user-error
"The Code Review database was created with a newer Code Review version. %s"
"You need to update the Code Review package."))
((< version code-review-db-version)
(emacsql-close db)
(error "BUG: The Code Review database scheme changed %s"
"and there is no upgrade path.")))))
code-review-db-connection)
(defclass code-review-db-database (closql-database)
((name :initform "Code-review")
(object-class :initform 'code-review-db-pullreq)
(file :initform 'code-review-db-database-file)
(schema :initform 'code-review-db-table-schema)
(version :initform 9)))

(defvar code-review--override-connection-class nil)
(defvar code-review--sqlite-available-p t)

(defun code-review-db (&optional livep)
(condition-case err
(closql-db 'code-review-db-database livep code-review--override-connection-class)
(error (setq code-review--sqlite-available-p nil)
(signal (car err) (cdr err)))))

;;; Schema

Expand Down Expand Up @@ -220,22 +193,17 @@
[path] :references path [id]
:on-delete :cascade))))

(cl-defmethod closql--db-init ((db code-review-db-database))
"Initialize the DB."
(emacsql-with-transaction db
(pcase-dolist (`(,table . ,schema) code-review-db-table-schema)
(emacsql db [:create-table $i1 $S2] table schema))
(closql--db-set-version db code-review-db-version)))

(defun code-review--db-maybe-update (db version)
(emacsql-with-transaction db
(when (= version 7)
(message "Upgrading Code Review database from version 7 to 8...")
(cl-defmethod closql--db-update-schema (db code-review-db-database)
(let ((code-version (oref-default 'code-review-db-database version))
(version (closql--db-get-version db)))
(closql-with-transaction db
(when (< version code-version)
(message "Upgrading Code Review database from version %s to %s..." version code-version)
(emacsql db [:alter-table pullreq :add-column base-ref-name :default nil])
(emacsql db [:alter-table pullreq :add-column head-ref-name :default nil])
(closql--db-set-version db (setq version 8))
(message "Upgrading Code Review database from version 7 to 8...done"))
version))
(message "Upgrading Code Review database from version %s to %s...done" version code-version)))
(cl-call-next-method)))

;;; Core

Expand Down Expand Up @@ -267,11 +235,10 @@

(defun code-review-db-all-unfinished ()
"Get a list of all unfinished Reviews."
(when (and code-review-db-connection (emacsql-live-p code-review-db-connection))
(emacsql-close code-review-db-connection)
(setq code-review-db-connection nil))
(when-let ((db (code-review-db t)))
(emacsql-close db))
(let ((class 'code-review-db-pullreq)
(db (code-review-db)))
(db (code-review-db t)))
(->> (emacsql db
[:select :*
:from 'pullreq
Expand Down Expand Up @@ -436,12 +403,12 @@
:buffer pr-id
:name curr-path
:at-pos-p t)))
(emacsql-with-transaction db
(closql-with-transaction db
(closql-insert db buf t)
(closql-insert db path t)))
(let* ((paths (oref buf paths))
(curr-path-re-enabled? nil))
(emacsql-with-transaction db
(closql-with-transaction db
;;; disable all previous ones and enable curr-path is already exists
(-map
(lambda (path)
Expand Down

0 comments on commit 94d5272

Please sign in to comment.