From ae17f6db14c064e3fcc680ea9661f0a9dab42db8 Mon Sep 17 00:00:00 2001 From: Tom Russell Date: Sun, 30 Sep 2018 19:49:41 +0100 Subject: [PATCH] Index and constrain building_user_likes (one like per building per user) --- migrations/004.location-date-size-like.up.sql | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/migrations/004.location-date-size-like.up.sql b/migrations/004.location-date-size-like.up.sql index 673d8138..2e62b36b 100644 --- a/migrations/004.location-date-size-like.up.sql +++ b/migrations/004.location-date-size-like.up.sql @@ -70,9 +70,16 @@ ALTER TABLE buildings ADD COLUMN IF NOT EXISTS size_width_frontage real; -- Likes -- Total likes (denormalised from up-to-one-vote-per-user) -ALTER TABLE buildings ADD COLUMN IF NOT EXISTS likes_total integer; +ALTER TABLE buildings ADD COLUMN IF NOT EXISTS likes_total integer DEFAULT 0; +-- Store users-buildings likes (many-to-many) CREATE TABLE IF NOT EXISTS building_user_likes ( + building_like_id serial PRIMARY KEY, building_id integer REFERENCES buildings, user_id uuid REFERENCES users ); +CREATE INDEX building_likes_idx ON building_user_likes ( building_id ); +CREATE INDEX user_likes_idx ON building_user_likes ( user_id ); + +-- One like per-building, per-user +ALTER TABLE building_user_likes ADD CONSTRAINT building_like_once UNIQUE (building_id, user_id);