diff --git a/cli-e2e/spec/non_sync_cases.edn b/cli-e2e/spec/non_sync_cases.edn index 4c3bdfe523..2f771a3268 100644 --- a/cli-e2e/spec/non_sync_cases.edn +++ b/cli-e2e/spec/non_sync_cases.edn @@ -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"]}} diff --git a/src/main/logseq/cli/command/graph.cljs b/src/main/logseq/cli/command/graph.cljs index 7e7fdb1b94..c413a7e963 100644 --- a/src/main/logseq/cli/command/graph.cljs +++ b/src/main/logseq/cli/command/graph.cljs @@ -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))}}))) diff --git a/src/main/logseq/cli/commands.cljs b/src/main/logseq/cli/commands.cljs index ed950d04ad..77bfbe8ed0 100644 --- a/src/main/logseq/cli/commands.cljs +++ b/src/main/logseq/cli/commands.cljs @@ -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) diff --git a/src/main/logseq/cli/format.cljs b/src/main/logseq/cli/format.cljs index e6a8e0c6ee..7c6bbd3405 100644 --- a/src/main/logseq/cli/format.cljs +++ b/src/main/logseq/cli/format.cljs @@ -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)) diff --git a/src/test/logseq/cli/commands_test.cljs b/src/test/logseq/cli/commands_test.cljs index b8b277e934..771105802e 100644 --- a/src/test/logseq/cli/commands_test.cljs +++ b/src/test/logseq/cli/commands_test.cljs @@ -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 diff --git a/src/test/logseq/cli/format_test.cljs b/src/test/logseq/cli/format_test.cljs index a48fac5693..1cac14a926 100644 --- a/src/test/logseq/cli/format_test.cljs +++ b/src/test/logseq/cli/format_test.cljs @@ -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)))))