Просмотр исходного кода

fix: fail-invalid? option on in-app db validation

alert can't be called in a worker
Gabriel Horner 2 лет назад
Родитель
Сommit
66f57104ab
2 измененных файлов с 16 добавлено и 11 удалено
  1. 11 10
      deps/db/src/logseq/db/frontend/validate.cljs
  2. 5 1
      src/main/frontend/worker/pipeline.cljs

+ 11 - 10
deps/db/src/logseq/db/frontend/validate.cljs

@@ -1,5 +1,5 @@
 (ns logseq.db.frontend.validate
-  "Validate db"
+  "Validate frontend db"
   (:require [datascript.core :as d]
             [logseq.db.frontend.malli-schema :as db-malli-schema]
             [malli.util :as mu]
@@ -8,9 +8,9 @@
 
 (defn validate-db!
   "Validates the entities that have changed in the given datascript tx-report.
-   Validation is only for DB graphs"
+   Validation is only for DB graphs. Returns boolean indicating if db is valid"
   [{:keys [db-after tx-data tx-meta]} validate-options]
-  (let [{:keys [closed-schema? fail-invalid?]} validate-options
+  (let [{:keys [closed-schema?]} validate-options
         changed-ids (->> tx-data (map :e) distinct)
         ent-maps* (->> changed-ids (mapcat #(d/datoms db-after :eavt %)) db-malli-schema/datoms->entity-maps vals)
         ent-maps (vec (db-malli-schema/update-properties-in-ents ent-maps*))
@@ -20,10 +20,11 @@
                     closed-schema?
                     mu/closed-schema)]
     (js/console.log "changed eids:" changed-ids tx-meta)
-    (when-let [errors (->> ent-maps
-                           (m/explain db-schema)
-                           :errors)]
-      (js/console.error "Invalid datascript entities detected amongst changed entity ids:" changed-ids)
-      (pprint/pprint {:errors errors})
-      (pprint/pprint {:entity-maps ent-maps})
-      (when fail-invalid? (js/alert "Invalid DB!")))))
+    (if-let [errors (->> ent-maps
+                         (m/explain db-schema)
+                         :errors)]
+      (do (js/console.error "Invalid datascript entities detected amongst changed entity ids:" changed-ids)
+          (pprint/pprint {:errors errors})
+          (pprint/pprint {:entity-maps ent-maps})
+          false)
+      true)))

+ 5 - 1
src/main/frontend/worker/pipeline.cljs

@@ -5,6 +5,7 @@
             [logseq.outliner.pipeline :as outliner-pipeline]
             [frontend.worker.react :as worker-react]
             [frontend.worker.file :as file]
+            [frontend.worker.util :as worker-util]
             [logseq.db.frontend.validate :as validate]
             [logseq.db.sqlite.util :as sqlite-util]
             [frontend.worker.db.fix :as db-fix]
@@ -62,7 +63,10 @@
 (defn validate-and-fix-db!
   [repo conn tx-report context]
   (when (and (:dev? context) (sqlite-util/db-based-graph? repo))
-    (validate/validate-db! tx-report (:validate-db-options context)))
+    (let [valid? (validate/validate-db! tx-report (:validate-db-options context))]
+      (when (and (get-in context [:validate-db-options :fail-invalid?]) (not valid?))
+        (worker-util/post-message :notification
+                                  (pr-str [["Invalid DB!"] :error])))))
   (when (and (:dev? context)
              (not (:node-test? context)))
     (fix-db! conn tx-report)))