Adding migrations

This commit is contained in:
Bryce Covert
2019-01-22 17:56:19 -08:00
parent e6adf11fa1
commit fbd6311e95
3 changed files with 102 additions and 67 deletions

View File

@@ -47,6 +47,7 @@
[com.amazonaws/aws-java-sdk-s3 "1.11.282"] [com.amazonaws/aws-java-sdk-s3 "1.11.282"]
[org.clojure/data.json "0.2.6"] [org.clojure/data.json "0.2.6"]
[cider/cider-nrepl "0.16.0"] [cider/cider-nrepl "0.16.0"]
[io.rkn/conformity "0.5.1"]
[hiccup "1.0.5"]] [hiccup "1.0.5"]]
:plugins [[lein-ring "0.9.7"] :plugins [[lein-ring "0.9.7"]
[lein-cljsbuild "1.1.5"]] [lein-cljsbuild "1.1.5"]]
@@ -117,7 +118,7 @@
:main auto-ap.server :main auto-ap.server
:aot [auto-ap.server] :aot [auto-ap.server auto-ap.datomic.migrate]
:uberjar-name "auto-ap.jar" :uberjar-name "auto-ap.jar"

View File

@@ -34,23 +34,6 @@
result result
nil))) nil)))
(def functions
[{:db/ident :pay
:db/doc "Data function that increments value of attribute a by amount."
:db/fn #db/fn {:lang "clojure"
:params [db e amount]
:code (let [current-outstanding-balance (-> (d/entity db e) :invoice/outstanding-balance)
new-outstanding-balance (- current-outstanding-balance amount)]
[[:db/add e :invoice/outstanding-balance new-outstanding-balance]
[:db/add e :invoice/status (if (> new-outstanding-balance 0)
:invoice-status/unpaid
:invoice-status/paid)]])}}
{:db/ident :inc
:db/doc "Data function that increments value of attribute a by amount."
:db/fn #db/fn {:lang "clojure"
:params [db e a amount]
:code [[:db/add e a
(-> (d/entity db e) a (+ amount))]]}}] )
(def vendor-schema (def vendor-schema
[{:db/ident :vendor/original-id [{:db/ident :vendor/original-id
@@ -532,29 +515,18 @@
{:db/ident :user-role/none} {:db/ident :user-role/none}
]) ])
(defn create-schema [] (def base-schema
(->> [ address-schema contact-schema vendor-schema client-schema bank-account-schema invoice-schema invoice-expense-account-schema payment-schema invoice-payment-schema transaction-schema user-schema])
(concat address-schema contact-schema vendor-schema client-schema bank-account-schema invoice-schema invoice-expense-account-schema payment-schema invoice-payment-schema transaction-schema user-schema functions)
(d/transact (d/connect uri))))
(defn load-entities []
(d/transact (d/connect uri)
[{:db/ident :test
:testing/number "1"}
{:db/ident :test-2
:testing/number "2"}]
))
(defn query-entities [] (defn query-entities []
(d/q '[:find (pull ?e [:vendor/name]) (d/q '[:find (pull ?e [:vendor/name])
:where [?e :vendor/name]] :where [?e :vendor/name]]
(d/db (d/connect uri)))) (d/db (d/connect uri))))
(defn load-vendors [vendors] (defn load-vendors [vendors]
(d/transact (->> vendors
(d/connect uri)
(->> vendors
(map (map
(fn [{:keys [primary-phone address email primary-contact secondary-email secondary-contact primary-email name default-expense-account id code secondary-phone invoice-reminder-schedule print-as]}] (fn [{:keys [primary-phone address email primary-contact secondary-email secondary-contact primary-email name default-expense-account id code secondary-phone invoice-reminder-schedule print-as]}]
(let [vendor-id (d/tempid :db.part/user) (let [vendor-id (d/tempid :db.part/user)
@@ -575,7 +547,7 @@
:email primary-email}) :email primary-email})
:secondary-contact (remove-nils #:contact {:name secondary-contact :secondary-contact (remove-nils #:contact {:name secondary-contact
:phone secondary-phone :phone secondary-phone
:email secondary-email})}))))))) :email secondary-email})}))))))
(defn load-clients [clients] (defn load-clients [clients]
(->> clients (->> clients
@@ -609,15 +581,14 @@
bank-accounts) bank-accounts)
#:bank-account {:original-id (str client-id "-" 0) #:bank-account {:original-id (str client-id "-" 0)
:external-id 0 :external-id 0
:type :bank-account-type/cash})} ))) :type :bank-account-type/cash})} )))))
(d/transact (d/connect uri))))
(defn load-invoices [invoices] (defn load-invoices [invoices]
(->> invoices (->> invoices
(map (map
(fn [{:keys [id status total outstanding-balance invoice-number date customer-identifier company-id vendor-id default-location default-expense-account] invoice-id :id}] (fn [{:keys [id status total outstanding-balance invoice-number date customer-identifier company-id vendor-id default-location default-expense-account] invoice-id :id}]
(remove-nils #:invoice {:original-id id [(remove-nils #:invoice {:original-id id
:invoice-number invoice-number :invoice-number invoice-number
:date (coerce/to-date date) :date (coerce/to-date date)
:customer-identifier customer-identifier :customer-identifier customer-identifier
@@ -628,26 +599,24 @@
:default-expense-account default-expense-account :default-expense-account default-expense-account
:total (double total) :total (double total)
:outstanding-balance (double outstanding-balance) :outstanding-balance (double outstanding-balance)
:status (keyword "invoice-status" status)}))) :status (keyword "invoice-status" status)})]))))
(d/transact (d/connect uri))))
(defn load-invoices-expense-accounts [invoices-expense-accounts] (defn load-invoices-expense-accounts [invoices-expense-accounts]
(->> invoices-expense-accounts (->> invoices-expense-accounts
(map (map
(fn [{:keys [id expense-account-id location amount invoice-id]}] (fn [{:keys [id expense-account-id location amount invoice-id]}]
(remove-nils #:invoice {:original-id invoice-id [(remove-nils #:invoice {:original-id invoice-id
:expense-accounts [#:invoice-expense-account {:original-id id :expense-accounts [#:invoice-expense-account {:original-id id
:expense-account-id expense-account-id :expense-account-id expense-account-id
:location location :location location
:amount (double amount)}]}))) :amount (double amount)}]})]))))
(d/transact (d/connect uri))))
(defn load-payments [checks] (defn load-payments [checks]
(->> checks (->> checks
(map (map
(fn [{:keys [id s3-uuid s3-key s3-url vendor-id company-id check-number memo date amount paid-to data bank-account-id status type] invoice-id :id}] (fn [{:keys [id s3-uuid s3-key s3-url vendor-id company-id check-number memo date amount paid-to data bank-account-id status type] invoice-id :id}]
(remove-nils #:payment {:original-id id [(remove-nils #:payment {:original-id id
:s3-uuid s3-uuid :s3-uuid s3-uuid
:s3-key s3-key :s3-key s3-key
:s3-url s3-url :s3-url s3-url
@@ -663,18 +632,16 @@
:status (keyword "payment-status" status) :status (keyword "payment-status" status)
:type (if type :type (if type
(keyword "payment-type" type) (keyword "payment-type" type)
:payment-type/check)}))) :payment-type/check)})]))))
(d/transact (d/connect uri))))
(defn load-invoices-payments [invoices-checks] (defn load-invoices-payments [invoices-checks]
(->> invoices-checks (->> invoices-checks
(map (map
(fn [{:keys [id invoice-id check-id amount]}] (fn [{:keys [id invoice-id check-id amount]}]
(remove-nils #:invoice-payment {:original-id id [(remove-nils #:invoice-payment {:original-id id
:payment [:payment/original-id check-id] :payment [:payment/original-id check-id]
:invoice [:invoice/original-id invoice-id] :invoice [:invoice/original-id invoice-id]
:amount (double amount)}))) :amount (double amount)})]))))
(d/transact (d/connect uri))))
(defn load-transactions [transactions] (defn load-transactions [transactions]
(->> transactions (->> transactions
@@ -682,7 +649,7 @@
(fn [{:keys [id amount description-original description-simple merchant-id merchant-name (fn [{:keys [id amount description-original description-simple merchant-id merchant-name
date post-date type account-id status vendor-id company-id check-id check-number date post-date type account-id status vendor-id company-id check-id check-number
bank-account-id]}] bank-account-id]}]
(remove-nils #:transaction {:original-id id [(remove-nils #:transaction {:original-id id
:description-original description-original :description-original description-original
:description-simple description-simple :description-simple description-simple
:merchant-id merchant-id :merchant-id merchant-id
@@ -698,9 +665,7 @@
:client (when company-id [:client/original-id company-id]) :client (when company-id [:client/original-id company-id])
:payment (when check-id [:payment/original-id check-id]) :payment (when check-id [:payment/original-id check-id])
:bank-account (when bank-account-id :bank-account (when bank-account-id
[:bank-account/original-id (str company-id "-" bank-account-id)])}))) [:bank-account/original-id (str company-id "-" bank-account-id)])})]))))
(d/transact (d/connect uri)))
)
(defn load-users [users] (defn load-users [users]
(->> users (->> users
@@ -712,7 +677,7 @@
:provider-id provider-id :provider-id provider-id
:provider provider :provider provider
:clients (map (fn [c] [:client/original-id c]) companies)}))) :clients (map (fn [c] [:client/original-id c]) companies)})))
(d/transact (d/connect uri)))) ))
(defn query-vendors [] (defn query-vendors []
(d/q '[:find (pull ?e [*]) (d/q '[:find (pull ?e [*])
@@ -749,21 +714,34 @@
:where [?e :user/original-id]] :where [?e :user/original-id]]
(d/db (d/connect uri)))) (d/db (d/connect uri))))
(defn migrate-vendors [conn]
[(let [all-vendors (v/get-all)]
(load-vendors all-vendors))])
(defn do-it [] (defn migrate-clients [conn]
(create-database )
@(create-schema )
(let [all-vendors (v/get-all)]
@(load-vendors all-vendors))
(let [all-clients (c/get-all)] (let [all-clients (c/get-all)]
@(load-clients all-clients)) [(load-clients all-clients)]))
@(load-invoices (i/get-all))
@(load-payments (checks/get-all)) (defn migrate-invoices [conn]
@(load-invoices-payments (ic/get-all)) (load-invoices (i/get-all)))
@(load-invoices-expense-accounts (iea/get-all))
@(load-transactions (transactions/get-all)) (defn migrate-payments [conn]
@(load-users (users/get-all)) (load-payments (checks/get-all)))
(count (clojure.pprint/pprint (query-payments)))) (defn migrate-invoices-payments [conn]
(load-invoices-payments (ic/get-all)))
(defn migrate-invoices-expense-accounts [conn]
(load-invoices-expense-accounts (iea/get-all)))
(defn migrate-transactions [conn]
(let [trans (load-transactions (transactions/get-all))]
(if (seq trans)
trans
[[]])))
(defn migrate-users [conn]
[(load-users (users/get-all))])

View File

@@ -0,0 +1,56 @@
(ns auto-ap.datomic.migrate
(:require [auto-ap.datomic :refer [uri]]
[datomic.api :as d]
[clojure.java.io :as io]
[io.rkn.conformity :as c])
(:import [datomic Util])
(:gen-class))
(defn read-dtm
"Reads a dtm file (i.e., an edn file with datomic tags in it) from the classpath
and returns a vector of all forms contained within."
[filename]
(-> (io/resource filename) (io/reader) (Util/readAll)))
(defn functions [conn]
[[{:db/ident :pay
:db/doc "Data function that increments value of attribute a by amount."
:db/fn (d/function '{:lang "clojure"
:params [db e amount]
:code (let [current-outstanding-balance (-> (d/entity db e) :invoice/outstanding-balance)
new-outstanding-balance (- current-outstanding-balance amount)]
[[:db/add e :invoice/outstanding-balance new-outstanding-balance]
[:db/add e :invoice/status (if (> new-outstanding-balance 0)
:invoice-status/unpaid
:invoice-status/paid)]])})}
{:db/ident :inc
:db/doc "Data function that increments value of attribute a by amount."
:db/fn (d/function '{:lang "clojure"
:params [db e a amount]
:code [[:db/add e a
(-> (d/entity db e) a (+ amount))]] })}]] )
(defn -main [& args]
(println "Creating database...")
#_(d/delete-database uri)
(d/create-database uri)
(let [
conn (d/connect uri)
norms-map {:auto-ap/base-schema {:txes auto-ap.datomic/base-schema}
:auto-ap/functions {:txes-fn 'auto-ap.datomic.migrate/functions :requires [:auto-ap/base-schema]}
:auto-ap/migrate-vendors {:txes-fn 'auto-ap.datomic/migrate-vendors :requires [:auto-ap/base-schema]}
:auto-ap/migrate-clients {:txes-fn 'auto-ap.datomic/migrate-clients :requires [:auto-ap/migrate-vendors]}
:auto-ap/migrate-users {:txes-fn 'auto-ap.datomic/migrate-users :requires [:auto-ap/migrate-clients]}
:auto-ap/migrate-invoices {:txes-fn 'auto-ap.datomic/migrate-invoices :requires [:auto-ap/migrate-vendors :auto-ap/migrate-clients]}
:auto-ap/migrate-payments {:txes-fn 'auto-ap.datomic/migrate-payments :requires [:auto-ap/migrate-invoices]}
:auto-ap/migrate-invoices-payments {:txes-fn 'auto-ap.datomic/migrate-invoices-payments :requires [:auto-ap/migrate-payments :auto-ap/migrate-invoices]}
:auto-ap/migrate-invoices-expense-accounts {:txes-fn 'auto-ap.datomic/migrate-invoices-expense-accounts :requires [:auto-ap/migrate-invoices-payments]}
:auto-ap/migrate-transactions {:txes-fn 'auto-ap.datomic/migrate-transactions :requires [:auto-ap/migrate-invoices-expense-accounts]}
}]
(println "Conforming database...")
(println (c/ensure-conforms conn norms-map))
(d/release conn)
(println "Done")))
#_(-main)