Reference OLD instead of NEW in trigger
authorMagnus Hagander <magnus@hagander.net>
Thu, 13 Aug 2020 12:11:52 +0000 (14:11 +0200)
committerMagnus Hagander <magnus@hagander.net>
Thu, 13 Aug 2020 12:11:52 +0000 (14:11 +0200)
The trigger is used for both UPDATE and DELETE, but in DELETE there is
no value in OLD.

Given that it only references the user_id field and this field cannot
change, we can just use OLD in both the UPDATE and DELETE case.

Back-patching in existing migration since it hasn't really been deployed
anywhere yet.

pgweb/account/migrations/0006_communityauth_sync.py

index 40870bdf9729502b050015ebc1219b6e2de06eb5..c06574717c49c216b51dbb2191cac04e6deb989c 100644 (file)
@@ -78,10 +78,10 @@ $$ language 'plpgsql'""",
             """CREATE FUNCTION account_secondaryemail_changetrack () RETURNS trigger AS $$
 BEGIN
    INSERT INTO account_communityauthchangelog (user_id, site_id, changedat)
-            SELECT NEW.user_id, s.id, CURRENT_TIMESTAMP
+            SELECT OLD.user_id, s.id, CURRENT_TIMESTAMP
             FROM account_communityauthsite s
             INNER JOIN account_communityauthlastlogin ll ON ll.site_id=s.id
-            WHERE s.push_changes AND ll.user_id=NEW.user_id
+            WHERE s.push_changes AND ll.user_id=OLD.user_id
     ON CONFLICT (user_id, site_id) DO UPDATE SET changedat=greatest(account_communityauthchangelog.changedat, CURRENT_TIMESTAMP);
    NOTIFY communityauth_changetrack;
    RETURN NEW;