/test/lobos/test/sample_schema.clj

http://github.com/budu/lobos · Clojure · 48 lines · 33 code · 8 blank · 7 comment · 0 complexity · 1b414e2ec68cf5c694b338455f845d2c MD5 · raw file

  1. ;; Copyright (c) Nicolas Buduroi. All rights reserved.
  2. ;; The use and distribution terms for this software are covered by the
  3. ;; Eclipse Public License 1.0 which can be found in the file
  4. ;; epl-v10.html at the root of this distribution. By using this software
  5. ;; in any fashion, you are agreeing to be bound by the terms of this
  6. ;; license.
  7. ;; You must not remove this notice, or any other, from this software.
  8. (ns lobos.test.sample-schema
  9. (:refer-clojure :exclude [alter compile drop
  10. bigint boolean char double float time])
  11. (:use (lobos core schema)))
  12. (defn surrogate-key [table]
  13. (integer table :id :auto-inc :primary-key))
  14. (defn datetime-tracked [table]
  15. (-> table
  16. (timestamp :updated_on)
  17. (timestamp :created_on (default (now)))))
  18. (defn refer-to [table ptable]
  19. (let [cname (-> (->> ptable name butlast (apply str))
  20. (str "_id")
  21. keyword)]
  22. (integer table cname [:refer ptable :id :on-delete :set-null])))
  23. (defmacro tbl [name & elements]
  24. `(-> (table ~name
  25. (surrogate-key)
  26. (datetime-tracked))
  27. ~@elements))
  28. (def sample-schema
  29. (schema :lobos
  30. (tbl :users
  31. (varchar :name 100 :unique)
  32. (check :name (> (length :name) 1)))
  33. (tbl :posts
  34. (varchar :title 200 :unique)
  35. (text :content)
  36. (refer-to :users))
  37. (tbl :comments
  38. (text :content)
  39. (refer-to :users)
  40. (refer-to :posts))))