Give response a composite key

This commit is contained in:
Brandon Dyck 2020-10-04 16:21:57 -06:00
parent 12c826bd34
commit 1d8694846f
2 changed files with 18 additions and 10 deletions

View File

@ -90,20 +90,23 @@ const schema = `
); );
CREATE TABLE response ( CREATE TABLE response (
id TEXT NOT NULL PRIMARY KEY, id TEXT NOT NULL,
event_id TEXT NOT NULL, event_id TEXT NOT NULL,
guest_name TEXT NOT NULL, guest_name TEXT NOT NULL,
PRIMARY KEY (id, event_id),
FOREIGN KEY (event_id) REFERENCES event(id) FOREIGN KEY (event_id) REFERENCES event(id)
); );
CREATE TABLE response_time ( CREATE TABLE response_time (
event_id TEXT NOT NULL,
response_id TEXT NOT NULL, response_id TEXT NOT NULL,
date DATE NOT NULL, date DATE NOT NULL,
time INTEGER NOT NULL, time INTEGER NOT NULL,
CHECK (0 <= time < 24), 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 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) result.PerHourCounts = make(map[date.Date]map[int]int)
const perHourCountQuery = ` const perHourCountQuery = `
SELECT date, time, COUNT(*) SELECT date, time, COUNT(*)
FROM response FROM response_time
JOIN response_time ON response.id = response_time.response_id
WHERE event_id = ? WHERE event_id = ?
GROUP BY date, time;` GROUP BY date, time;`
err = sqlitex.Exec(conn, perHourCountQuery, err = sqlitex.Exec(conn, perHourCountQuery,
@ -344,16 +346,21 @@ func (s *Store) CreateEventResponse(ctx context.Context, cmd CreateEventResponse
return 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) err = sqlitex.Exec(conn, responseQuery, nil, cmd.EventID, responseID, cmd.GuestName)
if err != nil { if err != nil {
return 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 d, hs := range cmd.DateHours {
for h := range hs { 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 { if err != nil {
return return
} }
@ -403,7 +410,7 @@ func (s *Store) GetEventResponse(ctx context.Context, query GetEventResponseQuer
const responseTimeQuery = ` const responseTimeQuery = `
SELECT date, time SELECT date, time
FROM response_time FROM response_time
WHERE response_id = ?` WHERE event_id = ? AND response_id = ?`
err = sqlitex.Exec(conn, responseTimeQuery, err = sqlitex.Exec(conn, responseTimeQuery,
func(stmt *sqlite.Stmt) error { func(stmt *sqlite.Stmt) error {
d, err := date.Parse(dbDateLayout, stmt.ColumnText(0)) 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{}{} result.DateHours[d][stmt.ColumnInt(1)] = struct{}{}
return nil return nil
}, query.ResponseID) }, query.EventID, query.ResponseID)
if err != nil { if err != nil {
return GetEventResponseResult{}, err return GetEventResponseResult{}, err
} }

View File

@ -1,6 +1,8 @@
Essential: Essential:
------------ ------------
Show response after submission Show response after submission
Add a schema ID to the DB
Move front and back into internal
Require earliest and latest dates for creation Require earliest and latest dates for creation
Ensure latest date is at least earliest date Ensure latest date is at least earliest date
Ensure date span is within a maximum Ensure date span is within a maximum
@ -9,7 +11,6 @@ Prevent blank event names and guest names
Cleanup: Cleanup:
------------ ------------
Give response (and therefore response_time) a composite key
Get rid of the Stat call in NewStore. Get rid of the Stat call in NewStore.
I can't remember the details. I think the old version I can't remember the details. I think the old version
of sqlitex was panicking if I failed to open a DB file. of sqlitex was panicking if I failed to open a DB file.