One example of an edge case:
- If a file has LF line endings,
- and is situated in a repository where .editorconfig asks for
CRLF,
(Such repositories exist, for example
<https://github.com/obsidianmd/obsidian-docs/>.)
- and editorconfig-mode is enabled,
org-roam-db--file-hash would then return different results depending
on whether the file path is passed to it or not.
While this is a contrived edge case, there may be other cases like
this where the data from insert-file-contents-literally and the data
from find-file-noselect makes secure-hash return different values.
This commit removes the branch in org-roam-db--file-hash to compute
the hash based on buffer content, instead making it always require a
file path.
(This commit also allows org-roam-db-insert-file to take an optional
HASH argument, allowing it to avoid recalculating the hash.)
Since now we always compute the hash with the file on disk, the
special case for encrypted files is no longer needed.
This should have no impact on whether db is synchronized. The only
case when db--file-hash uses the "calculate in current buffer" path is
in db-insert-file; the only use of db-insert-file is in
db-update-file, which already calls db--file-hash with the file path,
and does not rely on the value from the current buffer.
When trying to save from a narrowed buffer, only the Roam nodes visible
in the narrowed buffer are written to the database.
This commit ensures that org-roam behaves the same whether the current
buffer is narrowed or not.
Previously we only updated the database on file save, but we need to
update the file's atime on file access, so we hook into `find-file-hook`
to do so. We only update the file atime if the file exists in the file table.
Adds two customizable variables: org-roam-db-extra-links-elements and
org-roam-db-extra-links-exclude-keys, which govern which elements are to
be considered for link parsing by Org-roam.
Also added sane defaults to org-roam-db-extra-links-exclude-keys.
* (fix):db: pragma foreign keys to work with sqlite3
This commit is follow-up for PR #2009 should fix issues #1927 & #1910.
Currently, `[:pragma (= foreign_keys ON)]` is present only in function
`org-roam-db--init`. This means the `foreign_keys` is turned on only when the db
file is created for the first time. Subsequent Emacs sessions won't turn it on
as the db file is already present and does not evaluate `org-roam-db--init`.
This PRAGMA needs to be turned on each database connection at runtime according
to sqlite's documentation (https://sqlite.org/foreignkeys.html#fk_enable)
```
Foreign key constraints are disabled by default (for backwards compatibility),
so must be enabled separately for each database connection
```
I have observed that on Windows only the Emacs session that initially creates
the Org-roam db file works correctly with the `sqlite3` option. Subsequent Emacs
sessions have the same "Unique constraint failed" error messages.
I have tested this patch on Windows and Ubuntu; both seem to work in the first
and subsequent Emacs sessions.
I am not 100% sure if the location of the PRAGMA is the best; use it as a
proof-of-cocenpt for the fix. Thank you.
* remove: [:pragma (= foreign_keys ON)] from org-roam-db--init
* fix: defconst org-roam--sqlite-available-p outputs error
When `org-roam-database-connector` is not `sqlite`, it outputs an unnecessary
error when Org-roam starts up as `defconst` evaluates
`(emacsql-sqlite-ensure-binary)`.
```
Org-roam initialization: (error "No EmacSQL SQLite binary available, aborting")
```
For other database connectors, this is not relevant.
* chore: rm org-roam--sqlite-available-p
As per our conversation, org-roam--sqlite-available-p not needed.
Previously node caching used org-map-entries: this only mapped over agenda
entries, hence skipping various nodes. Instead, we should be using
org-map-region, which maps over the entire file.
Org-ref v3 introduced a new citation syntax, and removed some functions
that org-roam had previously relied upon (e.g. for extracting keys from
multi-citations).
This commit introduces support on two fronts:
1. Add `org-roam-org-ref-path-to-keys` which converts a link path to a
list of keys. This supports both Org-ref v2 and v3.
2. Adds support for parsing multi-citations in refs.
Require optional libraries 'org-ref and 'oc only once per function run:
org-roam-db-update-file and org-roam-db-sync will call these requires at
the top level instead of within the link mapper.
Instead check if the (autoloaded) function `org-cite-insert' is bound.
This prevents a lot of unnecessary filesystem I/O when generating the
org roam database.
The current implementation uses org-element-map to iterate through all
links in the current buffer. This function doesn't consider links
within properties drawer. This implementation does consider such
links.
Support caching the new Org 9.5 citations.
Because citations now has first-class support, and are treated
differently from links, they are now cached in their own table.
Org-ref citations, instead of being stored in the links table, are now
stored in the citations table instead.
To use a citation as a ROAM_REF, use the `@citeKey` syntax
Previously when ROAM_REFS is non-empty, but the refs are invalid, this
will cause an empty SQL expression and crash the db caching. This fixes
that scenario.
(condition-case nil
(test)
(t nil))
Is a construct that's only available starting from Emacs 27. Since
Org-roam supports Emacs 26, gotta use what's given.
Note: ignore-errors isn't exactly the same as the construct above from
Emacs 27. Compare to ignore-errors to would trap all possible signals,
while ignore-errors will only trap "error" signals. This, however,
shouldn't affect us in case of this refactoring.
Fixes#1777.
When inserting the GPG decrypted org buffer to to DB, make sure that
the hash of the encrypted file is stored.
This prevents reparsing of GPG files over and over again when
synchronizing the database with the filesystem.
Co-authored-by: Jethro Kuan <jethrokuan95@gmail.com>
Detangle the codebase and change how dependencies are resolved to
allow the package to better modularize and load itself without
introducing circular dependencies, especially when autoloads involved.
A force resync of the database does not work in Windows (Cygwin):
apply: Removing old name: Device or resource busy, /home/brschoen/.emacs.d/org-roam.db
Therefore, close the connection first and then try to remove the database file.