From 2054799e43e2437b94f2d86981d21ec786d12a70 Mon Sep 17 00:00:00 2001 From: Stephanie Daugherty Date: Mon, 29 Aug 2011 14:30:15 -0700 Subject: Bug 637648 - Rename the "tags" table to "tag" r=LpSolit, a=LpSolit --- Bugzilla/Bug.pm | 8 ++++---- Bugzilla/DB.pm | 14 +++++++++++--- Bugzilla/DB/Schema.pm | 6 +++--- Bugzilla/DB/Sqlite.pm | 4 ++++ Bugzilla/Install/DB.pm | 20 ++++++++++++++++++-- Bugzilla/Search.pm | 4 ++-- Bugzilla/User.pm | 4 ++-- template/en/default/global/field-descs.none.tmpl | 2 +- 8 files changed, 45 insertions(+), 17 deletions(-) diff --git a/Bugzilla/Bug.pm b/Bugzilla/Bug.pm index 6fdab3645..8beecdcd2 100644 --- a/Bugzilla/Bug.pm +++ b/Bugzilla/Bug.pm @@ -2918,10 +2918,10 @@ sub add_tag { my $tag_id = $user->tags->{$tag}->{id}; # If this tag doesn't exist for this user yet, create it. if (!$tag_id) { - $dbh->do('INSERT INTO tags (user_id, name) VALUES (?, ?)', + $dbh->do('INSERT INTO tag (user_id, name) VALUES (?, ?)', undef, ($user->id, $tag)); - $tag_id = $dbh->selectrow_array('SELECT id FROM tags + $tag_id = $dbh->selectrow_array('SELECT id FROM tag WHERE name = ? AND user_id = ?', undef, ($tag, $user->id)); # The list has changed. @@ -2957,7 +2957,7 @@ sub remove_tag { # Decrement the counter, and delete the tag if no bugs are using it anymore. if (!--$user->tags->{$tag}->{bug_count}) { - $dbh->do('DELETE FROM tags WHERE name = ? AND user_id = ?', + $dbh->do('DELETE FROM tag WHERE name = ? AND user_id = ?', undef, ($tag, $user->id)); # The list has changed. @@ -2974,7 +2974,7 @@ sub tags { if (!exists $self->{tags}) { $self->{tags} = $dbh->selectcol_arrayref( 'SELECT name FROM bug_tag - INNER JOIN tags ON tags.id = bug_tag.tag_id + INNER JOIN tag ON tag.id = bug_tag.tag_id WHERE bug_id = ? AND user_id = ?', undef, ($self->id, $user->id)); } diff --git a/Bugzilla/DB.pm b/Bugzilla/DB.pm index 8d1cf32a0..a537d6131 100644 --- a/Bugzilla/DB.pm +++ b/Bugzilla/DB.pm @@ -93,6 +93,12 @@ use constant FULLTEXT_OR => ''; use constant WORD_START => '(^|[^[:alnum:]])'; use constant WORD_END => '($|[^[:alnum:]])'; +# On most databases, in order to drop an index, you have to first drop +# the foreign keys that use that index. However, on some databases, +# dropping the FK immediately before dropping the index causes problems +# and doesn't need to be done anyway, so those DBs set this to 0. +use constant INDEX_DROPS_REQUIRE_FK_DROPS => 1; + ##################################################################### # Overridden Superclass Methods ##################################################################### @@ -947,9 +953,11 @@ sub bz_drop_index { my $index_exists = $self->bz_index_info($table, $name); if ($index_exists) { - # We cannot delete an index used by a FK. - foreach my $column (@{$index_exists->{FIELDS}}) { - $self->bz_drop_related_fks($table, $column); + if ($self->INDEX_DROPS_REQUIRE_FK_DROPS) { + # We cannot delete an index used by a FK. + foreach my $column (@{$index_exists->{FIELDS}}) { + $self->bz_drop_related_fks($table, $column); + } } $self->bz_drop_index_raw($table, $name); $self->_bz_real_schema->delete_index($table, $name); diff --git a/Bugzilla/DB/Schema.pm b/Bugzilla/DB/Schema.pm index fb62965e3..33527c367 100644 --- a/Bugzilla/DB/Schema.pm +++ b/Bugzilla/DB/Schema.pm @@ -1000,7 +1000,7 @@ use constant ABSTRACT_SCHEMA => { ], }, - tags => { + tag => { FIELDS => [ id => {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1}, name => {TYPE => 'varchar(64)', NOTNULL => 1}, @@ -1010,7 +1010,7 @@ use constant ABSTRACT_SCHEMA => { DELETE => 'CASCADE'}}, ], INDEXES => [ - tags_user_id_idx => {FIELDS => [qw(user_id name)], TYPE => 'UNIQUE'}, + tag_user_id_idx => {FIELDS => [qw(user_id name)], TYPE => 'UNIQUE'}, ], }, @@ -1021,7 +1021,7 @@ use constant ABSTRACT_SCHEMA => { COLUMN => 'bug_id', DELETE => 'CASCADE'}}, tag_id => {TYPE => 'INT3', NOTNULL => 1, - REFERENCES => {TABLE => 'tags', + REFERENCES => {TABLE => 'tag', COLUMN => 'id', DELETE => 'CASCADE'}}, ], diff --git a/Bugzilla/DB/Sqlite.pm b/Bugzilla/DB/Sqlite.pm index e40a264f0..fab1e3c87 100644 --- a/Bugzilla/DB/Sqlite.pm +++ b/Bugzilla/DB/Sqlite.pm @@ -39,6 +39,10 @@ use constant ISOLATION_LEVEL => undef; use constant WORD_START => '(?:^|\W)'; use constant WORD_END => '(?:$|\W)'; +# For some reason, dropping the related FKs causes the index to +# disappear early, which causes all sorts of problems. +use constant INDEX_DROPS_REQUIRE_FK_DROPS => 0; + #################################### # Functions Added To SQLite Itself # #################################### diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index af276882c..5ce3c7a4e 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -646,6 +646,8 @@ sub update_table_definitions { $dbh->bz_add_column('bug_see_also', 'id', {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1}); + _rename_tags_to_tag(); + # 2011-01-29 LpSolit@gmail.com - Bug 616185 _migrate_user_tags(); @@ -3485,9 +3487,9 @@ sub _migrate_user_tags { WHERE query_type != 0'); my $sth_tags = $dbh->prepare( - 'INSERT INTO tags (user_id, name) VALUES (?, ?)'); + 'INSERT INTO tag (user_id, name) VALUES (?, ?)'); my $sth_tag_id = $dbh->prepare( - 'SELECT id FROM tags WHERE user_id = ? AND name = ?'); + 'SELECT id FROM tag WHERE user_id = ? AND name = ?'); my $sth_bug_tag = $dbh->prepare('INSERT INTO bug_tag (bug_id, tag_id) VALUES (?, ?)'); my $sth_nq = $dbh->prepare('UPDATE namedqueries SET query = ? @@ -3586,6 +3588,20 @@ sub _migrate_disabledtext_boolean { } } +sub _rename_tags_to_tag { + my $dbh = Bugzilla->dbh; + if ($dbh->bz_table_info('tags')) { + # If we get here, it's because the schema created "tag" as an empty + # table while "tags" still exists. We get rid of the empty + # tag table so we can do the rename over the top of it. + $dbh->bz_drop_table('tag'); + $dbh->bz_drop_index('tags', 'tags_user_id_idx'); + $dbh->bz_rename_table('tags','tag'); + $dbh->bz_add_index('tag', 'tag_user_id_idx', + {FIELDS => [qw(user_id name)], TYPE => 'UNIQUE'}); + } +} + 1; __END__ diff --git a/Bugzilla/Search.pm b/Bugzilla/Search.pm index a5c3e032d..d47e0ae99 100644 --- a/Bugzilla/Search.pm +++ b/Bugzilla/Search.pm @@ -2525,8 +2525,8 @@ sub _multiselect_table { " ON keywords.keywordid = keyworddefs.id"; } elsif ($field eq 'tag') { - $args->{full_field} = 'tags.name'; - return "bug_tag INNER JOIN tags ON bug_tag.tag_id = tags.id" + $args->{full_field} = 'tag.name'; + return "bug_tag INNER JOIN tag ON bug_tag.tag_id = tag.id" . " AND user_id = " . $self->_user->id; } elsif ($field eq 'bug_group') { diff --git a/Bugzilla/User.pm b/Bugzilla/User.pm index 3a3edcb5b..ea186a0fd 100644 --- a/Bugzilla/User.pm +++ b/Bugzilla/User.pm @@ -423,8 +423,8 @@ sub tags { # in which case there are no bugs with this tag yet. $self->{tags} = $dbh->selectall_hashref( 'SELECT name, id, COUNT(bug_id) AS bug_count - FROM tags - LEFT JOIN bug_tag ON bug_tag.tag_id = tags.id + FROM tag + LEFT JOIN bug_tag ON bug_tag.tag_id = tag.id WHERE user_id = ? ' . $dbh->sql_group_by('id', 'name'), 'name', undef, $self->id); } diff --git a/template/en/default/global/field-descs.none.tmpl b/template/en/default/global/field-descs.none.tmpl index 130ad82c6..21f41c86c 100644 --- a/template/en/default/global/field-descs.none.tmpl +++ b/template/en/default/global/field-descs.none.tmpl @@ -133,7 +133,7 @@ "settings" => "Settings", "short_desc" => "Summary", "status_whiteboard" => "Whiteboard", - "tags.name" => "Tags", + "tag.name" => "Tags", "target_milestone" => "Target Milestone", "version" => "Version", "work_time" => "Hours Worked", -- cgit v1.2.3-24-g4f1b