Update handling of the database

v2 reverted to using `emacsql-sqlite` instead of `emacsql-sqlite3`. It
will now try to build the needed `sqlite3` executable by itself, using a
C compiler that it can find, which is normally gcc or clang.

Previously in v1 it would only check for `sqlite3` executable (using
`executable-find`) and wouldn't do anything else.
This commit is contained in:
Wetlize 2021-07-23 00:40:49 +03:00
parent 5ef733b488
commit 9e12873779
2 changed files with 31 additions and 6 deletions

View file

@ -24,7 +24,7 @@ of org-mode to properly utilize ID links.")
:init
(doom-load-packages-incrementally
'(ansi-color dash f rx seq magit-section))
'(ansi-color dash f rx seq magit-section emacsql emacsql-sqlite))
(map! :after org
:map org-mode-map
@ -65,17 +65,41 @@ of org-mode to properly utilize ID links.")
;; Don't display warning message dedicated for v1 users. Need to be set early.
(setq org-roam-v2-ack t)
(defadvice! +org-roam-suppress-sqlite-build-a (orig-fn &rest args)
"Suppress automatic building of sqlite3 binary when loading `org-roam'.
This is a blocking operation that can take a while to complete
and better be deferred when there will be an actual demand for
the database. See `+org-init-roam-h' for the launch process."
:around #'emacsql-sqlite-ensure-binary
(if (not (boundp 'org-roam-db-location))
(apply orig-fn args)
(advice-remove #'emacsql-sqlite-ensure-binary #'+org-roam-suppress-sqlite-build-a)
nil))
:config
(defun +org-init-roam-h ()
"Setup `org-roam' but don't immediately initialize its database.
Instead, initialize it when it will be actually needed."
(letf! ((#'org-roam-db-sync #'ignore))
(org-roam-setup))
(defadvice! +org-roam-init-db-a (&rest _)
"Initialize org-roam database when it's first time queried to."
(defadvice! +org-roam-try-init-db-a (&rest _)
"Try to initialize org-roam database at the last possible safe moment.
In case of failure, fail gracefully."
:before #'org-roam-db-query
(advice-remove 'org-roam-db-query #'+org-roam-init-db-a)
(message "Initializing org-roam database...")
(let ((run-cleanup-p t))
(unwind-protect
;; Try to build the binary if it doesn't exist. In case of failure
;; this will error, run the cleanup and exit, and in case of success
;; this will return nil and sync the database.
(setq run-cleanup-p (emacsql-sqlite-ensure-binary))
(when run-cleanup-p
(setq org-roam--sqlite-available-p nil)
(org-roam-teardown)
(message (concat "EmacSQL failied to build SQLite binary for org-roam; "
"see *Compile-Log* buffer for details.\n"
"To try reinitialize org-roam, run \"M-x org-roam-setup\"")))))
(advice-remove 'org-roam-db-query #'+org-roam-try-init-db-a)
(org-roam-db-sync)))
(setq org-roam-directory

View file

@ -5,10 +5,11 @@
(unless (executable-find "gnuplot")
(warn! "Couldn't find gnuplot. org-plot/gnuplot will not work")))
(when (featurep! +roam)
(unless (executable-find "sqlite3")
(warn! "Couldn't find the sqlite3 executable. org-roam will not work.")))
(when (or (featurep! +roam)
(featurep! +roam2))
(unless (executable-find "sqlite3")
(warn! "Couldn't find the sqlite3 executable. org-roam will not work."))
(unless (executable-find "dot")
(warn! "Couldn't find the dot executable (from graphviz). org-roam will not be able to generate graph visualizations.")))