Implement UserOperation_donor_update view and its triggers

Note my comment that the error messages don't work properly on NULL
values.  I once in my life spent years wondering about how to handle
three-valued logic problems in SQL.  I thought that part of my life
was apparently over, but I guess not.  🤷
This commit is contained in:
Bradley M. Kuhn 2022-06-22 10:53:05 -07:00
parent fe0cda3232
commit 72f2b5aebd

View file

@ -1,4 +1,4 @@
-- Conservancy Supporter Database, Version 0.2
-- Conservancy Supporter Database, Version 0.4
DROP TABLE IF EXISTS "donor";
@ -202,3 +202,73 @@ DELETE FROM _temp_request_configuration_change;
END;
-- -------------- END VIEW: UserOperation_fix_tshirt_size_request ---------------------
-- -------------- VIEW: UserOperation_donor_update ---------------------
DROP VIEW IF EXISTS UserOperation_donor_update;
CREATE VIEW UserOperation_donor_update AS
SELECT donor.ledger_entity_id as donor_name, donor.rt_ticket as ticket, donor.display_name as public_name,
donor.public_ack as public_acknowledge, donor.is_supporter as is_sustainer, donor.id as donor_id
FROM donor
ORDER BY donor.ledger_entity_id, donor.id;
DROP TRIGGER IF EXISTS donor_update_display_name;
CREATE TRIGGER donor_update_display_name
INSTEAD OF UPDATE OF public_name on UserOperation_donor_update
BEGIN
UPDATE donor SET display_name = NEW.public_name
WHERE id = OLD.donor_id
AND (display_name = OLD.public_name OR display_name is NULL);
-- FIXME: I think this error message doesn't properly display when NEW.ticket
-- is NOT NULL and the select returns a NULL
SELECT CASE
WHEN ( (SELECT display_name FROM donor WHERE id = OLD.donor_id) != NEW.public_name )
THEN RAISE(FAIL, "Error encountered while updating public_name; please verify your changes worked!")
END;
END;
DROP TRIGGER IF EXISTS donor_update_rt_ticket;
CREATE TRIGGER donor_update_rt_ticket
INSTEAD OF UPDATE OF ticket on UserOperation_donor_update
BEGIN
UPDATE donor SET rt_ticket = NEW.ticket
WHERE id = OLD.donor_id
AND (rt_ticket = OLD.ticket or rt_ticket is NULL);
-- FIXME: I think this error message doesn't properly display when NEW.ticket
-- is NOT NULL and the select returns a NULL
SELECT CASE
WHEN ( (SELECT rt_ticket FROM donor WHERE id = OLD.donor_id) != NEW.ticket )
THEN RAISE(FAIL, "Error encountered while updating RT ticket; please verify your changes worked!")
END;
END;
DROP TRIGGER IF EXISTS donor_update_public_acknowledgment;
-- FIXME: This trigger does *not* add 'public-ack-change-N' request, as it should. I was not sure *why*
-- I'd kept track of public-ack changes in the request table anyway, so I did not implement that here.
CREATE TRIGGER donor_update_public_acknowledgment
INSTEAD OF UPDATE OF public_acknowledge on UserOperation_donor_update
BEGIN
UPDATE donor SET public_ack = NEW.public_acknowledge
WHERE id = OLD.donor_id
AND (public_ack = OLD.public_acknowledge OR public_ack is NULL);
-- FIXME: I think this error message doesn't properly display when NEW.ticket
-- is NOT NULL and the select returns a NULL
SELECT CASE
WHEN ( (SELECT public_ack FROM donor WHERE id = OLD.donor_id) != NEW.public_acknowledge )
THEN RAISE(FAIL, "Error encountered while updating public_acknowledge setting; please verify your changes worked!")
END;
END;
-- -------------- END VIEW: UserOperation_donor_update ---------------------