fix(cli): ensure cli commands are running against DB graphs

Fixed all local graphs and a couple of API commands
This commit is contained in:
Gabriel Horner
2025-11-18 11:56:45 -05:00
parent 5489e7572f
commit d6a20e6a9b
8 changed files with 25 additions and 6 deletions

View File

@@ -78,5 +78,6 @@
(cli-util/error "Command missing required option 'graph'"))
(if (fs/existsSync (cli-util/get-graph-path graph))
(let [conn (apply sqlite-cli/open-db! (cli-util/->open-db-args graph))]
(cli-util/ensure-db-graph-for-command @conn)
(export-repo-as-markdown! (str common-config/db-version-prefix graph) @conn opts))
(cli-util/error "Graph" (pr-str graph) "does not exist")))

View File

@@ -24,6 +24,7 @@
(cli-util/error "Command missing required option 'graph'"))
(if (fs/existsSync (cli-util/get-graph-path graph))
(let [conn (apply sqlite-cli/open-db! (cli-util/->open-db-args graph))
_ (cli-util/ensure-db-graph-for-command @conn)
export-map (sqlite-export/build-export @conn (build-export-options options))]
(write-export-edn-map export-map options))
(cli-util/error "Graph" (pr-str graph) "does not exist")))

View File

@@ -22,6 +22,7 @@
(let [graph-dir (cli-util/get-graph-path graph)]
(if (fs/existsSync graph-dir)
(let [conn (apply sqlite-cli/open-db! (cli-util/->open-db-args graph))
_ (cli-util/ensure-db-graph-for-command @conn)
kv-value #(:kv/value (d/entity @conn %))]
(pprint/print-table
(map #(array-map "Name" (first %) "Value" (second %))

View File

@@ -24,6 +24,7 @@
(cli-util/error "Command missing required option 'graph'"))
(if (fs/existsSync (cli-util/get-graph-path graph))
(let [conn (apply sqlite-cli/open-db! (cli-util/->open-db-args graph))
_ (cli-util/ensure-db-graph-for-command @conn)
{:keys [init-tx block-props-tx misc-tx]}
(sqlite-export/build-import import-map @conn {})
txs (vec (concat init-tx block-props-tx misc-tx))]

View File

@@ -77,6 +77,7 @@
(doseq [graph graphs]
(if (fs/existsSync (cli-util/get-graph-path graph))
(let [conn (apply sqlite-cli/open-db! (cli-util/->open-db-args graph))
_ (cli-util/ensure-db-graph-for-command @conn)
query* (when (string? (first args)) (common-util/safe-read-string {:log-error? false} (first args)))
results (cond
;; Run datalog query if detected

View File

@@ -55,6 +55,7 @@
(cli-util/error "Command missing required option 'graph'"))
(if (fs/existsSync (cli-util/get-graph-path graph))
(let [conn (apply sqlite-cli/open-db! (cli-util/->open-db-args graph))
_ (cli-util/ensure-db-graph-for-command @conn)
nodes (->> (d/datoms @conn :aevt :block/title)
(filter (fn [datom]
(string/includes? (:v datom) search-term)))

View File

@@ -5,6 +5,7 @@
[clojure.string :as string]
[logseq.cli.common.graph :as cli-common-graph]
[logseq.db.common.sqlite :as common-sqlite]
[logseq.db.common.entity-plus :as entity-plus]
[nbb.error]
[promesa.core :as p]))
@@ -79,4 +80,9 @@
"class" "classes"} word (str word "s"))))]
(str (count (:properties edn-map)) " " (pluralize "property" (count (:properties edn-map))) ", "
(count (:classes edn-map)) " " (pluralize "class" (count (:classes edn-map))) " and "
(count (:pages-and-blocks edn-map)) " " (pluralize "page" (count (:pages-and-blocks edn-map))))))
(count (:pages-and-blocks edn-map)) " " (pluralize "page" (count (:pages-and-blocks edn-map))))))
(defn ensure-db-graph-for-command
[db]
(when-not (entity-plus/db-based-graph? db)
(error "This command must be called on a DB graph")))

View File

@@ -1,14 +1,14 @@
(ns logseq.api.db-based.cli
"API fns for CLI"
(:require [frontend.handler.ui :as ui-handler]
"API fns for CLI. DB graph checks are either at the top level or in cli-common-mcp-tools ns."
(:require [clojure.string :as string]
[frontend.handler.ui :as ui-handler]
[frontend.modules.outliner.op :as outliner-op]
[frontend.modules.outliner.ui :as ui-outliner-tx]
[frontend.state :as state]
[logseq.cli.common.mcp.tools :as cli-common-mcp-tools]
[logseq.common.config :as common-config]
[logseq.db.sqlite.util :as sqlite-util]
[promesa.core :as p]
[clojure.string :as string]
[logseq.common.config :as common-config]))
[promesa.core :as p]))
(defn list-tags
[options]
@@ -53,9 +53,15 @@
(ui-handler/re-render-root!)
(cli-common-mcp-tools/summarize-upsert-operations ops options)))
(defn- ensure-db-graph
[repo]
(when-not (sqlite-util/db-based-graph? repo)
(throw (ex-info "This endpoint must be called on a DB graph" {}))))
(defn import-edn
"Given EDN data as a transitized string, converts to EDN and imports it."
[edn-data*]
(ensure-db-graph (state/get-current-repo))
(p/let [edn-data (sqlite-util/transit-read edn-data*)
{:keys [error]} (ui-outliner-tx/transact!
{:outliner-op :batch-import-edn}
@@ -67,6 +73,7 @@
"Given sqlite.export options, exports the current graph as a json map with the
:export-body key containing a transit string of the export EDN"
[options*]
(ensure-db-graph (state/get-current-repo))
(p/let [options (-> (js->clj options* :keywordize-keys true)
(update :export-type (fnil keyword :graph)))
result (state/<invoke-db-worker :thread-api/export-edn (state/get-current-repo) options)]