* Work in progress triggers.

This commit is contained in:
Toshio Kuratomi 2008-03-11 08:01:36 -07:00
parent f9b2dd8825
commit 427064a8c2

View file

@ -282,168 +282,217 @@ create table visit_identity (
-- --
-- When a person's fedorabugs role is updated, add them to bugzilla queue. -- When a person's fedorabugs role is updated, add them to bugzilla queue.
-- --
-- create or replace function bugzilla_sync() returns trigger as $bz_sync$ create or replace function bugzilla_sync() returns trigger as $bz_sync$
-- # Decide which row we are operating on and the action to take # Decide which row we are operating on and the action to take
-- if TD['event'] == 'DELETE': if TD['event'] == 'DELETE':
-- # 'r' for removing an entry from bugzilla # 'r' for removing an entry from bugzilla
-- newaction = 'r' newaction = 'r'
-- row = TD['old'] row = TD['old']
-- else: else:
-- # insert or update # insert or update
-- row = TD['new'] row = TD['new']
-- if row['role_status'] == 'approved': if row['role_status'] == 'approved':
-- # approved so add an entry to bugzilla # approved so add an entry to bugzilla
-- newaction = 'a' newaction = 'a'
-- else: else:
-- # no longer approved so remove the entry from bugzilla # no longer approved so remove the entry from bugzilla
-- newaction = 'r' newaction = 'r'
--
-- # Get the group id for fedorabugs # Get the group id for fedorabugs
-- result = plpy.execute("select id from groups where name = 'fedorabugs'", 1) result = plpy.execute("select id from groups where name = 'fedorabugs'", 1)
-- if not result: if not result:
-- # Danger Will Robinson! A basic FAS group does not exist! # Danger Will Robinson! A basic FAS group does not exist!
-- plpy.error('Basic FAS group fedorabugs does not exist') plpy.error('Basic FAS group fedorabugs does not exist')
-- # If this is not a fedorabugs role, no change needed # If this is not a fedorabugs role, no change needed
-- if row['group_id'] != result[0]['id']: if row['group_id'] != result[0]['id']:
-- return None return None
--
-- # Retrieve the bugzilla email address # Retrieve the bugzilla email address
-- plan = plpy.prepare("select email, purpose from person_emails as pee," plan = plpy.prepare("select email, purpose from person_emails as pee,"
-- " email_purposes as epu" " email_purposes as epu"
-- " where epu.id = epu.email_id and pee.person_id = $1" " where epu.id = epu.email_id and pee.person_id = $1"
-- " and purpose in ('bugzilla', 'primary')", " and purpose in ('bugzilla', 'primary')",
-- ('text',)) ('text',))
-- result = plpy.execute(plan, row['person_id']) result = plpy.execute(plan, row['person_id'])
-- email = None email = None
-- for record in result: for record in result:
-- email = record['email'] email = record['email']
-- if record['purpose'] == 'bugzilla': if record['purpose'] == 'bugzilla':
-- break break
-- if not email: if not email:
-- raise plpy.error('Cannot approve fedorabugs for person_id(%s) because they have no email address to use with bugzilla' % row['person_id']) raise plpy.error('Cannot approve fedorabugs for person_id(%s) because they have no email address to use with bugzilla' % row['person_id'])
--
-- # If there is already a row in bugzilla_queue update, otherwise insert # If there is already a row in bugzilla_queue update, otherwise insert
-- plan = plpy.prepare("select email from bugzilla_queue where email = $1", plan = plpy.prepare("select email from bugzilla_queue where email = $1",
-- ('text',)) ('text',))
-- result = plpy.execute(plan, (email,), 1) result = plpy.execute(plan, (email,), 1)
-- if result: if result:
-- plan = plpy.prepare("update bugzilla_queue set action = $1" plan = plpy.prepare("update bugzilla_queue set action = $1"
-- " where email = $2", ('char', 'text')) " where email = $2", ('char', 'text'))
-- plpy.execute(plan, (newaction, email)) plpy.execute(plan, (newaction, email))
-- else: else:
-- plan = plpy.prepare("insert into bugzilla_queue (email, group_id" plan = plpy.prepare("insert into bugzilla_queue (email, group_id"
-- ", person_id, action) values ($1, $2, $3, $4)", ", person_id, action) values ($1, $2, $3, $4)",
-- ('text', 'text', 'text', 'char')) ('text', 'text', 'text', 'char'))
-- plpy.execute(plan, (email, row['group_id'], row['person_id'], newaction)) plpy.execute(plan, (email, row['group_id'], row['person_id'], newaction))
-- return None return None
-- $bz_sync$ language plpythonu; $bz_sync$ language plpythonu;
--
-- create trigger role_bugzilla_sync before update or insert or delete create trigger role_bugzilla_sync before update or insert or delete
-- on person_roles on person_roles
-- for each row execute procedure bugzilla_sync(); for each row execute procedure bugzilla_sync();
-- --
-- When an email address changes, check whether it needs to be changed in -- When an email address changes, check whether it needs to be changed in
-- bugzilla as well. -- bugzilla as well.
-- --
-- create or replace function bugzilla_sync_email() returns trigger AS $bz_sync_e$ create or replace function bugzilla_sync_email() returns trigger AS $bz_sync_e$
-- # To port this we need to operate on two tables now if TD['new']['email'] != TD['old']['email']:
-- if TD['event'] == 'DELETE': pass
-- row = TD['old'] # if person new in fedorabugs and email_purpose == bz or (email_purpose == primary and no bz email)
-- else: # delete old email
-- row = TD['new'] # add new email
-- if TD['new']['person_id'] != TD['old']['person_id']:
-- if TD['event'] == 'UPDATE': pass
-- if TD['old']['email'] == TD['new']['email']: # Check that email_purpose == bz or (email_purpose == primary and no bz email)
-- # Email has not changed. We do not care # if person old in fedorabugs and person new not in fedorabugs
-- return None # delete old email
-- if row['purpose'] not in ('bugzilla', 'primary'): # if person old not in fedorabugs and person new in fedorabugs
-- # The change is to an email address that does not affect bugzilla # add new email
-- return None if TD['new']['verified'] != TD['old']['verified']:
-- elif row['purpose'] == 'primary': pass
-- # Check if there is a better email. ### FIXME: We are here
-- plan = plpy.prepare("select email from person_emails where" # if person new in fedorabugs and
-- " purpose = 'bugzilla' and person_id = $1", ('text',))
-- result = plpy.execute(plan, (row['person_id'],), 1) ### FIXME: All below here has to be merged into the above structure
-- if result: # Retrieve the purpose
-- # If the change is to primary but there is a bugzilla address, it plan = plpy.prepare("select purpose from email_purposes where"
-- # will have no effect. " email_id = $1", ('text',))
-- return None result = plpy.execute(plan, (TD['new']['id']))
-- # Check that the person belongs to fedorabugs
-- plan = plpy.prepare("select * from people as p, person_roles as r," # Check that the person belongs to fedorabugs
-- " groups as g where p.id = r.person_id and r.group_id = g.id" plan = plpy.prepare("select * from people as p, person_roles as r,"
-- " and r.role_status = 'approved' and g.name = 'fedorabugs'" " groups as g where p.id = r.person_id and r.group_id = g.id"
-- " and p.id = $1", ('text',)) " and r.role_status = 'approved' and g.name = 'fedorabugs'"
-- result = plpy.execute(plan, (row['person_id'],), 1) " and p.id = $1", ('text',))
-- if not result: result = plpy.execute(plan, (TD['new']['person_id'],), 1)
-- # Person does not belong to fedorabugs so this will have no effect. if not result:
-- return None # Person does not belong to fedorabugs so this will have no effect.
-- return None
-- # We now know that we have changes to make
-- $bz_sync_e$ language plpythonu;
-- #
-- # Remove the old Email address create trigger email_bugzilla_sync before update
-- # on person_emails
-- oldEmail = None for each row execute procedure bugzilla_sync_email();
-- if TD['event'] in ('DELETE', 'UPDATE'):
-- oldEmail = TD['old']['email']
-- elif row['purpose'] == 'bugzilla': create or replace function bugzilla_sync_purpose() returns trigger AS
-- # Insert: check if there is an email for primary that this email is $bz_sync_p$
-- # superceding ### FIXME: This trigger needs a complete rewrite.
-- plan = plpy.prepare("select email from person_emails" # Genericize a row so we can access things that would be in either
-- " where purpose = 'primary' and person_id = $1", ('text',)) if TD['event'] == 'DELETE':
-- result = plpy.execute(plan, (row['person_id'],), 1) row = TD['old']
-- if result: else:
-- oldEmail = result[0]['email'] row = TD['new']
--
-- if oldEmail: # Check that the person belongs to fedorabugs
-- plan = plpy.prepare("select email from bugzilla_queue where email = $1", plan = plpy.prepare("select * from people as p, person_roles as r,"
-- ('text',)) " groups as g where p.id = r.person_id and r.group_id = g.id"
-- result = plpy.execute(plan, oldEmail, 1) " and r.role_status = 'approved' and g.name = 'fedorabugs'"
-- if result: " and p.id = $1", ('text',))
-- plan = plpy.prepare("update bugzilla_queue set action = 'r'" result = plpy.execute(plan, (row['person_id'],), 1)
-- " where email = $1", ('text',)) if not result:
-- plpy.execute(plan, (oldEmail)) # Person does not belong to fedorabugs so this will have no effect.
-- else: return None
-- plan = plpy.prepare("insert into bugzilla_queue () values(email"
-- ", group_id, person_id, action) values ($1, $2, $3, 'r')", # Check that a change has occurred:
-- ('text', 'text', 'text')) # if email in
-- plpy.execute(plan, (oldEmail, row['group_id'], row['person_id']))
-- # To port this we need to operate on two tables now
-- #
-- # Add a new email address to bugzilla if TD['event'] == 'UPDATE':
-- # if TD['old']['email'] == TD['new']['email']:
-- newEmail = None # Email has not changed. We do not care
-- if TD['event'] in ('INSERT', 'UPDATE'): return None
-- newEmail = TG['new'] if row['purpose'] not in ('bugzilla', 'primary'):
-- elif row['purpose'] == 'bugzilla': # The change is to an email address that does not affect bugzilla
-- # When deleting a bugzilla email, check if there is a primary to return None
-- # fallback on elif row['purpose'] == 'primary':
-- plan = plpy.prepare("select email from person_emails" # Check if there is a better email.
-- " where purpose = 'primary' and person_id = $1", ('text',)) plan = plpy.prepare("select email from person_emails where"
-- result = plpy.execute(plan, (row['person_id'],), 1) " purpose = 'bugzilla' and person_id = $1", ('text',))
-- if result: result = plpy.execute(plan, (row['person_id'],), 1)
-- newEmail = result[0]['email'] if result:
-- # If the change is to primary but there is a bugzilla address, it
-- if newEmail: # will have no effect.
-- plan = plpy.prepare("select email from bugzilla_queue where email = $1", return None
-- ('text',))
-- result = plpy.execute(plan, newEmail, 1) # We now know that we have changes to make
-- if result:
-- plan = plpy.prepare("update bugzilla_queue set action = 'a'" #
-- " where email = $1", ('text',)) # Remove the old Email address
-- plpy.execute(plan, (newEmail)) #
-- else: oldEmail = None
-- plan = plpy.prepare("insert into bugzilla_queue () values(email" if TD['event'] in ('DELETE', 'UPDATE'):
-- ", group_id, person_id, action) values ($1, $2, $3, 'a')", oldEmail = TD['old']['email']
-- ('text', 'text', 'text')) elif row['purpose'] == 'bugzilla':
-- plpy.execute(plan, (newEmail, row['group_id'], row['person_id'])) # Insert: check if there is an email for primary that this email is
-- return None # superceding
-- $bz_sync_e$ language plpythonu; plan = plpy.prepare("select email from person_emails"
-- " where purpose = 'primary' and person_id = $1", ('text',))
-- create trigger email_bugzilla_sync before update or insert or delete result = plpy.execute(plan, (row['person_id'],), 1)
-- on person_emails if result:
-- for each row execute procedure bugzilla_sync_email(); oldEmail = result[0]['email']
if oldEmail:
plan = plpy.prepare("select email from bugzilla_queue where email = $1",
('text',))
result = plpy.execute(plan, oldEmail, 1)
if result:
plan = plpy.prepare("update bugzilla_queue set action = 'r'"
" where email = $1", ('text',))
plpy.execute(plan, (oldEmail))
else:
plan = plpy.prepare("insert into bugzilla_queue () values(email"
", group_id, person_id, action) values ($1, $2, $3, 'r')",
('text', 'text', 'text'))
plpy.execute(plan, (oldEmail, row['group_id'], row['person_id']))
#
# Add a new email address to bugzilla
#
newEmail = None
if TD['event'] in ('INSERT', 'UPDATE'):
newEmail = TG['new']
elif row['purpose'] == 'bugzilla':
# When deleting a bugzilla email, check if there is a primary to
# fallback on
plan = plpy.prepare("select email from person_emails"
" where purpose = 'primary' and person_id = $1", ('text',))
result = plpy.execute(plan, (row['person_id'],), 1)
if result:
newEmail = result[0]['email']
if newEmail:
plan = plpy.prepare("select email from bugzilla_queue where email = $1",
('text',))
result = plpy.execute(plan, newEmail, 1)
if result:
plan = plpy.prepare("update bugzilla_queue set action = 'a'"
" where email = $1", ('text',))
plpy.execute(plan, (newEmail))
else:
plan = plpy.prepare("insert into bugzilla_queue () values(email"
", group_id, person_id, action) values ($1, $2, $3, 'a')",
('text', 'text', 'text'))
plpy.execute(plan, (newEmail, row['group_id'], row['person_id']))
return None
$bz_sync_p$ language plpythonu;
create trigger email_bugzilla_sync before update or insert or delete
on person_emails
for each row execute procedure bugzilla_sync_email();
-- For Fas to connect to the database -- For Fas to connect to the database
GRANT ALL ON TABLE people, groups, person_roles, person_emails, email_purposes, group_roles, group_emails, group_email_purposes, bugzilla_queue, configs, person_seq, visit, visit_identity, log, log_id_seq, person_emails_id_seq, group_emails_id_seq TO GROUP fedora; GRANT ALL ON TABLE people, groups, person_roles, person_emails, email_purposes, group_roles, group_emails, group_email_purposes, bugzilla_queue, configs, person_seq, visit, visit_identity, log, log_id_seq, person_emails_id_seq, group_emails_id_seq TO GROUP fedora;