Skip to content

Commit

Permalink
Fix database connection precedence (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
thierrymarianne authored Dec 1, 2018
1 parent eee8912 commit 39b6bc3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/command/unarchive_statuses.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
(defn unarchive-statuses
[week year]
(let [press-aggregate-name (:press (edn/read-string (:aggregate env)))
db-read-params {:models (get-entity-manager (:database-archive env))}
db-write-params {:models (get-entity-manager (:database env))}
db-read-params {:models (get-entity-manager (:database-archive env) :is-archive-connection)}
archived-status-model (:archived-status (:models db-read-params))
read-aggregate-model (:aggregate (:models db-read-params))
write-status-aggregate-model (:status-aggregate (:models db-write-params))
write-status-model (:status (:models db-write-params))
aggregate (first (find-aggregate-by-name press-aggregate-name read-aggregate-model))
{:keys [statuses-ids total-timely-statuses]} (get-timely-statuses-for-aggregate
press-aggregate-name
week
year
:are-archived)
db-write-params {:models (get-entity-manager (:database env))}
write-status-aggregate-model (:status-aggregate (:models db-write-params))
write-status-model (:status (:models db-write-params))
matching-archived-statuses (find-statuses-having-ids statuses-ids archived-status-model)
new-statuses (bulk-unarchive-statuses matching-archived-statuses write-status-model)
{total-new-relationships :total-new-relationships} (new-relationships
Expand Down
38 changes: 20 additions & 18 deletions src/repository/entity_manager.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
[utils.string]
[twitter.status-hash]))

(declare database-connection
(declare archive-database-connection database-connection
tokens
users members
subscriptions subscribees
Expand Down Expand Up @@ -143,27 +143,29 @@
member-subscribees)

(defn prepare-connection
[config]
(let [connection (defdb database-connection
{:classname "com.mysql.jdbc.Driver"
:subprotocol "mysql"
:subname (str "//" (:host config) ":" (:port config) "/" (:name config))
:useUnicode "yes"
:characterEncoding "UTF-8"
:characterSet "utf8mb4"
:collation "utf8mb4_unicode_ci"
:delimiters "`"
:useSSL false
:user (:user config)
:password (:password config)})]
[config & [is-archive-connection]]
(let [db-params {:classname "com.mysql.jdbc.Driver"
:subprotocol "mysql"
:subname (str "//" (:host config) ":" (:port config) "/" (:name config))
:useUnicode "yes"
:characterEncoding "UTF-8"
:characterSet "utf8mb4"
:collation "utf8mb4_unicode_ci"
:delimiters "`"
:useSSL false
:user (:user config)
:password (:password config)}
connection (if is-archive-connection
(defdb archive-database-connection db-params)
(defdb database-connection db-params))]
connection))

(defn connect-to-db
"Create a connection and provide with a map of entities"
; @see https://mathiasbynens.be/notes/mysql-utf8mb4
; @see https://clojurians-log.clojureverse.org/sql/2017-04-05
[config]
(let [connection (prepare-connection config)]
[config & [is-archive-connection]]
(let [connection (prepare-connection config is-archive-connection)]
{:aggregate (get-aggregate-model connection)
:archived-status (get-archived-status-model connection)
:highlight (get-highlight-model connection)
Expand All @@ -180,8 +182,8 @@
:users (get-user-model connection)}))

(defn get-entity-manager
[config]
(connect-to-db (edn/read-string config)))
[config & [is-archive-connection]]
(connect-to-db (edn/read-string config) is-archive-connection))

(defn find-distinct-ids-of-subscriptions
"Find distinct ids of subscription"
Expand Down

0 comments on commit 39b6bc3

Please sign in to comment.