Now creating a complete database initialisation script that runs.

This commit is contained in:
Simon Brooke 2019-11-16 13:15:27 +00:00
parent c2d006ac3b
commit 8f24c314a1
2 changed files with 44 additions and 27 deletions

View file

@ -232,11 +232,11 @@
farside (entity-for-property property application)] farside (entity-for-property property application)]
(flatten (flatten
(map (map
(fn [f] (fn [p]
(if (if
(= (:type (:attrs f)) "entity") (= (:type (:attrs p)) "entity")
(compose-convenience-entity-field f farside application) (compose-convenience-entity-field p farside application)
(str (safe-name (:table (:attrs farside))) "." (field-name f)))) (str (safe-name (:table (:attrs farside))) "." (field-name p))))
(user-distinct-properties farside))))) (user-distinct-properties farside)))))
@ -295,14 +295,16 @@
(defn emit-convenience-entity-field (defn emit-convenience-entity-field
[field entity application] [property entity application]
(str (if
(s/join (= "entity" (-> property :attrs :type))
" ||', '|| " (str
(compose-convenience-entity-field field entity application)) (s/join
" AS " " ||', '|| "
(field-name field) (compose-convenience-entity-field property entity application))
"_expanded")) " AS "
(field-name property)
"_expanded")))
(defn emit-convenience-view (defn emit-convenience-view
@ -310,9 +312,9 @@
menus, et cetera." menus, et cetera."
[entity application] [entity application]
(let [view-name (safe-name (str "lv_" (:table (:attrs entity))) :sql) (let [view-name (safe-name (str "lv_" (:table (:attrs entity))) :sql)
entity-fields (filter entity-properties (filter
#(= (:type (:attrs %)) "entity") #(= (:type (:attrs %)) "entity")
(properties entity))] (properties entity))]
(s/join (s/join
"\n" "\n"
(remove (remove
@ -329,21 +331,23 @@
"SELECT " "SELECT "
(s/join (s/join
",\n\t" ",\n\t"
(flatten (remove
(map nil?
#(if (flatten
(= (:type (:attrs %)) "entity") (map
(list #(if
(emit-convenience-entity-field % entity application) (= (: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 %)))
(str (safe-name entity) "." (field-name %))) (remove
(filter #(#{"link" "list"} (:type (:attrs %)))
#(not= (:type (:attrs %)) "link") (all-properties entity) ))))))
(all-properties entity) )))))
(str (str
"FROM " (s/join ", " (set (compose-convenience-view-select-list entity application true)))) "FROM " (s/join ", " (set (compose-convenience-view-select-list entity application true))))
(if-not (if-not
(empty? entity-fields) (empty? entity-properties)
(str (str
"WHERE " "WHERE "
(s/join (s/join
@ -360,7 +364,7 @@
(safe-name (:table (:attrs farside)) :sql) (safe-name (:table (:attrs farside)) :sql)
"." "."
(safe-name (first (key-names farside)) :sql)))) (safe-name (first (key-names farside)) :sql))))
entity-fields)))) entity-properties))))
";" ";"
(emit-permissions-grant view-name :SELECT (find-permissions entity application)))))))) (emit-permissions-grant view-name :SELECT (find-permissions entity application))))))))

View file

@ -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 ;" (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")] actual (emit-table dwelling-entity application "Test doc")]
(is (= actual expected)))) (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))))
)) ))