Follow-up: don't accept schema when creating DB.
This schema was already ignored.
This commit is contained in:
parent
52af06ce28
commit
73b155cfdc
2 changed files with 69 additions and 72 deletions
|
@ -446,63 +446,60 @@
|
||||||
(declare <with-internal)
|
(declare <with-internal)
|
||||||
|
|
||||||
(defn <db-with-sqlite-connection
|
(defn <db-with-sqlite-connection
|
||||||
([sqlite-connection]
|
[sqlite-connection]
|
||||||
(<db-with-sqlite-connection sqlite-connection {}))
|
(go-pair
|
||||||
|
(when-not (= sqlite-schema/current-version (<? (sqlite-schema/<ensure-current-version sqlite-connection)))
|
||||||
|
(raise "Could not ensure current SQLite schema version."))
|
||||||
|
|
||||||
([sqlite-connection schema]
|
(let [current-tx (<? (<current-tx sqlite-connection))
|
||||||
(go-pair
|
bootstrapped (>= current-tx 0)
|
||||||
(when-not (= sqlite-schema/current-version (<? (sqlite-schema/<ensure-current-version sqlite-connection)))
|
current-tx (max current-tx tx0)]
|
||||||
(raise "Could not ensure current SQLite schema version."))
|
(when-not bootstrapped
|
||||||
|
;; We need to bootstrap the DB.
|
||||||
|
(let [fail-alter-ident (fn [old new] (if-not (= old new)
|
||||||
|
(raise "Altering idents is not yet supported, got " new " altering existing ident " old
|
||||||
|
{:error :schema/alter-idents :old old :new new})
|
||||||
|
new))
|
||||||
|
fail-alter-attr (fn [old new] (if-not (= old new)
|
||||||
|
(raise "Altering schema attributes is not yet supported, got " new " altering existing schema attribute " old
|
||||||
|
{:error :schema/alter-schema :old old :new new})
|
||||||
|
new))]
|
||||||
|
(-> (map->DB
|
||||||
|
{:sqlite-connection sqlite-connection
|
||||||
|
:idents bootstrap-idents
|
||||||
|
:symbolic-schema bootstrap-symbolic-schema
|
||||||
|
:schema (ds/schema (into {} (map (fn [[k v]] [(k bootstrap-idents) v]) bootstrap-symbolic-schema))) ;; TODO: fail if ident missing.
|
||||||
|
:current-tx current-tx})
|
||||||
|
;; We use <with rather than <transact! to apply the bootstrap transaction data but to
|
||||||
|
;; not follow the regular schema application process. We can't apply the schema
|
||||||
|
;; changes, since the applied datoms would conflict with the bootstrapping idents and
|
||||||
|
;; schema. (The bootstrapping idents and schema are required to be able to write to
|
||||||
|
;; the database conveniently; without them, we'd have to manually write datoms to the
|
||||||
|
;; store. It's feasible but awkward.) After bootstrapping, we read back the idents
|
||||||
|
;; and schema, just like when we re-open.
|
||||||
|
(<with-internal (bootstrap-tx-data) fail-alter-ident fail-alter-attr)
|
||||||
|
(<?))))
|
||||||
|
|
||||||
(let [current-tx (<? (<current-tx sqlite-connection))
|
;; We just bootstrapped, or we are returning to an already bootstrapped DB.
|
||||||
bootstrapped (>= current-tx 0)
|
(let [idents (<? (<idents sqlite-connection))
|
||||||
current-tx (max current-tx tx0)]
|
symbolic-schema (<? (<symbolic-schema sqlite-connection))]
|
||||||
(when-not bootstrapped
|
(when-not bootstrapped
|
||||||
;; We need to bootstrap the DB.
|
(when (not (= idents bootstrap-idents))
|
||||||
(let [fail-alter-ident (fn [old new] (if-not (= old new)
|
(raise "After bootstrapping database, expected new materialized idents and old bootstrapped idents to be identical"
|
||||||
(raise "Altering idents is not yet supported, got " new " altering existing ident " old
|
{:error :bootstrap/bad-idents,
|
||||||
{:error :schema/alter-idents :old old :new new})
|
:new idents :old bootstrap-idents
|
||||||
new))
|
}))
|
||||||
fail-alter-attr (fn [old new] (if-not (= old new)
|
(when (not (= symbolic-schema bootstrap-symbolic-schema))
|
||||||
(raise "Altering schema attributes is not yet supported, got " new " altering existing schema attribute " old
|
(raise "After bootstrapping database, expected new materialized symbolic schema and old bootstrapped symbolic schema to be identical"
|
||||||
{:error :schema/alter-schema :old old :new new})
|
{:error :bootstrap/bad-symbolic-schema,
|
||||||
new))]
|
:new symbolic-schema :old bootstrap-symbolic-schema
|
||||||
(-> (map->DB
|
})))
|
||||||
{:sqlite-connection sqlite-connection
|
(map->DB
|
||||||
:idents bootstrap-idents
|
{:sqlite-connection sqlite-connection
|
||||||
:symbolic-schema bootstrap-symbolic-schema
|
:idents idents
|
||||||
:schema (ds/schema (into {} (map (fn [[k v]] [(k bootstrap-idents) v]) bootstrap-symbolic-schema))) ;; TODO: fail if ident missing.
|
:symbolic-schema symbolic-schema
|
||||||
:current-tx current-tx})
|
:schema (ds/schema (into {} (map (fn [[k v]] [(k idents) v]) symbolic-schema))) ;; TODO: fail if ident missing.
|
||||||
;; We use <with rather than <transact! to apply the bootstrap transaction data but to
|
:current-tx (inc current-tx)})))))
|
||||||
;; not follow the regular schema application process. We can't apply the schema
|
|
||||||
;; changes, since the applied datoms would conflict with the bootstrapping idents and
|
|
||||||
;; schema. (The bootstrapping idents and schema are required to be able to write to
|
|
||||||
;; the database conveniently; without them, we'd have to manually write datoms to the
|
|
||||||
;; store. It's feasible but awkward.) After bootstrapping, we read back the idents
|
|
||||||
;; and schema, just like when we re-open.
|
|
||||||
(<with-internal (bootstrap-tx-data) fail-alter-ident fail-alter-attr)
|
|
||||||
(<?))))
|
|
||||||
|
|
||||||
;; We just bootstrapped, or we are returning to an already bootstrapped DB.
|
|
||||||
(let [idents (<? (<idents sqlite-connection))
|
|
||||||
symbolic-schema (<? (<symbolic-schema sqlite-connection))]
|
|
||||||
(when-not bootstrapped
|
|
||||||
(when (not (= idents bootstrap-idents))
|
|
||||||
(raise "After bootstrapping database, expected new materialized idents and old bootstrapped idents to be identical"
|
|
||||||
{:error :bootstrap/bad-idents,
|
|
||||||
:new idents :old bootstrap-idents
|
|
||||||
}))
|
|
||||||
(when (not (= symbolic-schema bootstrap-symbolic-schema))
|
|
||||||
(raise "After bootstrapping database, expected new materialized symbolic schema and old bootstrapped symbolic schema to be identical"
|
|
||||||
{:error :bootstrap/bad-symbolic-schema,
|
|
||||||
:new symbolic-schema :old bootstrap-symbolic-schema
|
|
||||||
})))
|
|
||||||
(map->DB
|
|
||||||
{:sqlite-connection sqlite-connection
|
|
||||||
:idents idents
|
|
||||||
:symbolic-schema symbolic-schema
|
|
||||||
:schema (ds/schema (into {} (map (fn [[k v]] [(k idents) v]) symbolic-schema))) ;; TODO: fail if ident missing.
|
|
||||||
:current-tx (inc current-tx)}))))))
|
|
||||||
|
|
||||||
(defn connection-with-db [db]
|
(defn connection-with-db [db]
|
||||||
(map->Connection {:current-db (atom db)}))
|
(map->Connection {:current-db (atom db)}))
|
||||||
|
|
|
@ -123,7 +123,7 @@
|
||||||
(deftest-async test-add-one
|
(deftest-async test-add-one
|
||||||
(with-tempfile [t (tempfile)]
|
(with-tempfile [t (tempfile)]
|
||||||
(let [c (<? (s/<sqlite-connection t))
|
(let [c (<? (s/<sqlite-connection t))
|
||||||
db (<? (dm/<db-with-sqlite-connection c test-schema))
|
db (<? (dm/<db-with-sqlite-connection c))
|
||||||
conn (dm/connection-with-db db)]
|
conn (dm/connection-with-db db)]
|
||||||
(try
|
(try
|
||||||
(let [{tx0 :tx} (<? (dm/<transact! conn test-schema))]
|
(let [{tx0 :tx} (<? (dm/<transact! conn test-schema))]
|
||||||
|
@ -139,7 +139,7 @@
|
||||||
(deftest-async test-add-two
|
(deftest-async test-add-two
|
||||||
(with-tempfile [t (tempfile)]
|
(with-tempfile [t (tempfile)]
|
||||||
(let [c (<? (s/<sqlite-connection t))
|
(let [c (<? (s/<sqlite-connection t))
|
||||||
db (<? (dm/<db-with-sqlite-connection c test-schema))
|
db (<? (dm/<db-with-sqlite-connection c))
|
||||||
conn (dm/connection-with-db db)]
|
conn (dm/connection-with-db db)]
|
||||||
(try
|
(try
|
||||||
(let [{tx0 :tx} (<? (dm/<transact! conn test-schema))
|
(let [{tx0 :tx} (<? (dm/<transact! conn test-schema))
|
||||||
|
@ -169,7 +169,7 @@
|
||||||
(deftest-async test-retract
|
(deftest-async test-retract
|
||||||
(with-tempfile [t (tempfile)]
|
(with-tempfile [t (tempfile)]
|
||||||
(let [c (<? (s/<sqlite-connection t))
|
(let [c (<? (s/<sqlite-connection t))
|
||||||
db (<? (dm/<db-with-sqlite-connection c test-schema))
|
db (<? (dm/<db-with-sqlite-connection c))
|
||||||
conn (dm/connection-with-db db)]
|
conn (dm/connection-with-db db)]
|
||||||
(try
|
(try
|
||||||
(let [{tx0 :tx} (<? (dm/<transact! conn test-schema))
|
(let [{tx0 :tx} (<? (dm/<transact! conn test-schema))
|
||||||
|
@ -188,7 +188,7 @@
|
||||||
(deftest-async test-id-literal-1
|
(deftest-async test-id-literal-1
|
||||||
(with-tempfile [t (tempfile)]
|
(with-tempfile [t (tempfile)]
|
||||||
(let [c (<? (s/<sqlite-connection t))
|
(let [c (<? (s/<sqlite-connection t))
|
||||||
db (<? (dm/<db-with-sqlite-connection c test-schema))
|
db (<? (dm/<db-with-sqlite-connection c))
|
||||||
conn (dm/connection-with-db db)]
|
conn (dm/connection-with-db db)]
|
||||||
(try
|
(try
|
||||||
(let [tx0 (:tx (<? (dm/<transact! conn test-schema)))
|
(let [tx0 (:tx (<? (dm/<transact! conn test-schema)))
|
||||||
|
@ -214,7 +214,7 @@
|
||||||
(deftest-async test-unique
|
(deftest-async test-unique
|
||||||
(with-tempfile [t (tempfile)]
|
(with-tempfile [t (tempfile)]
|
||||||
(let [c (<? (s/<sqlite-connection t))
|
(let [c (<? (s/<sqlite-connection t))
|
||||||
db (<? (dm/<db-with-sqlite-connection c test-schema))
|
db (<? (dm/<db-with-sqlite-connection c))
|
||||||
conn (dm/connection-with-db db)]
|
conn (dm/connection-with-db db)]
|
||||||
(try
|
(try
|
||||||
(let [tx0 (:tx (<? (dm/<transact! conn test-schema)))]
|
(let [tx0 (:tx (<? (dm/<transact! conn test-schema)))]
|
||||||
|
@ -236,7 +236,7 @@
|
||||||
(deftest-async test-valueType-keyword
|
(deftest-async test-valueType-keyword
|
||||||
(with-tempfile [t (tempfile)]
|
(with-tempfile [t (tempfile)]
|
||||||
(let [c (<? (s/<sqlite-connection t))
|
(let [c (<? (s/<sqlite-connection t))
|
||||||
db (<? (dm/<db-with-sqlite-connection c test-schema))
|
db (<? (dm/<db-with-sqlite-connection c))
|
||||||
conn (dm/connection-with-db db)]
|
conn (dm/connection-with-db db)]
|
||||||
(try
|
(try
|
||||||
(let [tx0 (:tx (<? (dm/<transact! conn [{:db/id (dm/id-literal :db.part/user -1)
|
(let [tx0 (:tx (<? (dm/<transact! conn [{:db/id (dm/id-literal :db.part/user -1)
|
||||||
|
@ -271,7 +271,7 @@
|
||||||
(deftest-async test-vector-upsert
|
(deftest-async test-vector-upsert
|
||||||
(with-tempfile [t (tempfile)]
|
(with-tempfile [t (tempfile)]
|
||||||
(let [c (<? (s/<sqlite-connection t))
|
(let [c (<? (s/<sqlite-connection t))
|
||||||
db (<? (dm/<db-with-sqlite-connection c test-schema))
|
db (<? (dm/<db-with-sqlite-connection c))
|
||||||
conn (dm/connection-with-db db)
|
conn (dm/connection-with-db db)
|
||||||
tempids (fn [tx] (into {} (map (juxt (comp :idx first) second) (:tempids tx))))]
|
tempids (fn [tx] (into {} (map (juxt (comp :idx first) second) (:tempids tx))))]
|
||||||
(try
|
(try
|
||||||
|
@ -309,7 +309,7 @@
|
||||||
(deftest-async test-map-upsert
|
(deftest-async test-map-upsert
|
||||||
(with-tempfile [t (tempfile)]
|
(with-tempfile [t (tempfile)]
|
||||||
(let [c (<? (s/<sqlite-connection t))
|
(let [c (<? (s/<sqlite-connection t))
|
||||||
db (<? (dm/<db-with-sqlite-connection c test-schema))
|
db (<? (dm/<db-with-sqlite-connection c))
|
||||||
conn (dm/connection-with-db db)
|
conn (dm/connection-with-db db)
|
||||||
tempids (fn [tx] (into {} (map (juxt (comp :idx first) second) (:tempids tx))))]
|
tempids (fn [tx] (into {} (map (juxt (comp :idx first) second) (:tempids tx))))]
|
||||||
(try
|
(try
|
||||||
|
@ -363,7 +363,7 @@
|
||||||
(deftest-async test-map-upsert-conflicts
|
(deftest-async test-map-upsert-conflicts
|
||||||
(with-tempfile [t (tempfile)]
|
(with-tempfile [t (tempfile)]
|
||||||
(let [c (<? (s/<sqlite-connection t))
|
(let [c (<? (s/<sqlite-connection t))
|
||||||
db (<? (dm/<db-with-sqlite-connection c test-schema))
|
db (<? (dm/<db-with-sqlite-connection c))
|
||||||
conn (dm/connection-with-db db)
|
conn (dm/connection-with-db db)
|
||||||
tempids (fn [tx] (into {} (map (juxt (comp :idx first) second) (:tempids tx))))]
|
tempids (fn [tx] (into {} (map (juxt (comp :idx first) second) (:tempids tx))))]
|
||||||
(try
|
(try
|
||||||
|
@ -401,7 +401,7 @@
|
||||||
(deftest-async test-add-ident
|
(deftest-async test-add-ident
|
||||||
(with-tempfile [t (tempfile)]
|
(with-tempfile [t (tempfile)]
|
||||||
(let [c (<? (s/<sqlite-connection t))
|
(let [c (<? (s/<sqlite-connection t))
|
||||||
db (<? (dm/<db-with-sqlite-connection c test-schema))
|
db (<? (dm/<db-with-sqlite-connection c))
|
||||||
conn (dm/connection-with-db db)]
|
conn (dm/connection-with-db db)]
|
||||||
(try
|
(try
|
||||||
(let [report (<? (dm/<transact! conn [[:db/add (dm/id-literal :db.part/db -1) :db/ident :test/ident]]))
|
(let [report (<? (dm/<transact! conn [[:db/add (dm/id-literal :db.part/db -1) :db/ident :test/ident]]))
|
||||||
|
@ -429,7 +429,7 @@
|
||||||
(deftest-async test-add-schema
|
(deftest-async test-add-schema
|
||||||
(with-tempfile [t (tempfile)]
|
(with-tempfile [t (tempfile)]
|
||||||
(let [c (<? (s/<sqlite-connection t))
|
(let [c (<? (s/<sqlite-connection t))
|
||||||
db (<? (dm/<db-with-sqlite-connection c test-schema))
|
db (<? (dm/<db-with-sqlite-connection c))
|
||||||
conn (dm/connection-with-db db)]
|
conn (dm/connection-with-db db)]
|
||||||
(try
|
(try
|
||||||
(let [es [[:db/add :db.part/db :db.install/attribute (dm/id-literal :db.part/db -1)]
|
(let [es [[:db/add :db.part/db :db.install/attribute (dm/id-literal :db.part/db -1)]
|
||||||
|
@ -461,7 +461,7 @@
|
||||||
(deftest-async test-fulltext
|
(deftest-async test-fulltext
|
||||||
(with-tempfile [t (tempfile)]
|
(with-tempfile [t (tempfile)]
|
||||||
(let [c (<? (s/<sqlite-connection t))
|
(let [c (<? (s/<sqlite-connection t))
|
||||||
db (<? (dm/<db-with-sqlite-connection c test-schema))
|
db (<? (dm/<db-with-sqlite-connection c))
|
||||||
conn (dm/connection-with-db db)
|
conn (dm/connection-with-db db)
|
||||||
schema [{:db/id (dm/id-literal :db.part/db -1)
|
schema [{:db/id (dm/id-literal :db.part/db -1)
|
||||||
:db/ident :test/fulltext
|
:db/ident :test/fulltext
|
||||||
|
@ -535,7 +535,7 @@
|
||||||
(deftest-async test-txInstant
|
(deftest-async test-txInstant
|
||||||
(with-tempfile [t (tempfile)]
|
(with-tempfile [t (tempfile)]
|
||||||
(let [c (<? (s/<sqlite-connection t))
|
(let [c (<? (s/<sqlite-connection t))
|
||||||
db (<? (dm/<db-with-sqlite-connection c test-schema))
|
db (<? (dm/<db-with-sqlite-connection c))
|
||||||
conn (dm/connection-with-db db)
|
conn (dm/connection-with-db db)
|
||||||
{tx0 :tx} (<? (dm/<transact! conn test-schema))]
|
{tx0 :tx} (<? (dm/<transact! conn test-schema))]
|
||||||
(try
|
(try
|
||||||
|
@ -571,7 +571,7 @@
|
||||||
(deftest-async test-no-tx
|
(deftest-async test-no-tx
|
||||||
(with-tempfile [t (tempfile)]
|
(with-tempfile [t (tempfile)]
|
||||||
(let [c (<? (s/<sqlite-connection t))
|
(let [c (<? (s/<sqlite-connection t))
|
||||||
db (<? (dm/<db-with-sqlite-connection c test-schema))
|
db (<? (dm/<db-with-sqlite-connection c))
|
||||||
conn (dm/connection-with-db db)
|
conn (dm/connection-with-db db)
|
||||||
{tx0 :tx} (<? (dm/<transact! conn test-schema))]
|
{tx0 :tx} (<? (dm/<transact! conn test-schema))]
|
||||||
(try
|
(try
|
||||||
|
@ -586,7 +586,7 @@
|
||||||
(deftest-async test-explode-sequences
|
(deftest-async test-explode-sequences
|
||||||
(with-tempfile [t (tempfile)]
|
(with-tempfile [t (tempfile)]
|
||||||
(let [c (<? (s/<sqlite-connection t))
|
(let [c (<? (s/<sqlite-connection t))
|
||||||
db (<? (dm/<db-with-sqlite-connection c test-schema))
|
db (<? (dm/<db-with-sqlite-connection c))
|
||||||
conn (dm/connection-with-db db)
|
conn (dm/connection-with-db db)
|
||||||
{tx0 :tx} (<? (dm/<transact! conn test-schema))]
|
{tx0 :tx} (<? (dm/<transact! conn test-schema))]
|
||||||
(try
|
(try
|
||||||
|
@ -615,7 +615,7 @@
|
||||||
(deftest-async test-explode-maps
|
(deftest-async test-explode-maps
|
||||||
(with-tempfile [t (tempfile)]
|
(with-tempfile [t (tempfile)]
|
||||||
(let [c (<? (s/<sqlite-connection t))
|
(let [c (<? (s/<sqlite-connection t))
|
||||||
db (<? (dm/<db-with-sqlite-connection c test-schema))
|
db (<? (dm/<db-with-sqlite-connection c))
|
||||||
conn (dm/connection-with-db db)
|
conn (dm/connection-with-db db)
|
||||||
{tx0 :tx} (<? (dm/<transact! conn test-schema))]
|
{tx0 :tx} (<? (dm/<transact! conn test-schema))]
|
||||||
(try
|
(try
|
||||||
|
@ -650,7 +650,7 @@
|
||||||
(deftest-async test-explode-reverse-refs
|
(deftest-async test-explode-reverse-refs
|
||||||
(with-tempfile [t (tempfile)]
|
(with-tempfile [t (tempfile)]
|
||||||
(let [c (<? (s/<sqlite-connection t))
|
(let [c (<? (s/<sqlite-connection t))
|
||||||
db (<? (dm/<db-with-sqlite-connection c test-schema))
|
db (<? (dm/<db-with-sqlite-connection c))
|
||||||
conn (dm/connection-with-db db)
|
conn (dm/connection-with-db db)
|
||||||
{tx0 :tx} (<? (dm/<transact! conn test-schema))]
|
{tx0 :tx} (<? (dm/<transact! conn test-schema))]
|
||||||
(try
|
(try
|
||||||
|
|
Loading…
Reference in a new issue