From 559c3f3281c9c8b3b189e8cd7b6b95cc8f40eeee Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Sun, 12 May 2024 12:37:24 +0200 Subject: [PATCH] storage refactoring: prepare for using methods for storage creation and db access --- storage/storage.lisp | 38 +++++++++++++++----------------------- storage/tracking.lisp | 8 +++++--- test/config-postgres.lisp | 3 ++- test/config-sqlite.lisp | 3 ++- 4 files changed, 24 insertions(+), 28 deletions(-) diff --git a/storage/storage.lisp b/storage/storage.lisp index 80f9239..6c9bacb 100644 --- a/storage/storage.lisp +++ b/storage/storage.lisp @@ -14,14 +14,18 @@ (defparameter *db-config* nil) (defparameter *db-engine* nil) +(defparameter *backends* + '(:dbi #'dbi-make-storage)) + +(defvar *db-params* + '(:sqlite3 (:id-type integer :json-type json) + :postgres (:id-type bigserial :json-type jsonb))) + (defclass db-engine () ((connect :initarg :connect) (params :initarg :params) (config :initarg :config))) -(defun make-engine () - (make-engine-db (getf *db-config* :db-type) *db-config*)) - (defclass storage () ((engine :initarg :engine) (db :reader db) @@ -41,24 +45,12 @@ (print sql) (dbi:do-sql (db st) sql))) -;; db-driver-specific stuff - -(defun connect-sqlite (params config) - (declare (ignorable params)) - (apply #'dbi:connect-cached :sqlite3 (getf config :connect-args))) - -(defun connect-postgres (params config) - (declare (ignorable params)) - (apply #'dbi:connect-cached :postgres (getf config :connect-args))) - -(defvar *db-params* - '(:sqlite3 (:connect connect-sqlite - :id-type integer :json-type json) - :postgres (:connect connect-postgres - :id-type bigserial :json-type jsonb))) - -(defun make-engine-db (db-type config) - (let ((params (getf *db-params* db-type))) - (make-instance 'db-engine :params params :config config +(defun make-engine () + (let* ((config *db-config*) + (db-type (getf config :db-type)) + (params (getf *db-params* db-type)) + (conn-args (getf config :connect-args))) + (make-instance 'db-engine + :params params :config config :connect #'(lambda () - (funcall (getf params :connect) params config))))) + (apply #'dbi:connect-cached db-type conn-args))))) diff --git a/storage/tracking.lisp b/storage/tracking.lisp index fe479be..e705211 100644 --- a/storage/tracking.lisp +++ b/storage/tracking.lisp @@ -25,14 +25,16 @@ ((params (storage:db-params st)) (id-type (getf params :id-type)) (json-type (getf params :json-type)) - (hf-def (mapcar #'(lambda (x) (list x :type 'text)) head-fields)) + (hf-def (mapcar #'(lambda (x) + (list x :type 'text :not-null t :default '|''|)) + head-fields)) (sql (sxql:yield (sxql:make-statement :create-table table-name (nconc - `((trackid :type ,id-type :primary-key t)) + `((trackid :type ,id-type :primary-key t :not-null t)) hf-def - `((timestamp :type timestamp) + `((timestamp :type timestamptz :not-null t :default current_timestamp) (data :type ,json-type))))))) (print sql) (dbi:do-sql (storage:db st) sql))) diff --git a/test/config-postgres.lisp b/test/config-postgres.lisp index 0e3048a..a80f8a7 100644 --- a/test/config-postgres.lisp +++ b/test/config-postgres.lisp @@ -2,7 +2,8 @@ ;;; use: `(load "test/...")` from package scopes/test-storage (defparameter db-config-postgres - '(:db-type :postgres + '(:backend :dbi + :db-type :postgres :connect-args (:database-name "cltest" :host "localhost" diff --git a/test/config-sqlite.lisp b/test/config-sqlite.lisp index ce0dfa7..00c5323 100644 --- a/test/config-sqlite.lisp +++ b/test/config-sqlite.lisp @@ -2,7 +2,8 @@ ;;; use: `(load "test/...")` from package scopes/test-storage (defparameter db-config-sqlite - '(:db-type :sqlite3 + '(:backend :dbi + :db-type :sqlite3 :connect-args (:database-name "test/test.db") :options nil))