(fix): fix rename file corrupting database (#1308)

The rename file advice is passed relative file names: e.g. "foo.org" ->
"bar.org". This was not accounted for, and paths in the Org-roam
database are supposed to be absolute paths. This caused the storing of
relative paths in the Org-roam database, which were then never purged.

Fixes #1304
This commit is contained in:
Jethro Kuan
2020-11-19 22:26:59 +08:00
committed by GitHub
parent b17cc3b1e3
commit ab34dd138d
3 changed files with 41 additions and 37 deletions

View File

@ -8,6 +8,7 @@
### Fixed ### Fixed
- [#1281](https://github.com/org-roam/org-roam/pull/1281) fixed idle-timer not instantiated on `org-roam-mode` - [#1281](https://github.com/org-roam/org-roam/pull/1281) fixed idle-timer not instantiated on `org-roam-mode`
- [#1308](https://github.com/org-roam/org-roam/pull/1308) fixed file renames corrupting database
## 1.2.3 (13-11-2020) ## 1.2.3 (13-11-2020)

View File

@ -255,15 +255,14 @@ This function is called on `org-roam-db-file-update-timer'."
(dolist (table (mapcar #'car org-roam-db--table-schemata)) (dolist (table (mapcar #'car org-roam-db--table-schemata))
(org-roam-db-query `[:delete :from ,table])))) (org-roam-db-query `[:delete :from ,table]))))
(defun org-roam-db--clear-file (&optional filepath) (defun org-roam-db--clear-file (&optional file)
"Remove any related links to the file at FILEPATH. "Remove any related links to the FILE.
This is equivalent to removing the node from the graph." This is equivalent to removing the node from the graph."
(let ((file (expand-file-name (or filepath (setq file (or file (buffer-file-name (buffer-base-buffer))))
(buffer-file-name (buffer-base-buffer)))))) (dolist (table (mapcar #'car org-roam-db--table-schemata))
(dolist (table (mapcar #'car org-roam-db--table-schemata)) (org-roam-db-query `[:delete :from ,table
(org-roam-db-query `[:delete :from ,table :where (= ,(if (eq table 'links) 'source 'file) $s1)]
:where (= ,(if (eq table 'links) 'source 'file) $s1)] file)))
file))))
;;;;; Inserting ;;;;; Inserting
(defun org-roam-db--insert-meta (&optional update-p) (defun org-roam-db--insert-meta (&optional update-p)

View File

@ -1412,41 +1412,45 @@ To be added to `org-roam-title-change-hook'."
When NEW-FILE-OR-DIR is a directory, we use it to compute the new file path." When NEW-FILE-OR-DIR is a directory, we use it to compute the new file path."
(let ((new-file (if (directory-name-p new-file-or-dir) (let ((new-file (if (directory-name-p new-file-or-dir)
(expand-file-name (file-name-nondirectory old-file) new-file-or-dir) (expand-file-name (file-name-nondirectory old-file) new-file-or-dir)
new-file-or-dir))) new-file-or-dir))
files-affected
new-buffer)
(setq new-file (expand-file-name new-file))
(setq old-file (expand-file-name old-file))
(when (and (not (auto-save-file-name-p old-file)) (when (and (not (auto-save-file-name-p old-file))
(not (auto-save-file-name-p new-file)) (not (auto-save-file-name-p new-file))
(not (backup-file-name-p old-file)) (not (backup-file-name-p old-file))
(not (backup-file-name-p new-file)) (not (backup-file-name-p new-file))
(org-roam--org-roam-file-p old-file)) (org-roam--org-roam-file-p old-file))
(org-roam-db--ensure-built) (org-roam-db--ensure-built)
(let* ((new-buffer (or (find-buffer-visiting new-file) (setq new-buffer (or (find-buffer-visiting new-file)
(find-file-noselect new-file))) (find-file-noselect new-file)))
(files-affected (org-roam-db-query [:select :distinct [source] (setq files-affected (org-roam-db-query [:select :distinct [source]
:from links :from links
:where (= dest $s1)] :where (= dest $s1)]
old-file))) old-file))
;; Remove database entries for old-file.org ;; Remove database entries for old-file.org
(org-roam-db--clear-file old-file) (org-roam-db--clear-file old-file)
;; Replace links from old-file.org -> new-file.org in all Org-roam files with these links ;; Replace links from old-file.org -> new-file.org in all Org-roam files with these links
(mapc (lambda (file) (mapc (lambda (file)
(setq file (if (string-equal (car file) old-file) (setq file (if (string-equal (car file) old-file)
new-file new-file
(car file))) (car file)))
(with-current-buffer (or (find-buffer-visiting file) (with-current-buffer (or (find-buffer-visiting file)
(find-file-noselect file)) (find-file-noselect file))
(org-roam--replace-link old-file new-file) (org-roam--replace-link old-file new-file)
(save-buffer) (save-buffer)
(org-roam-db--update-file))) (org-roam-db--update-file)))
files-affected) files-affected)
;; If the new path is in a different directory, relative links ;; If the new path is in a different directory, relative links
;; will break. Fix all file-relative links: ;; will break. Fix all file-relative links:
(unless (string= (file-name-directory old-file) (unless (string= (file-name-directory old-file)
(file-name-directory new-file)) (file-name-directory new-file))
(with-current-buffer new-buffer (with-current-buffer new-buffer
(org-roam--fix-relative-links old-file) (org-roam--fix-relative-links old-file)
(save-buffer))) (save-buffer)))
(when (org-roam--org-roam-file-p new-file) (when (org-roam--org-roam-file-p new-file)
(org-roam-db--update-file new-file)))))) (org-roam-db--update-file new-file)))))
(defun org-roam--id-new-advice (&rest _args) (defun org-roam--id-new-advice (&rest _args)
"Update the database if a new Org ID is created." "Update the database if a new Org ID is created."