From 1d8694846fcd71605ecf1e89b20309769e28eca4 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Sun, 4 Oct 2020 16:21:57 -0600 Subject: [PATCH] Give response a composite key --- back/store.go | 25 ++++++++++++++++--------- todo.txt | 3 ++- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/back/store.go b/back/store.go index e717028..b5f9571 100644 --- a/back/store.go +++ b/back/store.go @@ -90,20 +90,23 @@ const schema = ` ); CREATE TABLE response ( - id TEXT NOT NULL PRIMARY KEY, + id TEXT NOT NULL, event_id TEXT NOT NULL, guest_name TEXT NOT NULL, + PRIMARY KEY (id, event_id), FOREIGN KEY (event_id) REFERENCES event(id) ); CREATE TABLE response_time ( + event_id TEXT NOT NULL, response_id TEXT NOT NULL, date DATE NOT NULL, time INTEGER NOT NULL, CHECK (0 <= time < 24), - UNIQUE (response_id, date, time) + UNIQUE (response_id, date, time), + FOREIGN KEY(event_id, response_id) REFERENCES response(event_id, id) ); CREATE TRIGGER enforce_valid_response_dates @@ -300,8 +303,7 @@ func (s *Store) GetEventResponseSummary(ctx context.Context, query GetEventRespo result.PerHourCounts = make(map[date.Date]map[int]int) const perHourCountQuery = ` SELECT date, time, COUNT(*) - FROM response - JOIN response_time ON response.id = response_time.response_id + FROM response_time WHERE event_id = ? GROUP BY date, time;` err = sqlitex.Exec(conn, perHourCountQuery, @@ -344,16 +346,21 @@ func (s *Store) CreateEventResponse(ctx context.Context, cmd CreateEventResponse return } - const responseQuery = `INSERT INTO response(event_id, id, guest_name) VALUES (?, ?, ?);` + const responseQuery = ` + INSERT INTO response(event_id, id, guest_name) + VALUES (?, ?, ?);` err = sqlitex.Exec(conn, responseQuery, nil, cmd.EventID, responseID, cmd.GuestName) if err != nil { return } - const responseTimeQuery = `INSERT INTO response_time(response_id, date, time) VALUES (?, ?, ?);` + const responseTimeQuery = ` + INSERT INTO response_time(event_id, response_id, date, time) + VALUES (?, ?, ?, ?);` for d, hs := range cmd.DateHours { for h := range hs { - err = sqlitex.Exec(conn, responseTimeQuery, nil, responseID, d.Format(dbDateLayout), h) + err = sqlitex.Exec(conn, responseTimeQuery, nil, + cmd.EventID, responseID, d.Format(dbDateLayout), h) if err != nil { return } @@ -403,7 +410,7 @@ func (s *Store) GetEventResponse(ctx context.Context, query GetEventResponseQuer const responseTimeQuery = ` SELECT date, time FROM response_time - WHERE response_id = ?` + WHERE event_id = ? AND response_id = ?` err = sqlitex.Exec(conn, responseTimeQuery, func(stmt *sqlite.Stmt) error { d, err := date.Parse(dbDateLayout, stmt.ColumnText(0)) @@ -415,7 +422,7 @@ func (s *Store) GetEventResponse(ctx context.Context, query GetEventResponseQuer } result.DateHours[d][stmt.ColumnInt(1)] = struct{}{} return nil - }, query.ResponseID) + }, query.EventID, query.ResponseID) if err != nil { return GetEventResponseResult{}, err } diff --git a/todo.txt b/todo.txt index e1e54c2..2dca8a4 100644 --- a/todo.txt +++ b/todo.txt @@ -1,6 +1,8 @@ Essential: ------------ Show response after submission +Add a schema ID to the DB +Move front and back into internal Require earliest and latest dates for creation Ensure latest date is at least earliest date Ensure date span is within a maximum @@ -9,7 +11,6 @@ Prevent blank event names and guest names Cleanup: ------------ -Give response (and therefore response_time) a composite key Get rid of the Stat call in NewStore. I can't remember the details. I think the old version of sqlitex was panicking if I failed to open a DB file.