#21: Tables created

Sorted out the migratus stuff so that it actually works, and added a bit to README to say how to get the database initialised; added a new migration to add the roles and teams table.
This commit is contained in:
simon 2017-03-15 20:13:22 +00:00
parent 3850753743
commit d83a7924e2
6 changed files with 574 additions and 158 deletions

View file

@ -34,26 +34,6 @@
;;; redundant, and if they are the namespace probably needs to be renamed to 'entities'.
;;; See also resources/migrations/20161014170335-basic-setup.up.sql
(defn create-districts-table!
"Create a table to hold the electoral districts in which electors are registered.
Note that, as this app is being developed for the independence referendum in which
polling is across the whole of Scotland, this part of the design isn't fully thought
through; if later adapted to general or local elections, some breakdown or hierarchy
of polling districts into constituencies will be required."
[]
(sql/db-do-commands
yyydb/*db*
(sql/create-table-ddl
:districts
;; it may be necessary to have a serial abstract primary key but I suspect
;; polling districts already have numbers assigned by the Electoral Commission and
;; it would be sensible to use those. TODO: check.
[:id "integer not null primary key"]
[:name "varchar(64) not null"]
;; TODO: it would make sense to hold polygon data for polling districts so we can reflect
;; them on the map, but I haven't thought through how to do that yet.
)))
(kc/defentity district
(kc/pk :id)
@ -186,7 +166,7 @@
(kc/entity-fields :id :fullname :phone :email :is_admin :authorised)
(kc/has-one elector)
(kc/has-one address)
(kc/has-one canvasser {:fk :introduced_by})
;; (kc/has-one canvasser {:fk :introduced_by})
(kc/has-one authority))
@ -244,7 +224,7 @@
yyydb/*db*
(sql/create-table-ddl
:optionsdistricts
[:option_id"varchar(32) not null references options(option)"]
[:option_id "varchar(32) not null references options(option)"]
[:district_id "integer not null references districts(id)"])))
@ -395,6 +375,73 @@
(kc/has-one canvasser {:fk :actor}))
(defn create-role-table!
"Create a table to record roles. I'm not even yet certain that this is strictly necessary,
but it allows us to record the fact that different users (canvassers) have different roles
in the system."
[]
(sql/db-do-commands
yyydb/*db*
(sql/create-table-ddl
:roles
[:id "serial primary key"]
[:name "varchar(64) not null"])))
(defn create-role-membership-table!
"Create a link table to record membership of roles."
[]
(sql/db-do-commands
yyydb/*db*
(sql/create-table-ddl
:rolememberships
[:role_id "integer not null references role(id)"]
[:canvasser_id "integer not null references canvasser(id)"])))
(kc/defentity role
(kc/table :roles)
(kc/database yyydb/*db*)
(kc/entity-fields :id :name)
(kc/many-to-many canvasser :rolememberships))
(defn create-team-table!
"Create a table to record teams."
[]
(sql/db-do-commands
yyydb/*db*
(sql/create-table-ddl
:teams
[:id "serial primary key"]
[:name "varchar(64) not null"]
;; the electoral district within which this address exists
[:district_id "integer references districts(id)"]
;; nominal home location of this team
[:latitude :real]
[:longitude :real])))
(defn create-team-membership-table!
"Create a link table to record membership of team."
[]
(sql/db-do-commands
yyydb/*db*
(sql/create-table-ddl
:teammemberships
[:team_id "integer not null references team(id)"]
[:canvasser_id "integer not null references canvasser(id)"])))
(kc/defentity team
(kc/table :teams)
(kc/database yyydb/*db*)
(kc/entity-fields :id :name :latitude :longitude)
(kc/has-one district)
(kc/many-to-many canvasser :teammemberships))
(defn init-db! []
"Initialised the whole database."
(create-districts-table!)
@ -409,4 +456,8 @@
(create-issue-expertise-table!)
(create-followup-requests-table!)
(create-followup-actions-table!)
(create-role-table!)
(create-role-membership-table!)
(create-team-table!)
(create-team-membership-table!)
)