storage: use config mechanism for providing database settings
This commit is contained in:
parent
df9ee430af
commit
0aa8ecb732
5 changed files with 40 additions and 16 deletions
|
@ -21,3 +21,9 @@
|
||||||
:components ((:file "test/test-storage"))
|
:components ((:file "test/test-storage"))
|
||||||
:perform (test-op (o c)
|
:perform (test-op (o c)
|
||||||
(symbol-call :scopes/test-storage :run-all)))
|
(symbol-call :scopes/test-storage :run-all)))
|
||||||
|
|
||||||
|
(defsystem :scopes/test-sqlite
|
||||||
|
:depends-on (:scopes)
|
||||||
|
:components ((:file "test/test-storage"))
|
||||||
|
:perform (test-op (o c)
|
||||||
|
(symbol-call :scopes/test-storage :run-sqlite)))
|
||||||
|
|
|
@ -4,8 +4,10 @@
|
||||||
|
|
||||||
(defpackage :scopes/storage
|
(defpackage :scopes/storage
|
||||||
(:use :common-lisp)
|
(:use :common-lisp)
|
||||||
(:local-nicknames (:alx :alexandria))
|
(:local-nicknames (:config :scopes/config)
|
||||||
(:export #:*db-config*
|
(:alx :alexandria))
|
||||||
|
(:export #:config
|
||||||
|
#:*db-config*
|
||||||
#:make-engine #:make-storage #:engine
|
#:make-engine #:make-storage #:engine
|
||||||
#:timestamp-to-sql
|
#:timestamp-to-sql
|
||||||
#:db-options #:db-params #:qualified-table-name
|
#:db-options #:db-params #:qualified-table-name
|
||||||
|
@ -15,7 +17,13 @@
|
||||||
|
|
||||||
(in-package :scopes/storage)
|
(in-package :scopes/storage)
|
||||||
|
|
||||||
(defparameter *db-config* nil)
|
;;;; config
|
||||||
|
|
||||||
|
(defclass config (config:base)
|
||||||
|
((config:env-slots :initform '(db-name db-user db-password))
|
||||||
|
(db-config :reader db-config :initarg :db-config)))
|
||||||
|
|
||||||
|
;;;; db configurations
|
||||||
|
|
||||||
(defvar *backends*
|
(defvar *backends*
|
||||||
'(:dbi dbi-make-engine))
|
'(:dbi dbi-make-engine))
|
||||||
|
@ -32,9 +40,10 @@
|
||||||
(params :initarg :params)
|
(params :initarg :params)
|
||||||
(config :reader config :initarg :config)))
|
(config :reader config :initarg :config)))
|
||||||
|
|
||||||
(defun make-engine ()
|
(defun make-engine (cfg)
|
||||||
(let ((backend (getf *db-config* :backend)))
|
(let* ((dbconf (db-config cfg))
|
||||||
(funcall (getf *backends* backend) *db-config*)))
|
(backend (getf dbconf :backend)))
|
||||||
|
(funcall (getf *backends* backend) dbconf)))
|
||||||
|
|
||||||
(defgeneric timestamp-to-sql (engine ts)
|
(defgeneric timestamp-to-sql (engine ts)
|
||||||
(:method ((engine db-engine) ts) ts))
|
(:method ((engine db-engine) ts) ts))
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
;;; cl-scopes/test/config-postgres.lisp
|
;;; cl-scopes/test/config-postgres.lisp
|
||||||
;;; use: `(load "test/...")` from package scopes/test-storage
|
;;; use: `(load "test/...")` from package scopes/test-storage
|
||||||
|
|
||||||
|
(in-package :scopes/test-storage)
|
||||||
|
|
||||||
(defparameter db-config-postgres
|
(defparameter db-config-postgres
|
||||||
'(:backend :dbi
|
'(:backend :dbi
|
||||||
:db-type :postgres
|
:db-type :postgres
|
||||||
|
@ -13,4 +15,7 @@
|
||||||
:options
|
:options
|
||||||
(:schema "testing")))
|
(:schema "testing")))
|
||||||
|
|
||||||
(setf scopes/test-storage:*db-config-test* db-config-postgres)
|
(config:root)
|
||||||
|
|
||||||
|
(config:add :storage :class 'storage:config :setup #'core:default-setup
|
||||||
|
:db-config db-config-postgres)
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
;;; cl-scopes/test/config-sqlite.lisp
|
;;; cl-scopes/test/config-sqlite.lisp
|
||||||
;;; use: `(load "test/...")` from package scopes/test-storage
|
;;; use: `(load "test/...")` from package scopes/test-storage
|
||||||
|
|
||||||
|
(in-package :scopes/test-storage)
|
||||||
|
|
||||||
(defparameter db-config-sqlite
|
(defparameter db-config-sqlite
|
||||||
`(:backend :dbi
|
`(:backend :dbi
|
||||||
:db-type :sqlite3
|
:db-type :sqlite3
|
||||||
|
@ -9,5 +11,7 @@
|
||||||
,(namestring (scopes/testing:test-path "test.db" "data")))
|
,(namestring (scopes/testing:test-path "test.db" "data")))
|
||||||
:options nil))
|
:options nil))
|
||||||
|
|
||||||
(setf scopes/test-storage:*db-config-test* db-config-sqlite)
|
(config:root)
|
||||||
|
|
||||||
|
(config:add :storage :class 'storage:config :setup #'core:default-setup
|
||||||
|
:db-config db-config-sqlite)
|
||||||
|
|
|
@ -4,7 +4,9 @@
|
||||||
|
|
||||||
(defpackage :scopes/test-storage
|
(defpackage :scopes/test-storage
|
||||||
(:use :common-lisp)
|
(:use :common-lisp)
|
||||||
(:local-nicknames (:storage :scopes/storage)
|
(:local-nicknames (:config :scopes/config)
|
||||||
|
(:core :scopes/core)
|
||||||
|
(:storage :scopes/storage)
|
||||||
(:tracking :scopes/storage/tracking)
|
(:tracking :scopes/storage/tracking)
|
||||||
(:t :scopes/testing))
|
(:t :scopes/testing))
|
||||||
(:export #:*db-config-test*
|
(:export #:*db-config-test*
|
||||||
|
@ -13,26 +15,24 @@
|
||||||
|
|
||||||
(in-package :scopes/test-storage)
|
(in-package :scopes/test-storage)
|
||||||
|
|
||||||
(defparameter *db-config-test* nil)
|
|
||||||
|
|
||||||
(defun run-all ()
|
(defun run-all ()
|
||||||
(run-sqlite)
|
(run-sqlite)
|
||||||
(run-postgres))
|
(run-postgres))
|
||||||
|
|
||||||
(defun run-sqlite ()
|
(defun run-sqlite ()
|
||||||
(load (t:test-path "config-sqlite"))
|
(load (t:test-path "config-sqlite"))
|
||||||
(let ((storage:*db-config* *db-config-test*)
|
(let ((t:*test-suite* (t:test-suite "sqlite")))
|
||||||
(t:*test-suite* (t:test-suite "sqlite")))
|
|
||||||
(run)))
|
(run)))
|
||||||
|
|
||||||
(defun run-postgres ()
|
(defun run-postgres ()
|
||||||
(load (t:test-path "config-postgres"))
|
(load (t:test-path "config-postgres"))
|
||||||
(let ((storage:*db-config* *db-config-test*)
|
(let ((t:*test-suite* (t:test-suite "postgres")))
|
||||||
(t:*test-suite* (t:test-suite "postgres")))
|
|
||||||
(run)))
|
(run)))
|
||||||
|
|
||||||
(defun run ()
|
(defun run ()
|
||||||
(let* ((engine (storage:make-engine))
|
(core:setup-services)
|
||||||
|
(let* ((cfg (core:config (core:find-service :storage)))
|
||||||
|
(engine (storage:make-engine cfg))
|
||||||
(st (storage:make-storage engine)))
|
(st (storage:make-storage engine)))
|
||||||
(setf (storage:schema st) (getf (storage:db-options st) :schema))
|
(setf (storage:schema st) (getf (storage:db-options st) :schema))
|
||||||
(test-track st)
|
(test-track st)
|
||||||
|
|
Loading…
Add table
Reference in a new issue