Remove dependency on test code from places importer.
This commit is contained in:
parent
63b304ea5f
commit
a68c281066
1 changed files with 47 additions and 31 deletions
|
@ -8,67 +8,83 @@
|
||||||
[datomish.pair-chan :refer [go-pair <?]]
|
[datomish.pair-chan :refer [go-pair <?]]
|
||||||
[cljs.core.async.macros :refer [go]]))
|
[cljs.core.async.macros :refer [go]]))
|
||||||
(:require
|
(:require
|
||||||
|
[datomish.db :as db]
|
||||||
|
[datomish.transact :as transact]
|
||||||
[datomish.util :as util #?(:cljs :refer-macros :clj :refer) [raise raise-str cond-let]]
|
[datomish.util :as util #?(:cljs :refer-macros :clj :refer) [raise raise-str cond-let]]
|
||||||
[datomish.sqlite :as s]
|
[datomish.sqlite :as s]
|
||||||
[datomish.api :as d]
|
|
||||||
#?@(:clj [[datomish.pair-chan :refer [go-pair <?]]
|
#?@(:clj [[datomish.pair-chan :refer [go-pair <?]]
|
||||||
[clojure.core.async :as a :refer [chan go <! >!]]])
|
[clojure.core.async :as a :refer [chan go <! >!]]])
|
||||||
#?@(:cljs [[datomish.pair-chan]
|
#?@(:cljs [[datomish.pair-chan]
|
||||||
[cljs.core.async :as a :refer [chan <! >!]]])))
|
[cljs.core.async :as a :refer [chan <! >!]]])))
|
||||||
|
|
||||||
(def places-schema-fragment
|
(def places-schema-fragment
|
||||||
[{:db/id (d/id-literal :db.part/user)
|
[{:db/id (db/id-literal :db.part/user)
|
||||||
:db/ident :page/url
|
:db/ident :page/url
|
||||||
:db/unique :db.unique/identity
|
:db/unique :db.unique/identity
|
||||||
:db/valueType :db.type/string ;; TODO: uri
|
:db/valueType :db.type/string ;; TODO: uri
|
||||||
:db.install/_attribute :db.part/db}
|
:db.install/_attribute :db.part/db}
|
||||||
{:db/id (d/id-literal :db.part/user)
|
{:db/id (db/id-literal :db.part/user)
|
||||||
:db/ident :page/guid
|
:db/ident :page/guid
|
||||||
:db/unique :db.unique/identity
|
:db/unique :db.unique/identity
|
||||||
:db/valueType :db.type/string ;; TODO: uuid or guid?
|
:db/valueType :db.type/string ;; TODO: uuid or guid?
|
||||||
:db.install/_attribute :db.part/db}
|
:db.install/_attribute :db.part/db}
|
||||||
{:db/id (d/id-literal :db.part/user)
|
{:db/id (db/id-literal :db.part/user)
|
||||||
:db/ident :page/title
|
:db/ident :page/title
|
||||||
:db/cardinality :db.cardinality/one
|
:db/cardinality :db.cardinality/one
|
||||||
:db/valueType :db.type/string
|
:db/valueType :db.type/string
|
||||||
:db.install/_attribute :db.part/db}
|
:db.install/_attribute :db.part/db}
|
||||||
{:db/id (d/id-literal :db.part/user)
|
{:db/id (db/id-literal :db.part/user)
|
||||||
:db/ident :page/visitAt
|
:db/ident :page/visitAt
|
||||||
:db/cardinality :db.cardinality/many
|
:db/cardinality :db.cardinality/many
|
||||||
:db/valueType :db.type/long ;; TODO: instant
|
:db/valueType :db.type/long ;; TODO: instant
|
||||||
:db.install/_attribute :db.part/db}
|
:db.install/_attribute :db.part/db}
|
||||||
])
|
])
|
||||||
|
|
||||||
|
(defn assoc-if
|
||||||
|
([m k v]
|
||||||
|
(if v
|
||||||
|
(assoc m k v)
|
||||||
|
m))
|
||||||
|
([m k v & kvs]
|
||||||
|
(if kvs
|
||||||
|
(let [[kk vv & remainder] kvs]
|
||||||
|
(apply assoc-if
|
||||||
|
(assoc-if m k v)
|
||||||
|
kk vv remainder))
|
||||||
|
(assoc-if m k v))))
|
||||||
|
|
||||||
|
|
||||||
(defn- place->entity [[id rows]]
|
(defn- place->entity [[id rows]]
|
||||||
(let [title (:title (first (filter :page/title rows)))]
|
(let [title (:title (first (filter :page/title rows)))
|
||||||
(cond-> {:db/id (d/id-literal :db.part/user)
|
required {:db/id (db/id-literal :db.part/user)
|
||||||
:page/url (:url (first rows))
|
:page/url (:url (first rows))
|
||||||
:page/guid (:guid (first rows))
|
:page/guid (:guid (first rows))}
|
||||||
:page/visitAt (map :visit_date rows)}
|
visits (map :visit_date rows)]
|
||||||
title (assoc :page/title title))))
|
(assoc-if required
|
||||||
|
:page/title title
|
||||||
|
:page/visitAt visits)))
|
||||||
|
|
||||||
(defn import-places [conn places-connection]
|
(defn import-places [conn places-connection]
|
||||||
(go-pair
|
(go-pair
|
||||||
;; Ensure schema fragment is in place, even though it may cost a (mostly empty) transaction.
|
;; Ensure schema fragment is in place, even though it may cost a (mostly empty) transaction.
|
||||||
(<? (d/<transact! conn places-schema-fragment))
|
(<? (transact/<transact! conn places-schema-fragment))
|
||||||
|
|
||||||
(->>
|
(let [rows
|
||||||
["SELECT DISTINCT p.id, p.url, p.title, p.visit_count, p.last_visit_date, p.guid,"
|
(<?
|
||||||
"hv.visit_date"
|
(s/all-rows
|
||||||
"FROM moz_places AS p LEFT JOIN moz_historyvisits AS hv"
|
places-connection
|
||||||
"WHERE p.hidden = 0 AND p.id = hv.place_id"
|
["SELECT DISTINCT p.id AS id, p.url AS url, p.title AS title, p.visit_count, p.last_visit_date, p.guid,
|
||||||
"ORDER BY p.id, hv.visit_date"
|
hv.visit_date
|
||||||
"LIMIT 20000"] ;; TODO: remove limit.
|
FROM moz_places AS p LEFT JOIN moz_historyvisits AS hv
|
||||||
(interpose " ")
|
WHERE p.hidden = 0 AND p.id = hv.place_id
|
||||||
(apply str)
|
ORDER BY p.id, hv.visit_date"]))]
|
||||||
(vector)
|
(<?
|
||||||
|
(transact/<transact!
|
||||||
|
conn
|
||||||
|
(map place->entity (group-by :id rows)))))))
|
||||||
|
|
||||||
(s/all-rows places-connection)
|
(defn import-places-from-path [db places]
|
||||||
(<?)
|
(go-pair
|
||||||
|
(let [conn (transact/connection-with-db db)
|
||||||
(group-by :id)
|
pdb (<? (s/<sqlite-connection places))]
|
||||||
|
(import-places conn pdb))))
|
||||||
(map place->entity)
|
|
||||||
|
|
||||||
(d/<transact! conn)
|
|
||||||
(<?))))
|
|
||||||
|
|
Loading…
Reference in a new issue