Skip to content

Commit

Permalink
fix schema update
Browse files Browse the repository at this point in the history
  • Loading branch information
huahaiy committed Jul 21, 2020
1 parent e82466d commit 71bebbf
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Change Log
All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/).

## WIP
## Fixed
- update existing schema instead of creating new ones

## 0.2.5
## Fixed
- Reset transaction after getting entries
Expand Down
2 changes: 1 addition & 1 deletion bench/bench.clj
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@

(def default-versions
[;["latest" "datomic"]
["0.18.13" "datascript"]
;["0.18.13" "datascript"]
;["latest" "datascript"]
["latest" "datalevin"]])

Expand Down
28 changes: 20 additions & 8 deletions src/datalevin/storage.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,36 @@
(lmdb/transact lmdb (for [[attr props] schema]
[:put c/schema attr props :attr :data])))

(defn- load-schema
[lmdb]
(into {} (lmdb/get-range lmdb c/schema [:all] :attr :data)))

(defn- init-max-aid
[lmdb]
(lmdb/entries lmdb c/schema))

(defn- assign-aid
[lmdb schema]
(let [^long init-aid (init-max-aid lmdb)]
;; TODO schema migration
(defn- update-schema
[lmdb old schema]
(let [^long init-aid (init-max-aid lmdb)
i (atom 0)]
(into {}
(map-indexed (fn [^long i [attr props]]
[attr (assoc props :db/aid (+ init-aid i))]))
(map (fn [[attr props]]
(if-let [old-props (old attr)]
[attr (merge old-props props)]
(let [res [attr (assoc props :db/aid (+ init-aid @i))]]
(swap! i inc)
res))))
schema)))

(defn- init-schema
[lmdb schema]
(when-not (seq (lmdb/get-range lmdb c/schema [:all] :attr :data))
(when (empty? (load-schema lmdb))
(transact-schema lmdb c/implicit-schema))
(when schema (transact-schema lmdb (assign-aid lmdb schema)))
(into {} (lmdb/get-range lmdb c/schema [:all] :attr :data)))
(when schema
(let [now (load-schema lmdb)]
(transact-schema lmdb (update-schema lmdb now schema))))
(load-schema lmdb))

(defn- init-max-gt
[lmdb]
Expand Down
11 changes: 11 additions & 0 deletions test/datalevin/storage_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,14 @@
store (sut/open {d p3} dir)]
(is (= s3 (sut/schema store))))
))

(deftest schema-test
(let [s {:a {:db/valueType :db.type/string}
:b {:db/valueType :db.type/long}}
dir (str "/tmp/datalevin-schema-test-" (UUID/randomUUID))
store (sut/open s dir)
s1 (sut/schema store)]
(sut/close store)
(let [store (sut/open s dir)]
(is (= s1 (sut/schema store))))
))
10 changes: 10 additions & 0 deletions test/datalevin/test/transact.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,13 @@
(d/datom 2 :a2 2)
(d/datom 2 :a3 3)]
(:tx-data report)))))

(deftest test-transact-same
"same data, transacted twice"
(let [es [{:db/id -1 :company "IBM" :country "US"}
{:db/id -2 :company "PwC" :country "Germany"}]
db (d/db-with (d/empty-db) es)
dts1 (d/datoms db :eavt)
db (d/db-with (d/empty-db) es)
dts2 (d/datoms db :eavt)]
(is (= dts1 dts2))))

0 comments on commit 71bebbf

Please sign in to comment.