Now creating a complete database initialisation script that runs.
This commit is contained in:
parent
c2d006ac3b
commit
8f24c314a1
|
@ -232,11 +232,11 @@
|
|||
farside (entity-for-property property application)]
|
||||
(flatten
|
||||
(map
|
||||
(fn [f]
|
||||
(fn [p]
|
||||
(if
|
||||
(= (:type (:attrs f)) "entity")
|
||||
(compose-convenience-entity-field f farside application)
|
||||
(str (safe-name (:table (:attrs farside))) "." (field-name f))))
|
||||
(= (:type (:attrs p)) "entity")
|
||||
(compose-convenience-entity-field p farside application)
|
||||
(str (safe-name (:table (:attrs farside))) "." (field-name p))))
|
||||
(user-distinct-properties farside)))))
|
||||
|
||||
|
||||
|
@ -295,14 +295,16 @@
|
|||
|
||||
|
||||
(defn emit-convenience-entity-field
|
||||
[field entity application]
|
||||
(str
|
||||
(s/join
|
||||
" ||', '|| "
|
||||
(compose-convenience-entity-field field entity application))
|
||||
" AS "
|
||||
(field-name field)
|
||||
"_expanded"))
|
||||
[property entity application]
|
||||
(if
|
||||
(= "entity" (-> property :attrs :type))
|
||||
(str
|
||||
(s/join
|
||||
" ||', '|| "
|
||||
(compose-convenience-entity-field property entity application))
|
||||
" AS "
|
||||
(field-name property)
|
||||
"_expanded")))
|
||||
|
||||
|
||||
(defn emit-convenience-view
|
||||
|
@ -310,9 +312,9 @@
|
|||
menus, et cetera."
|
||||
[entity application]
|
||||
(let [view-name (safe-name (str "lv_" (:table (:attrs entity))) :sql)
|
||||
entity-fields (filter
|
||||
#(= (:type (:attrs %)) "entity")
|
||||
(properties entity))]
|
||||
entity-properties (filter
|
||||
#(= (:type (:attrs %)) "entity")
|
||||
(properties entity))]
|
||||
(s/join
|
||||
"\n"
|
||||
(remove
|
||||
|
@ -329,21 +331,23 @@
|
|||
"SELECT "
|
||||
(s/join
|
||||
",\n\t"
|
||||
(flatten
|
||||
(map
|
||||
#(if
|
||||
(= (:type (:attrs %)) "entity")
|
||||
(list
|
||||
(emit-convenience-entity-field % entity application)
|
||||
(remove
|
||||
nil?
|
||||
(flatten
|
||||
(map
|
||||
#(if
|
||||
(= (:type (:attrs %)) "entity")
|
||||
(list
|
||||
(emit-convenience-entity-field % entity application)
|
||||
(str (safe-name entity) "." (field-name %)))
|
||||
(str (safe-name entity) "." (field-name %)))
|
||||
(str (safe-name entity) "." (field-name %)))
|
||||
(filter
|
||||
#(not= (:type (:attrs %)) "link")
|
||||
(all-properties entity) )))))
|
||||
(remove
|
||||
#(#{"link" "list"} (:type (:attrs %)))
|
||||
(all-properties entity) ))))))
|
||||
(str
|
||||
"FROM " (s/join ", " (set (compose-convenience-view-select-list entity application true))))
|
||||
(if-not
|
||||
(empty? entity-fields)
|
||||
(empty? entity-properties)
|
||||
(str
|
||||
"WHERE "
|
||||
(s/join
|
||||
|
@ -360,7 +364,7 @@
|
|||
(safe-name (:table (:attrs farside)) :sql)
|
||||
"."
|
||||
(safe-name (first (key-names farside)) :sql))))
|
||||
entity-fields))))
|
||||
entity-properties))))
|
||||
";"
|
||||
(emit-permissions-grant view-name :SELECT (find-permissions entity application))))))))
|
||||
|
||||
|
|
|
@ -421,4 +421,17 @@
|
|||
(let [expected "------------------------------------------------------------------------\n--\tTest doc \n--\t\n--\tAll dwellings within addresses in the system; a\n--\t dwelling is a house, flat or appartment in which electors live.\n--\t Every address should have at least one dwelling; essentially,\n--\t an address maps onto a street door and dwellings map onto\n--\t what's behind that door. So a tenement or a block of flats\n--\t would be one address with many dwellings. \n------------------------------------------------------------------------\nCREATE TABLE dwellings\n(\n\t id SERIAL NOT NULL PRIMARY KEY,\n\t address_id INTEGER NOT NULL,\n\t sub_address VARCHAR(32)\n);\nGRANT SELECT ON dwellings TO admin,\n\tanalysts,\n\tcanvassers,\n\tissueeditors,\n\tissueexperts,\n\tteamorganisers ;\nGRANT INSERT ON dwellings TO admin ;\nGRANT UPDATE ON dwellings TO admin ;\nGRANT DELETE ON dwellings TO admin ;"
|
||||
actual (emit-table dwelling-entity application "Test doc")]
|
||||
(is (= actual expected))))
|
||||
(testing "Convenience entity field - is an entity field, should emit"
|
||||
(let [property (child-with-tag address-entity :property #(= (-> % :attrs :name) "district_id"))
|
||||
expected "districts.name AS district_id_expanded"
|
||||
actual (emit-convenience-entity-field property address-entity application)]
|
||||
(is (= actual expected))))
|
||||
|
||||
(testing "Convenience entity field - is not an entity field, should not emit"
|
||||
(let [farside dwelling-entity
|
||||
property (child-with-tag address-entity :property #(= (-> % :attrs :name) "dwellings"))
|
||||
expected nil
|
||||
actual (emit-convenience-entity-field property address-entity application)]
|
||||
(is (= actual expected))))
|
||||
|
||||
))
|
||||
|
|
Loading…
Reference in a new issue