From 672a1efde4634fcaecaa45c8f2898d152057b88a Mon Sep 17 00:00:00 2001 From: Tom Russell Date: Tue, 4 Aug 2020 15:53:33 +0100 Subject: [PATCH] Add migrations for user verification of building attributes --- migrations/018.verification.down.sql | 2 ++ migrations/018.verification.up.sql | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 migrations/018.verification.down.sql create mode 100644 migrations/018.verification.up.sql diff --git a/migrations/018.verification.down.sql b/migrations/018.verification.down.sql new file mode 100644 index 00000000..22dbb2ee --- /dev/null +++ b/migrations/018.verification.down.sql @@ -0,0 +1,2 @@ +-- Building verification +DROP TABLE IF EXISTS building_verification; \ No newline at end of file diff --git a/migrations/018.verification.up.sql b/migrations/018.verification.up.sql new file mode 100644 index 00000000..ca25dce9 --- /dev/null +++ b/migrations/018.verification.up.sql @@ -0,0 +1,28 @@ +-- Building verification + +-- Users can verify the correctness of individual building attribute values. + +-- For a building, it's most useful to know the count of verifications of each +-- attribute with current (or past) values. + +-- For a user, it's useful to show which attributes they have already verified +-- on a given building. + + +-- Store user-building-attribute verification +CREATE TABLE IF NOT EXISTS building_verification ( + verification_id serial PRIMARY KEY, + verification_timestamp TIMESTAMP default NOW(), + building_id integer REFERENCES buildings, + user_id uuid REFERENCES users, + attribute varchar, -- bit of a hack to refer to any `buildings` table column name + verified_value jsonb -- bit of a hack to include "any" value +); +CREATE INDEX building_verification_idx ON building_verification ( building_id ); +CREATE INDEX user_verification_idx ON building_verification ( user_id ); +CREATE INDEX building_user_verification_idx ON building_verification ( building_id, user_id ); + +-- Enforce that a user only has one opinion about the correct value of an +-- attribute for a given building (don't need to allow multiple verified_values) +ALTER TABLE building_verification ADD CONSTRAINT verify_building_attribute_once + UNIQUE ( building_id, user_id, attribute );