diff --git a/project.clj b/project.clj index ed88e828..950a9e6f 100644 --- a/project.clj +++ b/project.clj @@ -47,6 +47,7 @@ [com.amazonaws/aws-java-sdk-s3 "1.11.282"] [org.clojure/data.json "0.2.6"] [cider/cider-nrepl "0.16.0"] + [io.rkn/conformity "0.5.1"] [hiccup "1.0.5"]] :plugins [[lein-ring "0.9.7"] [lein-cljsbuild "1.1.5"]] @@ -117,7 +118,7 @@ :main auto-ap.server - :aot [auto-ap.server] + :aot [auto-ap.server auto-ap.datomic.migrate] :uberjar-name "auto-ap.jar" diff --git a/src/clj/auto_ap/datomic.clj b/src/clj/auto_ap/datomic.clj index f63d8e1d..17fd3571 100644 --- a/src/clj/auto_ap/datomic.clj +++ b/src/clj/auto_ap/datomic.clj @@ -34,23 +34,6 @@ result 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 [{:db/ident :vendor/original-id @@ -532,29 +515,18 @@ {:db/ident :user-role/none} ]) -(defn create-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)))) +(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]) + -(defn load-entities [] - (d/transact (d/connect uri) - [{:db/ident :test - :testing/number "1"} - {:db/ident :test-2 - :testing/number "2"}] - )) (defn query-entities [] (d/q '[:find (pull ?e [:vendor/name]) :where [?e :vendor/name]] (d/db (d/connect uri)))) (defn load-vendors [vendors] - (d/transact - (d/connect uri) - (->> vendors + (->> vendors (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]}] (let [vendor-id (d/tempid :db.part/user) @@ -575,7 +547,7 @@ :email primary-email}) :secondary-contact (remove-nils #:contact {:name secondary-contact :phone secondary-phone - :email secondary-email})}))))))) + :email secondary-email})})))))) (defn load-clients [clients] (->> clients @@ -609,15 +581,14 @@ bank-accounts) #:bank-account {:original-id (str client-id "-" 0) :external-id 0 - :type :bank-account-type/cash})} ))) - (d/transact (d/connect uri)))) + :type :bank-account-type/cash})} ))))) (defn load-invoices [invoices] (->> invoices (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}] - (remove-nils #:invoice {:original-id id + [(remove-nils #:invoice {:original-id id :invoice-number invoice-number :date (coerce/to-date date) :customer-identifier customer-identifier @@ -628,26 +599,24 @@ :default-expense-account default-expense-account :total (double total) :outstanding-balance (double outstanding-balance) - :status (keyword "invoice-status" status)}))) - (d/transact (d/connect uri)))) + :status (keyword "invoice-status" status)})])))) (defn load-invoices-expense-accounts [invoices-expense-accounts] (->> invoices-expense-accounts (map (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-account-id expense-account-id :location location - :amount (double amount)}]}))) - (d/transact (d/connect uri)))) + :amount (double amount)}]})])))) (defn load-payments [checks] (->> checks (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}] - (remove-nils #:payment {:original-id id + [(remove-nils #:payment {:original-id id :s3-uuid s3-uuid :s3-key s3-key :s3-url s3-url @@ -663,18 +632,16 @@ :status (keyword "payment-status" status) :type (if type (keyword "payment-type" type) - :payment-type/check)}))) - (d/transact (d/connect uri)))) + :payment-type/check)})])))) (defn load-invoices-payments [invoices-checks] (->> invoices-checks (map (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] :invoice [:invoice/original-id invoice-id] - :amount (double amount)}))) - (d/transact (d/connect uri)))) + :amount (double amount)})])))) (defn load-transactions [transactions] (->> transactions @@ -682,7 +649,7 @@ (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 bank-account-id]}] - (remove-nils #:transaction {:original-id id + [(remove-nils #:transaction {:original-id id :description-original description-original :description-simple description-simple :merchant-id merchant-id @@ -698,9 +665,7 @@ :client (when company-id [:client/original-id company-id]) :payment (when check-id [:payment/original-id check-id]) :bank-account (when bank-account-id - [:bank-account/original-id (str company-id "-" bank-account-id)])}))) - (d/transact (d/connect uri))) - ) + [:bank-account/original-id (str company-id "-" bank-account-id)])})])))) (defn load-users [users] (->> users @@ -712,7 +677,7 @@ :provider-id provider-id :provider provider :clients (map (fn [c] [:client/original-id c]) companies)}))) - (d/transact (d/connect uri)))) + )) (defn query-vendors [] (d/q '[:find (pull ?e [*]) @@ -749,21 +714,34 @@ :where [?e :user/original-id]] (d/db (d/connect uri)))) +(defn migrate-vendors [conn] + [(let [all-vendors (v/get-all)] + (load-vendors all-vendors))]) -(defn do-it [] - (create-database ) - @(create-schema ) - (let [all-vendors (v/get-all)] - @(load-vendors all-vendors)) +(defn migrate-clients [conn] (let [all-clients (c/get-all)] - @(load-clients all-clients)) - @(load-invoices (i/get-all)) - @(load-payments (checks/get-all)) - @(load-invoices-payments (ic/get-all)) - @(load-invoices-expense-accounts (iea/get-all)) - @(load-transactions (transactions/get-all)) - @(load-users (users/get-all)) - - (count (clojure.pprint/pprint (query-payments)))) + [(load-clients all-clients)])) + +(defn migrate-invoices [conn] + (load-invoices (i/get-all))) + +(defn migrate-payments [conn] + (load-payments (checks/get-all))) + +(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))]) + diff --git a/src/clj/auto_ap/datomic/migrate.clj b/src/clj/auto_ap/datomic/migrate.clj new file mode 100644 index 00000000..3e4706d8 --- /dev/null +++ b/src/clj/auto_ap/datomic/migrate.clj @@ -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)