Skip to content

Commit dc0e9f9

Browse files
committed
Ensure the database folder exists
1 parent 57ba5fa commit dc0e9f9

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

src/clojure_skills/db/core.clj

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@
22
"Database connection management and operations."
33
(:require
44
[clojure-skills.config :as config]
5-
[clojure-skills.db.migrate :as migrate]))
5+
[clojure-skills.db.migrate :as migrate]
6+
[clojure.java.io :as io]))
7+
8+
(defn ensure-db-dir
9+
"Ensure the directory containing the database file exists."
10+
[db-path]
11+
(let [db-file (io/file db-path)]
12+
(when-let [parent (.getParentFile db-file)]
13+
(when-not (.exists parent)
14+
(.mkdirs parent)))))
615

716
(defn get-db
817
"Get database connection spec from config.
9-
Returns a simple db-spec map that next.jdbc can use directly.
10-
Foreign keys are enabled for SQLite to support CASCADE deletes."
18+
Returns a simple db-spec map that next.jdbc can use directly.
19+
Foreign keys are enabled for SQLite to support CASCADE deletes."
1120
([]
1221
(get-db (config/load-config)))
1322
([config]
@@ -22,5 +31,8 @@
2231
([db]
2332
(migrate/migrate-db db))
2433
([]
25-
(let [db (get-db)]
26-
(init-db db))))
34+
(let [config (config/load-config)
35+
db-path (config/get-db-path config)]
36+
(ensure-db-dir db-path)
37+
(let [db (get-db config)]
38+
(init-db db)))))

test/clojure_skills/cli_db_test.clj

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
["SELECT name FROM sqlite_master WHERE type='table'"])
4242
table-names (set (map :sqlite_master/name tables))]
4343
(testing "Then: the set should contain the tables created by migrations"
44-
(is (contains? table-names "skills") "Skills table exists")
45-
(is (contains? table-names "prompts") "Prompts table exists"))))))
44+
(is (contains? table-names "skills"))
45+
(is (contains? table-names "prompts")))))))
4646

4747
;; Tests for db init command
4848
(deftest db-init-command-test
@@ -162,8 +162,9 @@
162162
;; 3. Stats
163163
(let [{:keys [output]} (capture-output #(cli/cmd-stats {}))
164164
parsed (tu/parse-json-output output)]
165-
(is (= "stats" (:type parsed)))
166-
(is (map? (:database parsed))))
165+
(is (match? {:type "stats"
166+
:database map?}
167+
parsed)))
167168

168169
;; 4. Reset
169170
(let [{:keys [output]} (capture-output #(cli/cmd-reset-db {:force true}))]
@@ -180,12 +181,10 @@
180181
(with-redefs [cli/load-config-and-db mock-load-config-and-db]
181182
(testing "When: We request database statistics"
182183
(let [{:keys [output]} (capture-output #(cli/cmd-stats {}))
183-
parsed (tu/parse-json-output output)
184-
config (:configuration parsed)]
184+
parsed (tu/parse-json-output output)]
185185
(testing "Then: Configuration should include config file paths"
186-
(is (contains? config :config-file-path) "config-file-path is present")
187-
(is (string? (:config-file-path config)) "config-file-path is a string")
188-
(is (contains? config :project-config-path) "project-config-path is present")
189-
(is (string? (:project-config-path config)) "project-config-path is a string")
190-
(is (contains? config :database-path) "database-path is present")
191-
(is (string? (:database-path config)) "database-path is a string"))))))))
186+
(is (match? {:type "stats"
187+
:configuration {:config-file-path string?
188+
:project-config-path string?
189+
:database-path string?}}
190+
parsed)))))))))

0 commit comments

Comments
 (0)