From a97b69e17a21ceefcec192d0f6ef01288f18d27c Mon Sep 17 00:00:00 2001 From: kno3comma14 Date: Fri, 18 Aug 2023 21:51:29 -0500 Subject: [PATCH] Test project is working after db selection for Postgres --- .vscode/settings.json | 3 +++ src/xiana/db/client/mysql.clj | 1 + src/xiana/db/client/postgres.clj | 46 +++++++++++--------------------- src/xiana/db/migrate.clj | 19 +++++++------ src/xiana/session.clj | 15 ++++++----- test/xiana_fixture.clj | 8 +++--- 6 files changed, 42 insertions(+), 50 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 src/xiana/db/client/mysql.clj diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..e0f15db2 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.configuration.updateBuildConfiguration": "automatic" +} \ No newline at end of file diff --git a/src/xiana/db/client/mysql.clj b/src/xiana/db/client/mysql.clj new file mode 100644 index 00000000..cb3b2930 --- /dev/null +++ b/src/xiana/db/client/mysql.clj @@ -0,0 +1 @@ +(ns xiana.db.client.mysql) \ No newline at end of file diff --git a/src/xiana/db/client/postgres.clj b/src/xiana/db/client/postgres.clj index aa09223a..e78735f4 100644 --- a/src/xiana/db/client/postgres.clj +++ b/src/xiana/db/client/postgres.clj @@ -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) @@ -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)) @@ -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 @@ -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 @@ -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. @@ -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) @@ -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))}}))}) \ No newline at end of file diff --git a/src/xiana/db/migrate.clj b/src/xiana/db/migrate.clj index afd10cca..75e9c801 100644 --- a/src/xiana/db/migrate.clj +++ b/src/xiana/db/migrate.clj @@ -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 @@ -112,9 +111,9 @@ (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)))) @@ -122,9 +121,9 @@ (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)))) @@ -132,7 +131,7 @@ (defn create-script-action [{:keys [dir name] :as options}] (log/info "Create migration scripts skeleton") - (log/debug "options:" options) + ;(log/debug "options:" options) (create-script dir name)) (defn run diff --git a/src/xiana/session.clj b/src/xiana/session.clj index 3a051b8f..57b4e85f 100644 --- a/src/xiana/session.clj +++ b/src/xiana/session.clj @@ -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))) @@ -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)) diff --git a/test/xiana_fixture.clj b/test/xiana_fixture.clj index 7871a079..70a46d69 100644 --- a/test/xiana_fixture.clj +++ b/test/xiana_fixture.clj @@ -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] @@ -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