Repository: clojure-lsp/clojure-lsp
Pull request #325 (2021-02-17 06:39:37)

Configurable db path with :sqlite-db-path
Main use case is to allow storing sqlite.db somewhere else.


Patch:
diff --git a/deps.edn b/deps.edn
index 1c5e82a3..128fbe7e 100644
--- a/deps.edn
+++ b/deps.edn
@@ -15,5 +15,7 @@
         medley/medley {:mvn/version "1.3.0"}
         clj-kondo/clj-kondo {:mvn/version "2021.02.13"}}
  :paths ["resources" "src" "target/classes"]
- :aliases {:run {:main-opts ["-m" "clojure-lsp.main"]
+ :aliases {:test {:extra-paths ["test"]}
+           :debug {:extra-deps {nrepl/nrepl {:mvn/version "0.8.3"}}}
+           :run {:main-opts ["-m" "clojure-lsp.main"]
                  :jvm-opts ["-Xmx2g" "-server"]}}}
diff --git a/docs/settings.md b/docs/settings.md
index 5fde8278..961898fb 100644
--- a/docs/settings.md
+++ b/docs/settings.md
@@ -46,6 +46,7 @@ This is an [example how Emacs `lsp-mode`](https://github.com/emacs-lsp/lsp-mode/
 | `dependency-scheme`             | How the dependencies should be linked, `jar` will make urls compatible with java's JarURLConnection. You can have the client make an lsp extension request of `clojure/dependencyContents` with the jar uri and the server will return the jar entry's contents. [Similar to java clients](https://github.com/redhat-developer/vscode-java/blob/a24945453092e1c39267eac9367c759a6c7b0497/src/extension.ts#L290-L298) | `zip`             |
 | `cljfmt`                        | Used for formatting, json encoded configuration for [cljfmt](https://github.com/weavejester/cljfmt)                                                                                                                                                                                                                                                                                                                  | `{}`              |
 | `project-specs`                 | A vector containing a map of key/value pairs, defining how `clojure-lsp` should find your project classpath                                                                                                                                                                                                                                                                                                          | Check the default [here](https://github.com/clojure-lsp/clojure-lsp/blob/master/src/clojure_lsp/crawler.clj#L53-L60)                  |
+| `sqlite-db-path`                | Where to store the project's analysis cache, used to speed up next `clojure-lsp` startup. A path relative to project root or an absolute path. | `.lsp/sqlite.db`
 
 ### Lint
 
diff --git a/src/clojure_lsp/db.clj b/src/clojure_lsp/db.clj
index bde48fdf..8c669e38 100644
--- a/src/clojure_lsp/db.clj
+++ b/src/clojure_lsp/db.clj
@@ -13,8 +13,17 @@
 
 (def version 1)
 
+(defn ^:private get-sqlite-db-file [project-root]
+  (let [configured (some-> (get-in @db [:settings :sqlite-db-path])
+                           io/file)
+        default (io/file (str project-root) ".lsp" "sqlite.db")
+        file (or configured default)]
+    (if (.isAbsolute file)
+      file
+      (io/file (str project-root) file))))
+
 (defn ^:private make-spec [project-root]
-  (let [lsp-db (io/file (str project-root) ".lsp" "sqlite.db")]
+  (let [lsp-db (get-sqlite-db-file project-root)]
     {:dbtype "sqlite"
      :dbname (.getAbsolutePath lsp-db)}))
 


Test patch:
diff --git a/test/clojure_lsp/db_test.clj b/test/clojure_lsp/db_test.clj
new file mode 100644
index 00000000..fc2b2ea2
--- /dev/null
+++ b/test/clojure_lsp/db_test.clj
@@ -0,0 +1,27 @@
+(ns clojure-lsp.db-test
+  (:require [clojure-lsp.db :as db]
+            [clojure-lsp.test-helper :as h]
+            [clojure.test :refer [deftest testing is]]
+            [clojure.java.io :as io]
+            [clojure.string :as s]))
+
+(h/reset-db-after-test)
+
+(def project-path "/user/project")
+(def default-db-path (s/join "/" [project-path ".lsp" "sqlite.db"]))
+
+(deftest sqlite-db-file-setting
+  (testing "when not set"
+    (reset! db/db {})
+    (is (= (io/file default-db-path) (#'db/get-sqlite-db-file project-path))))
+  (testing "when set to relative path"
+    (let [settings-path "subdir/sqlite.db"
+          expected (s/join "/" [project-path settings-path])]
+      (reset! db/db {:settings {:sqlite-db-path settings-path}})
+      (is (= (io/file expected)
+             (#'db/get-sqlite-db-file project-path)))))
+  (testing "when set to absolute path"
+    (let [settings-path "/db-dir/sqlite.db"]
+      (reset! db/db {:settings {:sqlite-db-path settings-path}})
+      (is (= (io/file settings-path)
+             (#'db/get-sqlite-db-file project-path))))))