Browse Source

enhance(dev): CLI import prints last tx

since most import failures are due to a failing tx
Gabriel Horner 10 months ago
parent
commit
2782eea315
1 changed files with 30 additions and 16 deletions
  1. 30 16
      deps/graph-parser/script/db_import.cljs

+ 30 - 16
deps/graph-parser/script/db_import.cljs

@@ -2,21 +2,28 @@
   "Imports given file(s) to a db graph. This script is primarily for
    developing the import feature and for engineers who want to customize
    the import process"
-  (:require [clojure.string :as string]
-            [datascript.core :as d]
-            ["path" :as node-path]
-            ["os" :as os]
-            ["fs" :as fs]
+  (:require ["fs" :as fs]
             ["fs/promises" :as fsp]
-            [nbb.core :as nbb]
-            [nbb.classpath :as cp]
+            ["os" :as os]
+            ["path" :as node-path]
             [babashka.cli :as cli]
-            [logseq.graph-parser.exporter :as gp-exporter]
+            [cljs.pprint :as pprint]
+            [clojure.set :as set]
+            [clojure.string :as string]
+            [datascript.core :as d]
             [logseq.common.graph :as common-graph]
+            [logseq.graph-parser.exporter :as gp-exporter]
             #_:clj-kondo/ignore
             [logseq.outliner.cli :as outliner-cli]
-            [promesa.core :as p]
-            [clojure.set :as set]))
+            [nbb.classpath :as cp]
+            [nbb.core :as nbb]
+            [promesa.core :as p]))
+
+(def last-tx-data (atom nil))
+(def original-transact! d/transact!)
+(defn dev-transact! [conn tx-data tx-meta]
+  (reset! last-tx-data tx-data)
+  (original-transact! conn tx-data tx-meta))
 
 (defn- build-graph-files
   "Given a file graph directory, return all files including assets and adds relative paths
@@ -41,7 +48,7 @@
           _ (fsp/mkdir parent-dir #js {:recursive true})]
     (fsp/copyFile (:path file) (node-path/join parent-dir (node-path/basename (:path file))))))
 
-(defn- notify-user [{:keys [continue]} m]
+(defn- notify-user [{:keys [continue debug]} m]
   (println (:msg m))
   (when (:ex-data m)
     (println "Ex-data:" (pr-str (dissoc (:ex-data m) :error)))
@@ -55,7 +62,10 @@
                        (when (:sci.impl/f-meta %)
                          (str " calls #'" (get-in % [:sci.impl/f-meta :ns]) "/" (get-in % [:sci.impl/f-meta :name]))))
                  (reverse stack))))
-      (println (some-> (get-in m [:ex-data :error]) .-stack))))
+      (println (some-> (get-in m [:ex-data :error]) .-stack)))
+    (when debug
+      (println "Last Tx Data:")
+      (pprint/pprint @last-tx-data)))
   (when (and (= :error (:level m)) (not continue))
     (js/process.exit 1)))
 
@@ -83,7 +93,8 @@
                         ;; asset file options
                        {:<copy-asset (fn copy-asset [file]
                                        (<copy-asset-file file db-graph-dir file-graph-dir))})]
-    (gp-exporter/export-file-graph conn conn config-file *files options)))
+    (p/with-redefs [d/transact! dev-transact!]
+      (gp-exporter/export-file-graph conn conn config-file *files options))))
 
 (defn- resolve-path
   "If relative path, resolve with $ORIGINAL_PWD"
@@ -98,8 +109,9 @@
   (let [doc-options (gp-exporter/build-doc-options {:macros {}} (merge options (default-export-options options)))
         files' (mapv #(hash-map :path %)
                      (into [file] (map resolve-path files)))]
-    (p/let [_ (gp-exporter/export-doc-files conn files' <read-file doc-options)]
-      {:import-state (:import-state doc-options)})))
+    (p/with-redefs [d/transact! dev-transact!]
+     (p/let [_ (gp-exporter/export-doc-files conn files' <read-file doc-options)]
+       {:import-state (:import-state doc-options)}))))
 
 (def spec
   "Options spec"
@@ -107,6 +119,8 @@
           :desc "Print help"}
    :verbose {:alias :v
              :desc "Verbose mode"}
+   :debug {:alias :d
+           :desc "Debug mode"}
    :continue {:alias :c
               :desc "Continue past import failures"}
    :all-tags {:alias :a
@@ -150,7 +164,7 @@
         _ (when (:verbose options) (prn :options user-options))
         options' (merge {:user-options user-options
                          :graph-name db-name}
-                        (select-keys options [:files :verbose :continue]))]
+                        (select-keys options [:files :verbose :continue :debug]))]
     (p/let [{:keys [import-state]}
             (if directory?
               (import-file-graph-to-db file-graph' (node-path/join dir db-name) conn options')