From 3bc6ea42732020b00fef53a9b556e4a37e591bd7 Mon Sep 17 00:00:00 2001 From: "mkanat%bugzilla.org" <> Date: Sat, 10 Mar 2007 04:13:09 +0000 Subject: Bug 347439: Implement support for referential integrity in Bugzilla::DB::Schema and implement it on profiles_activity Patch By Max Kanat-Alexander (module owner) a=mkanat --- Bugzilla/Install/DB.pm | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'Bugzilla/Install/DB.pm') diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index 00032d15b..5e6401828 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -49,6 +49,39 @@ sub indicate_progress { } } +# This is used before adding a foreign key to a column, to make sure +# that the database won't fail adding the key. +sub check_references { + my ($table, $column, $foreign_table, $foreign_column) = @_; + my $dbh = Bugzilla->dbh; + + my $bad_values = $dbh->selectcol_arrayref( + "SELECT DISTINCT $table.$column + FROM $table LEFT JOIN $foreign_table + ON $table.$column = $foreign_table.$foreign_column + WHERE $foreign_table.$foreign_column IS NULL"); + + if (@$bad_values) { + my $values = join(', ', @$bad_values); + print <bz_add_column('milestones', 'id', {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1}); + # Referential Integrity begins here + check_references('profiles_activity', 'userid', 'profiles', 'userid'); + $dbh->bz_alter_column('profiles_activity', 'userid', + {TYPE => 'INT3', NOTNULL => 1, REFERENCES => + {TABLE => 'profiles', COLUMN => 'userid', DELETE => 'CASCADE'}}); + check_references('profiles_activity', 'who', 'profiles', 'userid'); + $dbh->bz_alter_column('profiles_activity', 'who', + {TYPE => 'INT3', NOTNULL => 1, REFERENCES => + {TABLE => 'profiles', COLUMN => 'userid'}}); + check_references('profiles_activity', 'fieldid', 'fielddefs', 'id'); + $dbh->bz_alter_column('profiles_activity', 'fieldid', + {TYPE => 'INT3', NOTNULL => 1, REFERENCES => + {TABLE => 'fielddefs', COLUMN => 'id'}}); + ################################################################ # New --TABLE-- changes should go *** A B O V E *** this point # ################################################################ -- cgit v1.2.3-24-g4f1b