Skip to content

Commit

Permalink
handle invalid as-of time points (#576)
Browse files Browse the repository at this point in the history
  • Loading branch information
kordano authored Nov 4, 2022
1 parent 9d96044 commit d4adcba
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/datahike/api.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns datahike.api
(:refer-clojure :exclude [filter])
(:require [datahike.connector :as dc]
[datahike.constants :as const]
[datahike.core :as dcore]
[datahike.pull-api :as dp]
[datahike.query :as dq]
Expand Down Expand Up @@ -659,7 +660,13 @@
(fn [db time-point]
{:pre [(or (int? time-point) (date? time-point))]}
(if (dbi/-temporal-index? db)
(AsOfDB. db time-point)
(if (int? time-point)
(if (<= const/tx0 time-point)
(AsOfDB. db time-point)
(throw (ex-info
(format "Invalid transaction ID. Must be bigger than %d." const/tx0)
{:time-point time-point})))
(AsOfDB. db time-point))
(throw (ex-info "as-of is only allowed on temporal indexed databases." {:config (dbi/-config db)})))))

(def ^{:arglists '([db])
Expand Down
1 change: 0 additions & 1 deletion test/datahike/test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
datahike.test.query-or-test
datahike.test.query-pull-test
datahike.test.query-rules-test
datahike.test.query-v3-test
datahike.test.schema-test
datahike.test.store-test
datahike.test.time-variance-test
Expand Down
7 changes: 7 additions & 0 deletions test/datahike/test/time_variance_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,10 @@
as-of-db (d/as-of @conn current-tx)]
(is (= {:aka ["Devil" "Tupen"]}
(d/pull as-of-db [:aka] michal))))))))

;; https://github.com/replikativ/datahike/issues/572
(deftest as-of-should-fail-on-invalid-time-points
(let [cfg (assoc-in cfg-template [:store :id] "as-of-invalid-time-points")
conn (setup-db cfg)]
(is (thrown-msg? "Invalid transaction ID. Must be bigger than 536870912."
(d/as-of @conn 42)))))

0 comments on commit d4adcba

Please sign in to comment.