From 35018a091aefc51dbd9ddbe4f06b44a977598d4c Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Mon, 4 Jul 2022 13:17:05 +0300 Subject: [PATCH] Fix plus tier view when many results w/ suggests --- migrations/002-fix-plus-tier.js | 103 ++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 migrations/002-fix-plus-tier.js diff --git a/migrations/002-fix-plus-tier.js b/migrations/002-fix-plus-tier.js new file mode 100644 index 000000000..d9cf02808 --- /dev/null +++ b/migrations/002-fix-plus-tier.js @@ -0,0 +1,103 @@ +// add missing min() call to handle multiple results w/ suggests correctly + +module.exports.up = function (db) { + db.prepare(`drop view "PlusTier"`).run(); + + db.prepare( + ` + create view "PlusTier" as + select + "votedId" as "userId", + min( + case + when "passedVoting" = 0 + and "wasSuggested" = 1 then null + when "passedVoting" = 1 then "tier" + when "passedVoting" = 0 + and "tier" != 3 then "tier" + 1 + end + ) as "tier" + from + "PlusVotingResult" + where + year = ( + select + "year" + from + "PlusVote" + where + "validAfter" < strftime('%s', 'now') + order by + "year" desc, + "month" desc + limit + 1 + ) + and "month" = ( + select + "month" + from + "PlusVote" + where + "validAfter" < strftime('%s', 'now') + order by + "year" desc, + "month" desc + limit + 1 + ) + group by + "votedId"; + ` + ).run(); +}; + +module.exports.down = function (db) { + db.prepare(`drop view "PlusTier"`).run(); + + db.prepare( + ` + create view "PlusTier" as + select + "votedId" as "userId", + case + when "passedVoting" = 0 + and "wasSuggested" = 1 then null + when "passedVoting" = 1 then "tier" + when "passedVoting" = 0 + and "tier" != 3 then "tier" + 1 + end "tier" + from + "PlusVotingResult" + where + year = ( + select + "year" + from + "PlusVote" + where + "validAfter" < strftime('%s', 'now') + order by + "year" desc, + "month" desc + limit + 1 + ) + and "month" = ( + select + "month" + from + "PlusVote" + where + "validAfter" < strftime('%s', 'now') + order by + "year" desc, + "month" desc + limit + 1 + ) + group by + "votedId"; + ` + ).run(); +};