Update model for new structure of email tables.

Fixes for triggers and syntax errors in fas.sql.
This commit is contained in:
Toshio Kuratomi 2008-03-05 14:01:31 -08:00
parent 895ccd09f0
commit 846a11e702
2 changed files with 136 additions and 123 deletions

View file

@ -55,11 +55,21 @@ get_engine()
# #
PeopleTable = Table('people', metadata, autoload=True) PeopleTable = Table('people', metadata, autoload=True)
PersonEmailsTable = Table('person_emails', metadata, autoload=True) # This is a view and therefore needs to have its key columns defined
PersonEmailsTable = Table('person_emailsv', metadata,
Column('id', Integer, primary_key=True),
Column('purpose', Unicode, primary_key=True),
Column('person_id', Integer, ForeignKey('people.id')),
autoload=True)
PersonRolesTable = Table('person_roles', metadata, autoload=True) PersonRolesTable = Table('person_roles', metadata, autoload=True)
ConfigsTable = Table('configs', metadata, autoload=True) ConfigsTable = Table('configs', metadata, autoload=True)
GroupsTable = Table('groups', metadata, autoload=True) GroupsTable = Table('groups', metadata, autoload=True)
GroupEmailsTable = Table('group_emails', metadata, autoload=True) # This is a view and therefore needs to have its key columns defined
GroupEmailsTable = Table('group_emailsv', metadata,
Column('id', Integer, primary_key=True),
Column('purpose', Unicode, primary_key=True),
Column('person_id', Integer, ForeignKey('groups.id')),
autoload=True)
GroupRolesTable = Table('group_roles', metadata, autoload=True) GroupRolesTable = Table('group_roles', metadata, autoload=True)
BugzillaQueueTable = Table('bugzilla_queue', metadata, autoload=True) BugzillaQueueTable = Table('bugzilla_queue', metadata, autoload=True)
LogTable = Table('log', metadata, autoload=True) LogTable = Table('log', metadata, autoload=True)
@ -449,5 +459,3 @@ mapper(Requests, RequestsTable, properties = {
mapper(Visit, visits_table) mapper(Visit, visits_table)
mapper(VisitIdentity, visit_identity_table, mapper(VisitIdentity, visit_identity_table,
properties=dict(users=relation(People, backref='visit_identity'))) properties=dict(users=relation(People, backref='visit_identity')))

View file

@ -81,17 +81,22 @@ CREATE TABLE person_emails (
create index person_emails_person_id_idx on person_emails(person_id); create index person_emails_person_id_idx on person_emails(person_id);
cluster person_emails_person_id_idx on person_emails; cluster person_emails_person_id_idx on person_emails;
CREATE TABLE purpose_emails ( CREATE TABLE email_purposes (
email_id INTEGER NOT NULL references emails(id), email_id INTEGER NOT NULL references person_emails(id),
person_id INTEGER NOT NULL references people(id), person_id INTEGER NOT NULL references people(id),
purpose text NOT NULL, purpose text NOT NULL,
primary key (person_id, purpose), primary key (person_id, purpose),
check (purpose ~ ('(bugzilla|primary|cla|pending|other[0-9]+)')) check (purpose ~ ('(bugzilla|primary|cla|pending|other[0-9]+)'))
); );
create index purpose_emails_email_id_idx on purpose_emails(email_id); create index email_purposes_email_id_idx on email_purposes(email_id);
create index purpose_emails_person_id_idx on purpose_emails(person_id); create index email_purposes_person_id_idx on email_purposes(person_id);
cluster purpose_emails_person_id_idx on purpose_emails; cluster email_purposes_person_id_idx on email_purposes;
-- Set up a view so we can get all the information about a person's emails.
create view person_emailsv (id, email, person_id, validtoken, description, verified, purpose) as (select pe.id, email, pe.person_id, validtoken,
description, verified, purpose from person_emails as pe,
email_purposes as pep where pe.id = pep.email_id);
CREATE TABLE configs ( CREATE TABLE configs (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
@ -145,24 +150,28 @@ CREATE TABLE group_emails (
group_id INTEGER NOT NULL references groups(id), group_id INTEGER NOT NULL references groups(id),
validtoken text, validtoken text,
description text, description text,
verified boolean NOT NULL DEFAULT false, verified boolean NOT NULL DEFAULT false
); );
create index group_emails_group_id_idx on group_emails(group_id); create index group_emails_group_id_idx on group_emails(group_id);
cluster group_emails_group_id_idx on group_emails; cluster group_emails_group_id_idx on group_emails;
CREATE TABLE group_purpose_emails ( CREATE TABLE group_email_purposes (
email_id INTEGER NOT NULL references emails(id), email_id INTEGER NOT NULL references group_emails(id),
group_id INTEGER NOT NULL references groups(id), group_id INTEGER NOT NULL references groups(id),
purpose text NOT NULL, purpose text NOT NULL,
primary key (person_id, purpose) primary key (group_id, purpose),
check (purpose ~ ('(bugzilla|primary|mailing list|other[0-9]+)')) check (purpose ~ ('(bugzilla|primary|mailing list|other[0-9]+)'))
); );
create index group_purpose_emails_email_id_idx on group_purpose_emails(email_id); create index group_email_purposes_email_id_idx on group_email_purposes(email_id);
create index group_purpose_emails_person_id_idx on group_purpose_emails(person_id); create index group_email_purposes_person_id_idx on group_email_purposes(group_id);
cluster group_purpose_emails_person_id_idx on group_purpose_emails; cluster group_email_purposes_person_id_idx on group_email_purposes;
-- Set up a view so we can get all the information about a group's emails.
create view group_emailsv (id, email, group_id, validtoken, description, verified, purpose) as (select ge.id, ge.email, ge.group_id, ge.validtoken,
ge.description, ge.verified, gep.purpose from group_emails as ge,
group_email_purposes as gep where ge.id = gep.email_id);
CREATE TABLE person_roles ( CREATE TABLE person_roles (
person_id INTEGER NOT NULL REFERENCES people(id), person_id INTEGER NOT NULL REFERENCES people(id),
@ -305,8 +314,8 @@ create or replace function bugzilla_sync() returns trigger as $bz_sync$
# 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,"
" purpose_emails as pue" " email_purposes as epu"
" where pee.id = pue.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'])
@ -342,112 +351,107 @@ create trigger role_bugzilla_sync before update or insert or delete
-- 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$
if TD['event'] == 'DELETE': -- # To port this we need to operate on two tables now
row = TD['old'] -- if TD['event'] == 'DELETE':
else: -- row = TD['old']
row = TD['new'] -- else:
-- row = TD['new']
if TD['event'] == 'UPDATE': --
if TD['old']['email'] == TD['new']['email']: -- if TD['event'] == 'UPDATE':
# Email has not changed. We do not care -- if TD['old']['email'] == TD['new']['email']:
return None -- # Email has not changed. We do not care
if row['purpose'] not in ('bugzilla', 'primary'): -- return None
# The change is to an email address that does not affect bugzilla -- if row['purpose'] not in ('bugzilla', 'primary'):
return None -- # The change is to an email address that does not affect bugzilla
elif row['purpose'] == 'primary': -- return None
# Check if there is a better email. -- elif row['purpose'] == 'primary':
plan = plpy.prepare("select email from person_emails where" -- # Check if there is a better email.
" purpose = 'bugzilla' 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)
# If the change is to primary but there is a bugzilla address, it -- if result:
# will have no effect. -- # If the change is to primary but there is a bugzilla address, it
return None -- # will have no effect.
# Check that the person belongs to fedorabugs -- return None
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, (row['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 --
-- # We now know that we have changes to make
# --
# Remove the old Email address -- #
# -- # Remove the old Email address
oldEmail = None -- #
if TD['event'] in ('DELETE', 'UPDATE'): -- oldEmail = None
oldEmail = TD['old']['email'] -- if TD['event'] in ('DELETE', 'UPDATE'):
elif row['purpose'] == 'bugzilla': -- oldEmail = TD['old']['email']
# Insert: check if there is an email for primary that this email is -- elif row['purpose'] == 'bugzilla':
# superceding -- # Insert: check if there is an email for primary that this email is
plan = plpy.prepare("select email from person_emails" -- # superceding
" where purpose = 'primary' and person_id = $1", ('text',)) -- plan = plpy.prepare("select email from person_emails"
result = plpy.execute(plan, (row['person_id'],), 1) -- " where purpose = 'primary' and person_id = $1", ('text',))
if result: -- result = plpy.execute(plan, (row['person_id'],), 1)
oldEmail = result[0]['email'] -- if result:
-- oldEmail = result[0]['email']
if oldEmail: --
plan = plpy.prepare("select email from bugzilla_queue where email = $1", -- if oldEmail:
('text',)) -- plan = plpy.prepare("select email from bugzilla_queue where email = $1",
result = plpy.execute(plan, oldEmail, 1) -- ('text',))
if result: -- result = plpy.execute(plan, oldEmail, 1)
plan = plpy.prepare("update bugzilla_queue set action = 'r'" -- if result:
" where email = $1", ('text',)) -- plan = plpy.prepare("update bugzilla_queue set action = 'r'"
plpy.execute(plan, (oldEmail)) -- " where email = $1", ('text',))
else: -- plpy.execute(plan, (oldEmail))
plan = plpy.prepare("insert into bugzilla_queue () values(email" -- else:
", group_id, person_id, action) values ($1, $2, $3, 'r')", -- plan = plpy.prepare("insert into bugzilla_queue () values(email"
('text', 'text', 'text')) -- ", group_id, person_id, action) values ($1, $2, $3, 'r')",
plpy.execute(plan, (oldEmail, row['group_id'], row['person_id'])) -- ('text', 'text', 'text'))
-- plpy.execute(plan, (oldEmail, row['group_id'], row['person_id']))
# --
# Add a new email address to bugzilla -- #
# -- # Add a new email address to bugzilla
newEmail = None -- #
if TD['event'] in ('INSERT', 'UPDATE'): -- newEmail = None
newEmail = TG['new'] -- if TD['event'] in ('INSERT', 'UPDATE'):
elif row['purpose'] == 'bugzilla': -- newEmail = TG['new']
# When deleting a bugzilla email, check if there is a primary to -- elif row['purpose'] == 'bugzilla':
# fallback on -- # When deleting a bugzilla email, check if there is a primary to
plan = plpy.prepare("select email from person_emails" -- # fallback on
" where purpose = 'primary' and person_id = $1", ('text',)) -- plan = plpy.prepare("select email from person_emails"
result = plpy.execute(plan, (row['person_id'],), 1) -- " where purpose = 'primary' and person_id = $1", ('text',))
if result: -- result = plpy.execute(plan, (row['person_id'],), 1)
newEmail = result[0]['email'] -- if result:
-- newEmail = result[0]['email']
if newEmail: --
plan = plpy.prepare("select email from bugzilla_queue where email = $1", -- if newEmail:
('text',)) -- plan = plpy.prepare("select email from bugzilla_queue where email = $1",
result = plpy.execute(plan, newEmail, 1) -- ('text',))
if result: -- result = plpy.execute(plan, newEmail, 1)
plan = plpy.prepare("update bugzilla_queue set action = 'a'" -- if result:
" where email = $1", ('text',)) -- plan = plpy.prepare("update bugzilla_queue set action = 'a'"
plpy.execute(plan, (newEmail)) -- " where email = $1", ('text',))
else: -- plpy.execute(plan, (newEmail))
plan = plpy.prepare("insert into bugzilla_queue () values(email" -- else:
", group_id, person_id, action) values ($1, $2, $3, 'a')", -- plan = plpy.prepare("insert into bugzilla_queue () values(email"
('text', 'text', 'text')) -- ", group_id, person_id, action) values ($1, $2, $3, 'a')",
plpy.execute(plan, (newEmail, row['group_id'], row['person_id'])) -- ('text', 'text', 'text'))
return None -- plpy.execute(plan, (newEmail, row['group_id'], row['person_id']))
$bz_sync_e$ language plpythonu; -- return None
-- $bz_sync_e$ language plpythonu;
create trigger email_bugzilla_sync before update or insert or delete --
on person_emails -- create trigger email_bugzilla_sync before update or insert or delete
for each row execute procedure bugzilla_sync_email(); -- 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, group_roles, group_emails, bugzilla_queue, configs, person_seq, visit, visit_identity, log, log_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 TO GROUP fedora;
-- For other services to connect to the necessary session tables
GRANT ALL ON TABLE visit, visit_identity TO GROUP apache;
-- For now other services would have to connect to the db to get auth
-- information so we need to allow select access on all these tables :-(
GRANT SELECT ON TABLE people, groups, person_roles, person_emails, group_roles, group_emails, configs TO GROUP apache;
-- Create default admin user - Default Password "admin" -- Create default admin user - Default Password "admin"
INSERT INTO people (username, human_name, password) VALUES ('admin', 'Admin User', '$1$djFfnacd$b6NFqFlac743Lb4sKWXj4/'); INSERT INTO people (username, human_name, password) VALUES ('admin', 'Admin User', '$1$djFfnacd$b6NFqFlac743Lb4sKWXj4/');
@ -461,4 +465,5 @@ INSERT INTO groups (name, display_name, owner_id, group_type) VALUES ('fedorabug
INSERT INTO person_roles (person_id, group_id, role_type, role_status, internal_comments, sponsor_id) VALUES ((SELECT id from people where username='admin'), (select id from groups where name='accounts'), 'administrator', 'approved', 'created at install time', (SELECT id from people where username='admin')); INSERT INTO person_roles (person_id, group_id, role_type, role_status, internal_comments, sponsor_id) VALUES ((SELECT id from people where username='admin'), (select id from groups where name='accounts'), 'administrator', 'approved', 'created at install time', (SELECT id from people where username='admin'));
-- Give admin user his email address -- Give admin user his email address
INSERT INTO person_emails (email, person_id, purpose, validtoken) VALUES ('root@localhost', (SELECT id from people where username='admin'), 'primary', 'valid'); INSERT INTO person_emails (email, person_id, verified) VALUES ('root@localhost', (SELECT id from people where username='admin'), true);
INSERT INTO email_purposes (email_id, person_id, purpose) VALUES ((SELECT id from person_emails where email='root@localhost'), (SELECT id from people where username='admin'), 'primary');