Migrates back to datomic on-prem

This commit is contained in:
2023-04-29 07:12:51 -07:00
parent aca8f7c92c
commit b2ad7790cf
88 changed files with 2289 additions and 2286 deletions

View File

@@ -6,8 +6,7 @@
[clj-time.core :as t]
[com.brunobonacci.mulog :as mu]
[com.unbounce.dogstatsd.core :as statsd]
[datomic.client.api :as dc]
[iol-ion.tx :refer [upsert-invoice upsert-ledger upsert-transaction]]
[datomic.api :as dc]
[manifold.deferred :as de]
[manifold.stream :as s]
[mount.core :as mount]
@@ -18,57 +17,57 @@
(t/plus (t/months -6))
(c/to-date))))
([start-date]
(let [txes-missing-ledger-entries (->> (dc/q {:query {:find ['?t ]
:in ['$ '?sd]
:where [
'[?t :transaction/date ?d]
'[(>= ?d ?sd)]
'(not [_ :journal-entry/original-entity ?t])
'(not [?t :transaction/amount 0.0])
'(not [?t :transaction/approval-status :transaction-approval-status/excluded])
'(not [?t :transaction/approval-status :transaction-approval-status/suppressed])
]}
:args [(dc/db conn) start-date]})
(let [txes-missing-ledger-entries (->> (dc/q {:find ['?t ]
:in ['$ '?sd]
:where [
'[?t :transaction/date ?d]
'[(>= ?d ?sd)]
'(not [_ :journal-entry/original-entity ?t])
'(not [?t :transaction/amount 0.0])
'(not [?t :transaction/approval-status :transaction-approval-status/excluded])
'(not [?t :transaction/approval-status :transaction-approval-status/suppressed])
]}
(dc/db conn) start-date)
(map first)
(mapv (fn [t]
`(upsert-transaction ~{:db/id t}))))
[:upsert-transaction {:db/id t}])))
invoices-missing-ledger-entries (->> (dc/q {:query {:find ['?t ]
:in ['$ '?sd]
:where ['[?t :invoice/date ?d]
'[(>= ?d ?sd)]
'(not [_ :journal-entry/original-entity ?t])
'[?t :invoice/total ?amt]
'[(not= 0.0 ?amt)]
'(not [?t :invoice/status :invoice-status/voided])
'(not [?t :invoice/import-status :import-status/pending])
'(not [?t :invoice/exclude-from-ledger true])
]}
:args [(dc/db conn) start-date]})
invoices-missing-ledger-entries (->> (dc/q {:find ['?t ]
:in ['$ '?sd]
:where ['[?t :invoice/date ?d]
'[(>= ?d ?sd)]
'(not [_ :journal-entry/original-entity ?t])
'[?t :invoice/total ?amt]
'[(not= 0.0 ?amt)]
'(not [?t :invoice/status :invoice-status/voided])
'(not [?t :invoice/import-status :import-status/pending])
'(not [?t :invoice/exclude-from-ledger true])
]}
(dc/db conn) start-date)
(map first)
(mapv (fn [i]
`(upsert-invoice ~{:db/id i}))))
[:upsert-invoice {:db/id i}])))
repairs (vec (concat txes-missing-ledger-entries invoices-missing-ledger-entries))]
(when (seq repairs)
(mu/log ::ledger-repairs-needed
:sample (take 3 repairs)
:transaction-count (count txes-missing-ledger-entries)
:invoice-count (count invoices-missing-ledger-entries))
(dc/transact conn {:tx-data (map (fn [repair]
`(upsert-ledger ~repair))
repairs) })))))
@(dc/transact conn (map (fn [repair]
[:upsert-ledger repair])
repairs))))))
(defn touch-transaction [e]
(dc/transact conn {:tx-data [{:db/id "datomic.tx"
:db/doc "touching transaction to update ledger"}
`(upsert-transaction ~{:db/id e})]}))
@(dc/transact conn [{:db/id "datomic.tx"
:db/doc "touching transaction to update ledger"}
[:upsert-transaction {:db/id e}]]))
(defn touch-invoice [e]
(dc/transact conn {:tx-data [{:db/id "datomic.tx"
:db/doc "touching invoice to update ledger"}
`(upsert-invoice ~{:db/id e})]}))
@(dc/transact conn [{:db/id "datomic.tx"
:db/doc "touching invoice to update ledger"}
[:upsert-invoice {:db/id e}]]))
@@ -297,19 +296,19 @@
(defn build-account-lookup [client-id]
(let [accounts (by :db/id (map first (dc/q {:query {:find ['(pull ?e [:db/id :account/name
:account/numeric-code
{:account/type [:db/ident]
:account/client-overrides [:account-client-override/client :account-client-override/name]}
])]
:in ['$]
:where ['[?e :account/name]]}
:args [(dc/db conn )]})))
(let [accounts (by :db/id (map first (dc/q {:find ['(pull ?e [:db/id :account/name
:account/numeric-code
{:account/type [:db/ident]
:account/client-overrides [:account-client-override/client :account-client-override/name]}
])]
:in ['$]
:where ['[?e :account/name]]}
(dc/db conn ))))
bank-accounts (by :db/id (map first (dc/q {:query {:find ['(pull ?e [:db/id :bank-account/name :bank-account/numeric-code {:bank-account/type [:db/ident]}])]
:in ['$]
:where ['[?e :bank-account/name]]}
:args [(dc/db conn)]})))
bank-accounts (by :db/id (map first (dc/q {:find ['(pull ?e [:db/id :bank-account/name :bank-account/numeric-code {:bank-account/type [:db/ident]}])]
:in ['$]
:where ['[?e :bank-account/name]]}
(dc/db conn))))
overrides-by-client (->> accounts
vals
(mapcat (fn [a]
@@ -344,10 +343,10 @@
:client-count (count clients)}
(mu/log ::reseting-index)
(let [so-far (atom 0)]
@(->> (dc/qseq '[:find (pull ?je [:journal-entry/date :journal-entry/client {:journal-entry/line-items [:journal-entry-line/account :journal-entry-line/location :db/id]}])
:in $ ?c
:where [?je :journal-entry/client ?c]]
(dc/db conn)
@(->> (dc/qseq {:query '[:find (pull ?je [:journal-entry/date :journal-entry/client {:journal-entry/line-items [:journal-entry-line/account :journal-entry-line/location :db/id]}])
:in $ ?c
:where [?je :journal-entry/client ?c]]
:args [(dc/db conn)]}
client)
(map first)
(mapcat (fn [je]
@@ -402,17 +401,16 @@
(defn accounts-needing-rebuild [ db client]
(let [client (pull-id db client)]
(->> (dc/qseq '[:find ?c ?a ?l (min ?d)
:in $ ?c
:where
[?jel :journal-entry-line/dirty true]
[?jel :journal-entry-line/account ?a]
[?jel :journal-entry-line/location ?l]
[?je :journal-entry/line-items ?jel]
[?je :journal-entry/client ?c]
[?je :journal-entry/date ?d]]
db
client)
(->> (dc/qseq {:query '[:find ?c ?a ?l (min ?d)
:in $ ?c
:where
[?jel :journal-entry-line/dirty true]
[?jel :journal-entry-line/account ?a]
[?jel :journal-entry-line/location ?l]
[?je :journal-entry/line-items ?jel]
[?je :journal-entry/client ?c]
[?je :journal-entry/date ?d]]
:args [db client]})
(map (fn [[client account location starting-at ]]
{:client client
:account account
@@ -421,16 +419,15 @@
(defn all-accounts-needing-rebuild [ db client]
(let [client (pull-id db client)]
(->> (dc/qseq '[:find ?c ?a ?l (min ?d)
:in $ ?c
:where
[?je :journal-entry/client ?c]
[?je :journal-entry/line-items ?jel]
[?jel :journal-entry-line/account ?a]
[?jel :journal-entry-line/location ?l]
[?je :journal-entry/date ?d]]
db
client)
(->> (dc/qseq {:query '[:find ?c ?a ?l (min ?d)
:in $ ?c
:where
[?je :journal-entry/client ?c]
[?je :journal-entry/line-items ?jel]
[?jel :journal-entry-line/account ?a]
[?jel :journal-entry-line/location ?l]
[?je :journal-entry/date ?d]]
:args [db client]})
(map (fn [[client account location starting-at ]]
{:client client
:account account