Skip to content

Commit

Permalink
Test project is working after db selection for Postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
kno3comma14 committed Aug 19, 2023
1 parent 56176ef commit a97b69e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 50 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "automatic"
}
1 change: 1 addition & 0 deletions src/xiana/db/client/mysql.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(ns xiana.db.client.mysql)
46 changes: 16 additions & 30 deletions src/xiana/db/client/postgres.clj
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,13 @@
(defn get-datasource
([config]
(get-datasource config 0))
([config count]
([config count]
(let [create-datasource (or (:xiana/create-custom-datasource config)
(get-pool-datasource config)
jdbc/get-datasource)
jdbc-opts (merge default-opts
(:xiana/jdbc-opts config))]
(try (-> config
:xiana/postgresql
(:xiana/jdbc-opts config))]
(try (-> config
create-datasource
(jdbc/with-options jdbc-opts))
(catch Exception e (if (< count 10)
Expand Down Expand Up @@ -121,7 +120,7 @@
([config]
(migrate! config 0))
([config count]
(try
(try
(migr/migrate (migr/get-db-config config))
(catch Exception e (if (< count 10)
(migrate! config (inc count))
Expand All @@ -131,11 +130,11 @@
(defn connect
"Adds `:datasource` key to the `:xiana/postgresql` config section
and duplicates `:xiana/postgresql` under the top-level `:db` key."
[{pg-config :xiana/postgresql :as config}]
(let [pg-config (assoc-in pg-config [:config :datasource] (get-datasource config))]
[{pg-config :xiana/postgresql :as config}]
(let [new-pg-config (assoc-in pg-config [:config :datasource] (get-datasource (:config pg-config)))]
(assoc config
:xiana/postgresql pg-config
:db pg-config)))
:xiana/postgresql (:config new-pg-config)
:db (:config new-pg-config))))

(defn ->sql-params
"Parse sql-map using honeysql format function with pre-defined
Expand Down Expand Up @@ -177,14 +176,14 @@
(<-db-object [_this obj]
(<-pgobject obj))

(define-container [this]
(docker-postgres! (:config this)))
(define-container [_this]
(docker-postgres! config))

(define-migration [this]
(migrate! (:config this)))
(define-migration [_this]
(migrate! config))

(define-migration [this count]
(migrate! (:config this) count))
(define-migration [_this count]
(migrate! config count))

(connect [_this]
(let [new-config {:xiana/postgresql config
Expand All @@ -210,9 +209,6 @@
(when-let [emb (embedded this)]
(.close emb))))

(defn create-postgres-DB [config jdbc-opts]
(->PostgresDB config jdbc-opts nil))

(def db-access
"Database access interceptor, works from `:query` and from `db-queries` keys
Enter: nil.
Expand All @@ -224,7 +220,7 @@
(fn [{query-or-fn :query
db-queries :db-queries
:as state}]
(let [datasource (get-in state [:deps :db :config :datasource])
(let [datasource (get-in state [:deps :db :datasource])
query (cond
(fn? query-or-fn) (query-or-fn state)
:else query-or-fn)
Expand All @@ -237,14 +233,4 @@
(fn [state]
(merge state
{:response {:status 500
:body (pr-str (:error state))}}))})


;; For config-map

;; dbtype
;; classname
;; port
;; dbname
;; user
;; datasource
:body (pr-str (:error state))}}))})
19 changes: 9 additions & 10 deletions src/xiana/db/migrate.clj
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,11 @@

(defn get-db-config
[config]
(let [db-selected (:xiana/db-selected config)
db-config (db-selected config)
db (if-let [ds (get-in db-config [:config :datasource])]
(let [db-selected (:xiana/selected-db config)
db-config (db-selected config)
db (if-let [ds (:datasource db-config)]
{:datasource (jdbc/get-datasource ds)}
db-config)]
(log/debug "config:" config)
db-config)]
(assoc (:xiana/migration config) :db db)))

(defn migrate
Expand All @@ -112,27 +111,27 @@
(defn migrate-action
[options]
(log/info "Migrate database")
(log/debug "options:" options)
;(log/debug "options:" options)
(let [cfg (get-db-config (load-config options))]
(log/debug "config:" cfg)
;(log/debug "config:" cfg)
(if-let [id (:id options)]
(migrate cfg id)
(migrate cfg))))

(defn rollback-action
[options]
(log/info "Rollback database")
(log/debug "options:" options)
;(log/debug "options:" options)
(let [cfg (get-db-config (load-config options))]
(log/debug "config:" cfg)
;(log/debug "config:" cfg)
(if-let [id (:id options)]
(rollback cfg id)
(rollback cfg))))

(defn create-script-action
[{:keys [dir name] :as options}]

Check warning on line 132 in src/xiana/db/migrate.clj

View workflow job for this annotation

GitHub Actions / clj-kondo check

src/xiana/db/migrate.clj#L132

[unused-binding] unused binding options
(log/info "Create migration scripts skeleton")
(log/debug "options:" options)
;(log/debug "options:" options)
(create-script dir name))

(defn run
Expand Down
15 changes: 9 additions & 6 deletions src/xiana/session.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
(:require
[honeysql.format :as sqlf]
[next.jdbc.result-set :refer [as-kebab-maps]]
[xiana.db-provisional :as dbp])
[xiana.db-provisional :as dbp]
[taoensso.timbre :as log])
(:import
(java.util
UUID)))
Expand Down Expand Up @@ -37,15 +38,17 @@
(every? (into {} record) [:port :dbname :host :dbtype :user :password]))

(defn connect
[{backend-config :xiana/session-backend :as cfg}]
(let [selected-db (:xiana/selected-db backend-config)
[{backend-config :xiana/session-backend :as cfg}]
(let [selected-db (:xiana/selected-db cfg)
db-cfg (selected-db cfg)
db-record-type (selected-db dbp/dbms-map)
db-record-instance (db-record-type backend-config {:builder-fn as-kebab-maps})
db-record-instance (db-record-type db-cfg {:builder-fn as-kebab-maps} nil)
_ (log/info db-record-instance)
connection (cond (validate-config-data db-record-instance) db-record-instance
(get-in cfg [:db :config :datasource]) (db-record-type (:config cfg)
(get-in cfg [:db :config :datasource]) (db-record-type db-cfg
{:builder-fn as-kebab-maps}
nil)
:else (db-record-type (merge (selected-db cfg) backend-config)
:else (db-record-type (merge db-cfg backend-config)
{:builder-fn as-kebab-maps}
nil))]
connection))
Expand Down
8 changes: 4 additions & 4 deletions test/xiana_fixture.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[piotr-yuxuan.closeable-map :refer [closeable-map]]
[xiana.commons :refer [rename-key]]
[xiana.config :as config]
[xiana.db :as db-core]
;[xiana.db :as db-core]
[xiana.rbac :as rbac]
[xiana.route :as routes]
[xiana.session :as session-backend]
Expand All @@ -15,9 +15,9 @@
(-> (config/config)
(merge app-cfg)
(rename-key :xiana/auth :auth)
db-core/docker-postgres!
db-core/connect
db-core/migrate!
;db-core/docker-postgres!
;db-core/connect
;db-core/migrate!
session-backend/init-backend
routes/reset
rbac/init
Expand Down

0 comments on commit a97b69e

Please sign in to comment.