use config class for storage setup (instead of prop list)

This commit is contained in:
Helmut Merz 2024-06-29 09:33:02 +02:00
parent c653247670
commit 5b41d12756
5 changed files with 24 additions and 43 deletions

View file

@ -4,7 +4,7 @@
(defpackage :scopes/config
(:use :common-lisp)
(:export #:base #:root #:*root*
(:export #:base #:root #:*root* #:*current*
#:env-data #:env-keys #:env-prefix #:env-path
#:actions #:add #:add-action #:children #:env-slots
#:name #:setup #:parent #:shutdown))

View file

@ -7,9 +7,9 @@
(:local-nicknames (:config :scopes/config)
(:core :scopes/core)
(:alx :alexandria))
(:export #:config
#:storage #:setup
#:params #:qualified-table-name
(:export #:config
#:storage #:params #:setup
#:qualified-table-name
#:do-sql #:query #:drop-table
#:normalize-keyword #:normalize-plist))
@ -18,13 +18,10 @@
;;;; config
(defclass config (config:base)
((config:env-slots :initform '(db-name db-user db-password))
(db-config :reader db-config :initarg :db-config) ;to be replaced
(backend)
(db-type)
(connect-args)))
;;;; db configurations
((backend :reader backend :initarg :backend :initform :dbi)
(db-type :reader db-type :initarg :db-type :initform :sqlite3)
(connect-args :reader connect-args :initarg :connect-args)
(options :reader options :initarg :options :initform nil)))
(defvar *db-params*
'(:sqlite3 (:id-type integer :json-type json :ts-sql identity)
@ -43,9 +40,8 @@
(schema :accessor schema)))
(defun setup (cfg)
(let* ((conf (db-config cfg))
(db-type (getf conf :db-type))
(conn-args (getf conf :connect-args))
(let* ((db-type (db-type cfg))
(conn-args (connect-args cfg))
(connect #'(lambda ()
(apply #'dbi:connect-cached db-type conn-args)))
(st (make-instance 'storage
@ -54,7 +50,7 @@
(ctx (make-instance 'context
:config cfg :name (config:name cfg) :storage st)))
(setf (conn st) (funcall connect))
(setf (schema st) (getf (getf conf :options) :schema))
(setf (schema st) (getf (options cfg) :schema))
ctx))
;;;; database (SQL) interface
@ -100,7 +96,5 @@
(push v res)
(push (normalize-keyword k) res))))
;;;; backend-/driver-specific stuff
(defun ts-string (ts)
(format nil "~a" (local-time:universal-to-timestamp ts)))

View file

@ -3,19 +3,12 @@
(in-package :scopes/test-storage)
(defparameter db-config-postgres
'(:backend :dbi
:db-type :postgres
:connect-args
(:database-name "cltest"
:host "localhost"
:port 5432
:username "testuser"
:password "secret")
:options
(:schema "testing")))
(defvar postgres-connect-args
'(:database-name "cltest" :host "localhost" :port 5432
:username "testuser" :password "secret"))
(config:root)
(config:root :env-keys '(db-name db-user db-password))
(config:add :storage :class 'storage:config :setup #'storage:setup
:db-config db-config-postgres)
:db-type :postgres
:connect-args postgres-connect-args)

View file

@ -3,15 +3,11 @@
(in-package :scopes/test-storage)
(defparameter db-config-sqlite
`(:backend :dbi
:db-type :sqlite3
:connect-args
(:database-name
,(namestring (scopes/testing:test-path "test.db" "data")))
:options nil))
(defvar sqlite-connect-args
(list :database-name (namestring (scopes/testing:test-path "test.db" "data"))))
(config:root)
(config:root :env-keys '(db-name))
(config:add :storage :class 'storage:config :setup #'storage:setup
:db-config db-config-sqlite)
:db-type :sqlite3
:connect-args sqlite-connect-args)

View file

@ -9,8 +9,7 @@
(:storage :scopes/storage)
(:tracking :scopes/storage/tracking)
(:t :scopes/testing))
(:export #:*db-config-test*
#:run #:run-all #:run-postgres #:run-sqlite)
(:export #:run #:run-all #:run-postgres #:run-sqlite)
(:import-from :scopes/testing #:deftest #:==))
(in-package :scopes/test-storage)
@ -31,8 +30,7 @@
(defun run ()
(core:setup-services)
(let* ((ctx (core:find-service :storage))
(cfg (core:config ctx)))
(let ((ctx (core:find-service :storage)))
(test-track ctx)
(t:show-result)))