enhance(cli): import edn can import into existing graph

This functionality is available in the old cli so making sure it's
available in the new one. Also update messaging so it's clear when a new
graph is created
This commit is contained in:
Gabriel Horner
2026-03-25 17:52:58 -04:00
parent 46ec2187db
commit 0009e7cc21
6 changed files with 41 additions and 18 deletions

View File

@@ -105,7 +105,7 @@
:cmds ["{{cli}} --data-dir {{data-dir-arg}} --config {{config-path-arg}} --output json graph import --graph {{graph-arg}} --type edn --input {{export-path-arg}}"]
:expect {:exit 0
:stdout-json-paths {[:status] "ok"}
:stdout-contains ["imported edn from" "{{export-path}}"]}
:stdout-contains ["Imported edn from" "{{export-path}}"]}
:covers {:commands ["graph import"]
:options {:global ["--config" "--graph" "--data-dir" "--output"]
:graph ["--type" "--input"]}}

View File

@@ -151,7 +151,7 @@
:import-type import-type
:input input
:allow-missing-graph true
:require-missing-graph true}}))
:require-missing-graph (= import-type "sqlite")}}))
(defn- graph-item->graph-name
[item]
@@ -304,7 +304,10 @@
(defn execute-graph-import
[action config]
(-> (p/let [_ (cli-server/stop-server! config (:repo action))
(-> (p/let [existing-graphs (cli-server/list-graphs config)
graph (core/repo->graph (:repo action))
new-graph? (not (some #(= graph %) existing-graphs))
_ (cli-server/stop-server! config (:repo action))
cfg (cli-server/ensure-server! config (:repo action))
import-type (:import-type action)
input-data (case import-type
@@ -321,4 +324,7 @@
_ (transport/invoke cfg method direct-pass? [(:repo action) payload])
_ (cli-server/restart-server! config (:repo action))]
{:status :ok
:data {:message (str "imported " import-type " from " (:input action))}})))
:data {:new-graph? new-graph?
:message (str (when new-graph?
(str "Created graph " graph "\n"))
"Imported " import-type " from " (:input action))}})))

View File

@@ -363,8 +363,7 @@
(defn- ensure-missing-graph
[action config]
(if (and (:repo action)
(or (:require-missing-graph action)
(= :graph-import (:type action))))
(:require-missing-graph action))
(p/let [graphs (cli-server/list-graphs config)
graph (command-core/repo->graph (:repo action))]
(if (some #(= graph %) graphs)

View File

@@ -606,8 +606,8 @@
(str "Exported " export-type " to " file))
(defn- format-graph-import
[{:keys [import-type input]}]
(str "Imported " import-type " from " input))
[_context {:keys [message]}]
message)
(defn- format-graph-action
[command {:keys [graph]}]
@@ -684,7 +684,7 @@
:remove-tag (format-remove-tag context)
:remove-property (format-remove-property context)
:graph-export (format-graph-export context)
:graph-import (format-graph-import context)
:graph-import (format-graph-import context data)
:query (format-query-results (:result data))
:query-list (format-query-list (:queries data))
:show (or (:message data) (pr-str data))

View File

@@ -2,6 +2,7 @@
(:require [cljs.test :refer [async deftest is testing]]
[clojure.string :as string]
[logseq.cli.command.add :as add-command]
[logseq.cli.command.graph :as graph-command]
[logseq.cli.command.list :as list-command]
[logseq.cli.command.show :as show-command]
[logseq.cli.command.sync :as sync-command]
@@ -2409,14 +2410,30 @@
(deftest test-execute-graph-import-rejects-existing-graph
(async done
(-> (p/with-redefs [cli-server/list-graphs (fn [_] ["demo"])
cli-server/ensure-server! (fn [_ _]
(throw (ex-info "should not start server" {})))]
(p/let [result (commands/execute {:type :graph-import :repo "logseq_db_demo" :allow-missing-graph true} {})]
(is (= :error (:status result)))
(is (= :graph-exists (get-in result [:error :code])))))
(p/catch (fn [e] (is false (str "unexpected error: " e))))
(p/finally done))))
(let [{:keys [action]} (graph-command/build-import-action "logseq_db_demo" "sqlite" "/tmp/test-db.sqlite")]
(-> (p/with-redefs [cli-server/list-graphs (fn [_] ["demo"])
cli-server/ensure-server! (fn [_ _]
(throw (ex-info "should not start server" {})))]
(p/let [result (commands/execute action {})]
(is (= :error (:status result)))
(is (= :graph-exists (get-in result [:error :code])))))
(p/catch (fn [e] (is false (str "unexpected error: " e))))
(p/finally done)))))
(deftest test-execute-graph-import-edn-allows-existing-graph
(async done
(let [{:keys [action]} (graph-command/build-import-action "logseq_db_demo" "edn" "/tmp/test.edn")]
(-> (p/with-redefs [cli-server/list-graphs (fn [_] ["demo"])
cli-server/stop-server! (fn [_ _] (p/resolved {:ok? true}))
cli-server/restart-server! (fn [_ _] (p/resolved {:ok? true}))
cli-server/ensure-server! (fn [config _] (assoc config :base-url "http://example"))
transport/read-input (fn [_] {:page "Test"})
transport/invoke (fn [_ _ _ _] {:ok true})]
(p/let [result (commands/execute action {})]
(is (= :ok (:status result))
"edn import into existing graph should succeed")))
(p/catch (fn [e] (is false (str "unexpected error: " e))))
(p/finally done)))))
(deftest test-execute-sync-download-rejects-existing-graph
(async done

View File

@@ -324,7 +324,8 @@
(let [result (format/format-result {:status :ok
:command :graph-import
:context {:import-type "sqlite"
:input "/tmp/import.sqlite"}}
:input "/tmp/import.sqlite"}
:data {:message "Imported sqlite from /tmp/import.sqlite"}}
{:output-format nil})]
(is (= "Imported sqlite from /tmp/import.sqlite" result)))))