From 5d024b1aa0a0d21f2711c568f405e98f10ad4088 Mon Sep 17 00:00:00 2001 From: Marcus Kammer Date: Wed, 5 Jun 2024 16:48:40 +0200 Subject: [PATCH] Be sure db file exist --- src/handlers/main.lisp | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/handlers/main.lisp b/src/handlers/main.lisp index 207cdff..7a6e901 100644 --- a/src/handlers/main.lisp +++ b/src/handlers/main.lisp @@ -1,9 +1,7 @@ (in-package :ml-survey/handlers) (defvar *survey-data-dir* - (ensure-directories-exist (format nil - "~adata/survey/" - (uiop:getcwd)))) + (ensure-directories-exist (uiop:merge-pathnames* "data/survey"))) (defun split-uri (uri) (check-type uri string) @@ -22,11 +20,23 @@ (local-time:now) :format '((:hour 2) ":" (:min 2) ":" (:sec 2)))) -(defun make-db-path (date-str file-str) - (pathname (concatenate 'string date-str file-str))) +(defun ensure-file-exists (pathname) + "Ensure that a file specified by PATHNAME exists, create it if it doesn't." + (unless (uiop:file-exists-p pathname) + (with-open-file (stream pathname + :direction :output + :if-exists :overwrite + :if-does-not-exist :create) + (format stream ""))) + pathname) -(defun make-surveys-db-path () - (make-db-path (today) "-surveys-db.lisp")) +(defun make-db-file (file-str) + "Prepare and ensure a database file at FILE-STR path." + (let ((path (uiop:merge-pathnames* file-str))) + (ensure-file-exits (truename path)))) + +(defun make-surveys-db-file () + (make-db-file "surveys-db.lisp")) (defun load-response (db) (check-type db (or string pathname))