Skip to content

Commit

Permalink
Add :around method around model/table-name so it ALWAYS returns a…
Browse files Browse the repository at this point in the history
… keyword (#104)
  • Loading branch information
camsaul committed Jan 18, 2023
1 parent d884b86 commit c50a1e1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
9 changes: 8 additions & 1 deletion src/toucan2/model.clj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
(throw (ex-info (format "Invalid model %s: don't know how to get its table name." (pr-str model))
{:model model}))))

(m/defmethod table-name :around :default
"Always return table names as keywords. This will facilitate using them directly inside Honey SQL, e.g.
{:select [:*], :from [(t2/table-name MyModel)]}"
[model]
(keyword (next-method model)))

#_{:clj-kondo/ignore [:redundant-fn-wrapper]} ; FIXME
(m/defmethod table-name clojure.lang.Named
[model]
Expand Down Expand Up @@ -121,7 +128,7 @@
(not= (m/effective-primary-method table-name model)
(m/default-effective-method table-name))))
(map (fn [[model a-namespace]]
[(table-name model) a-namespace])))
[(name (table-name model)) a-namespace])))
(model->namespace model))))

(defn namespace [model]
Expand Down
2 changes: 1 addition & 1 deletion test/toucan2/insert_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
(is (= (if returning-keys?
[4]
1)
(insert! (str (model/table-name ::test/venues)) {:name "Grant & Green", :category "bar"})))
(insert! (name (model/table-name ::test/venues)) {:name "Grant & Green", :category "bar"})))
(testing "Venue 4 should exist now"
(is (= (instance/instance ::test/venues {:id 4
:name "Grant & Green"
Expand Down
20 changes: 12 additions & 8 deletions test/toucan2/model_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,19 @@
#"Bad toucan2.model/primary-keys for model .* should return keyword or sequence of keywords, got .*"
(model/primary-keys ::primary-keys.returns-invalid)))))

(m/defmethod model/table-name ::string-table-name
[_model]
"my-table")

(deftest ^:parallel ^:parallel table-name-test
(doseq [[model expected] {"ABC" "ABC"
:abc "abc"
:ns/abc "abc"
:default "default"
'symbol "symbol"}]
(testing (pr-str `(model/table-name ~model))
(is (= expected
(model/table-name model))))))
(are [model expected] (= expected
(model/table-name model))
"ABC" :ABC
:abc :abc
:ns/abc :abc
:default :default
'symbol :symbol
::string-table-name :my-table))

(derive ::people.unquoted ::test/people)

Expand Down
2 changes: 1 addition & 1 deletion test/toucan2/test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
(str/trim stmt))))

(defn create-table!
"Create the table named `table-name` for the [[current-db-type]] using the SQL from [[create-table-sql-file]]."
"(Re-)Create the table named `table-name` for the [[current-db-type]] using the SQL from [[create-table-sql-file]]."
([table-name]
(create-table! (current-db-type) table-name))

Expand Down

0 comments on commit c50a1e1

Please sign in to comment.