diff --git a/scopes.asd b/scopes.asd index 81d7f9b..b204ac2 100644 --- a/scopes.asd +++ b/scopes.asd @@ -21,3 +21,9 @@ :components ((:file "test/test-storage")) :perform (test-op (o c) (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))) diff --git a/storage/storage.lisp b/storage/storage.lisp index e7d8153..fd2baae 100644 --- a/storage/storage.lisp +++ b/storage/storage.lisp @@ -4,8 +4,10 @@ (defpackage :scopes/storage (:use :common-lisp) - (:local-nicknames (:alx :alexandria)) - (:export #:*db-config* + (:local-nicknames (:config :scopes/config) + (:alx :alexandria)) + (:export #:config + #:*db-config* #:make-engine #:make-storage #:engine #:timestamp-to-sql #:db-options #:db-params #:qualified-table-name @@ -15,7 +17,13 @@ (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* '(:dbi dbi-make-engine)) @@ -32,9 +40,10 @@ (params :initarg :params) (config :reader config :initarg :config))) -(defun make-engine () - (let ((backend (getf *db-config* :backend))) - (funcall (getf *backends* backend) *db-config*))) +(defun make-engine (cfg) + (let* ((dbconf (db-config cfg)) + (backend (getf dbconf :backend))) + (funcall (getf *backends* backend) dbconf))) (defgeneric timestamp-to-sql (engine ts) (:method ((engine db-engine) ts) ts)) diff --git a/test/config-postgres.lisp b/test/config-postgres.lisp index a80f8a7..6bdd9b6 100644 --- a/test/config-postgres.lisp +++ b/test/config-postgres.lisp @@ -1,6 +1,8 @@ ;;; cl-scopes/test/config-postgres.lisp ;;; use: `(load "test/...")` from package scopes/test-storage +(in-package :scopes/test-storage) + (defparameter db-config-postgres '(:backend :dbi :db-type :postgres @@ -13,4 +15,7 @@ :options (: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) diff --git a/test/config-sqlite.lisp b/test/config-sqlite.lisp index d59fb3b..717cd61 100644 --- a/test/config-sqlite.lisp +++ b/test/config-sqlite.lisp @@ -1,6 +1,8 @@ ;;; cl-scopes/test/config-sqlite.lisp ;;; use: `(load "test/...")` from package scopes/test-storage +(in-package :scopes/test-storage) + (defparameter db-config-sqlite `(:backend :dbi :db-type :sqlite3 @@ -9,5 +11,7 @@ ,(namestring (scopes/testing:test-path "test.db" "data"))) :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) diff --git a/test/test-storage.lisp b/test/test-storage.lisp index 62048d7..75469f3 100644 --- a/test/test-storage.lisp +++ b/test/test-storage.lisp @@ -4,7 +4,9 @@ (defpackage :scopes/test-storage (:use :common-lisp) - (:local-nicknames (:storage :scopes/storage) + (:local-nicknames (:config :scopes/config) + (:core :scopes/core) + (:storage :scopes/storage) (:tracking :scopes/storage/tracking) (:t :scopes/testing)) (:export #:*db-config-test* @@ -13,26 +15,24 @@ (in-package :scopes/test-storage) -(defparameter *db-config-test* nil) - (defun run-all () (run-sqlite) (run-postgres)) (defun run-sqlite () (load (t:test-path "config-sqlite")) - (let ((storage:*db-config* *db-config-test*) - (t:*test-suite* (t:test-suite "sqlite"))) + (let ((t:*test-suite* (t:test-suite "sqlite"))) (run))) (defun run-postgres () (load (t:test-path "config-postgres")) - (let ((storage:*db-config* *db-config-test*) - (t:*test-suite* (t:test-suite "postgres"))) + (let ((t:*test-suite* (t:test-suite "postgres"))) (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))) (setf (storage:schema st) (getf (storage:db-options st) :schema)) (test-track st)