cacert-boardvoting/internal/migrations/2022052701_add_vote_constrains.up.sql

25 lines
614 B
MySQL
Raw Permalink Normal View History

2022-05-27 15:39:54 +00:00
-- add constraints on votes table
CREATE TABLE votes_new
(
decision INTEGER NOT NULL REFERENCES decisions (id),
voter INTEGER NOT NULL REFERENCES voters (id),
vote INTEGER NOT NULL,
voted DATETIME NOT NULL,
notes TEXT NOT NULL DEFAULT '',
PRIMARY KEY (decision, voter)
);
INSERT INTO votes_new (decision, voter, vote, voted, notes)
SELECT decision,
voter,
vote,
voted,
notes
FROM votes
GROUP BY decision, voter
HAVING MAX(voted) = voted;
ALTER TABLE votes
RENAME TO votes_orig_with_duplicates;
ALTER TABLE votes_new
RENAME TO votes;