From 4abf66cf44c7e5004b7838a9de2ed9f02136ac1a Mon Sep 17 00:00:00 2001 From: albert Date: Fri, 14 Oct 2011 14:28:04 -0400 Subject: [PATCH] fixes #123: Tags aren't alphabetized --- app/models/note.rb | 2 +- app/models/post.rb | 1 + db/migrate/20100204214746_create_posts.rb | 1 + .../20100211191709_create_tag_aliases.rb | 2 +- .../20100215223541_create_post_votes.rb | 3 + db/migrate/20100224171915_create_notes.rb | 6 +- ...20100307073438_create_tag_subscriptions.rb | 1 + .../20110607194023_create_pool_versions.rb | 2 + script/upgrade_schema.sql | 216 +++++++++++++++++- 9 files changed, 228 insertions(+), 6 deletions(-) diff --git a/app/models/note.rb b/app/models/note.rb index 0eb048bcf..a9f553588 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -13,7 +13,7 @@ class Note < ActiveRecord::Base validate :post_must_not_be_note_locked attr_accessible :x, :y, :width, :height, :body, :updater_id, :updater_ip_addr, :is_active, :post_id, :html_id scope :active, where("is_active = TRUE") - scope :body_matches, lambda {|query| where("text_index @@ plainto_tsquery(?)", query.scan(/\S+/).join(" & "))} + scope :body_matches, lambda {|query| where("body_index @@ plainto_tsquery(?)", query.scan(/\S+/).join(" & "))} scope :post_tag_match, lambda {|query| joins(:post).where("posts.tag_index @@ to_tsquery('danbooru', ?)", query)} search_methods :body_matches, :post_tag_match diff --git a/app/models/post.rb b/app/models/post.rb index 630b71930..a13c5b7f3 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -425,6 +425,7 @@ class Post < ActiveRecord::Base normalized_tags = TagImplication.with_descendants(normalized_tags) normalized_tags = filter_metatags(normalized_tags) normalized_tags = %w(tagme) if normalized_tags.empty? + normalized_tags.sort! set_tag_string(normalized_tags.uniq.join(" ")) end diff --git a/db/migrate/20100204214746_create_posts.rb b/db/migrate/20100204214746_create_posts.rb index 8d37ee7a7..e0b2b8208 100644 --- a/db/migrate/20100204214746_create_posts.rb +++ b/db/migrate/20100204214746_create_posts.rb @@ -33,6 +33,7 @@ class CreatePosts < ActiveRecord::Migration # Cached t.column :last_noted_at, :datetime t.column :last_commented_at, :datetime + t.column :fav_count, :integer, :null => false, :default => 0 # Tags t.column :tag_string, :text, :null => false, :default => "" diff --git a/db/migrate/20100211191709_create_tag_aliases.rb b/db/migrate/20100211191709_create_tag_aliases.rb index 7b943c3e4..791591bd6 100644 --- a/db/migrate/20100211191709_create_tag_aliases.rb +++ b/db/migrate/20100211191709_create_tag_aliases.rb @@ -4,7 +4,7 @@ class CreateTagAliases < ActiveRecord::Migration t.column :antecedent_name, :string, :null => false t.column :consequent_name, :string, :null => false t.column :creator_id, :integer, :null => false - t.column :creator_ip_addr, :inet, :null => false + t.column :creator_ip_addr, "inet", :null => false t.column :forum_topic_id, :integer t.column :status, :text, :null => false, :default => "pending" t.timestamps diff --git a/db/migrate/20100215223541_create_post_votes.rb b/db/migrate/20100215223541_create_post_votes.rb index 1f3dc6b7c..e790f690b 100644 --- a/db/migrate/20100215223541_create_post_votes.rb +++ b/db/migrate/20100215223541_create_post_votes.rb @@ -6,6 +6,9 @@ class CreatePostVotes < ActiveRecord::Migration t.column :score, :integer, :null => false t.timestamps end + + add_index :post_votes, :post_id + add_index :post_votes, :user_id end def self.down diff --git a/db/migrate/20100224171915_create_notes.rb b/db/migrate/20100224171915_create_notes.rb index 696c13233..235263b5a 100644 --- a/db/migrate/20100224171915_create_notes.rb +++ b/db/migrate/20100224171915_create_notes.rb @@ -9,14 +9,14 @@ class CreateNotes < ActiveRecord::Migration t.column :height, :integer, :null => false t.column :is_active, :boolean, :null => false, :default => true t.column :body, :text, :null => false - t.column :text_index, "tsvector", :null => false + t.column :body_index, "tsvector", :null => false t.timestamps end add_index :notes, :creator_id add_index :notes, :post_id - execute "CREATE INDEX index_notes_on_text_index ON notes USING GIN (text_index)" - execute "CREATE TRIGGER trigger_notes_on_update BEFORE INSERT OR UPDATE ON notes FOR EACH ROW EXECUTE PROCEDURE tsvector_update_trigger('text_index', 'pg_catalog.english', 'body')" + execute "CREATE INDEX index_notes_on_body_index ON notes USING GIN (body_index)" + execute "CREATE TRIGGER trigger_notes_on_update BEFORE INSERT OR UPDATE ON notes FOR EACH ROW EXECUTE PROCEDURE tsvector_update_trigger('body_index', 'pg_catalog.english', 'body')" end def self.down diff --git a/db/migrate/20100307073438_create_tag_subscriptions.rb b/db/migrate/20100307073438_create_tag_subscriptions.rb index 204e84f65..02bebb969 100644 --- a/db/migrate/20100307073438_create_tag_subscriptions.rb +++ b/db/migrate/20100307073438_create_tag_subscriptions.rb @@ -7,6 +7,7 @@ class CreateTagSubscriptions < ActiveRecord::Migration t.column :post_ids, :text, :null => false t.column :is_public, :boolean, :null => false, :default => true t.column :last_accessed_at, :datetime + t.column :is_opted_in, :boolean, :null => false, :default => false t.timestamps end diff --git a/db/migrate/20110607194023_create_pool_versions.rb b/db/migrate/20110607194023_create_pool_versions.rb index 9717ec57a..194b7595e 100644 --- a/db/migrate/20110607194023_create_pool_versions.rb +++ b/db/migrate/20110607194023_create_pool_versions.rb @@ -9,6 +9,8 @@ class CreatePoolVersions < ActiveRecord::Migration end add_index :pool_versions, :pool_id + add_index :pool_versions, :updater_id + add_index :pool_versions, :updater_ip_addr end def down diff --git a/script/upgrade_schema.sql b/script/upgrade_schema.sql index 339bcb8ad..b7015bac4 100644 --- a/script/upgrade_schema.sql +++ b/script/upgrade_schema.sql @@ -106,8 +106,8 @@ CREATE INDEX index_dmails_on_owner_id ON dmails USING btree (owner_id); insert into dmails (owner_id, from_id, to_id, title, body, is_read, is_deleted, created_at, updated_at) select dmails_orig.from_id, dmails_orig.from_id, dmails_orig.to_id, dmails_orig.title, dmails_orig.body, dmails_orig.has_seen, false, dmails_orig.created_at, dmails_orig.created_at from dmails_orig; insert into dmails (owner_id, from_id, to_id, title, body, is_read, is_deleted, created_at, updated_at) select dmails_orig.to_id, dmails_orig.from_id, dmails_orig.to_id, dmails_orig.title, dmails_orig.body, dmails_orig.has_seen, false, dmails_orig.created_at, dmails_orig.created_at from dmails_orig; -drop sequence favorite_tags_id_seq; alter table favorites drop column id; +drop sequence favorite_tags_id_seq; alter table favorites rename to favorites_orig; alter table favorites_orig drop constraint fk_favorites__post; alter table favorites_orig drop constraint fk_favorites__user; @@ -197,6 +197,220 @@ alter table post_disapprovals add column id serial primary key; create index index_post_disapprovals_on_user_id on post_disapprovals (user_id); create index index_post_disapprovals_on_post_id on post_disapprovals (post_id); +alter table note_versions drop constraint fk_note_versions__note; +alter table note_versions drop constraint fk_note_versions__post; +alter table note_versions drop constraint fk_note_versions__user; +alter table note_versions drop column version; +alter table note_versions rename column ip_addr to updater_ip_addr; +alter table note_versions rename column user_id to updater_id; +alter table note_versions drop column text_search_index; +alter index idx_note_versions__post rename to index_note_versions_on_post_id; +alter index idx_notes__note rename to index_note_versions_on_note_id; +alter index index_note_versions_on_user_id rename to index_note_versions_on_updater_id; +create index index_note_versions_on_updater_ip_addr on note_versions (updater_ip_addr); + +alter table notes drop constraint fk_notes__post; +alter table notes drop constraint fk_notes__user; +drop trigger trg_note_search_update on notes; +alter table notes rename column user_id to creator_id; +alter table notes rename column text_search_index to body_index; +alter index comments_text_search_idx rename to index_notes_on_body_index; +alter index idx_notes__post rename to index_notes_on_post_id; +drop index notes_text_search_idx; +CREATE TRIGGER trigger_notes_on_update BEFORE INSERT OR UPDATE ON notes FOR EACH ROW EXECUTE PROCEDURE tsvector_update_trigger('body_index', 'pg_catalog.english', 'body'); +create index index_notes_on_creator_id on notes (creator_id); +alter table notes drop column ip_addr; +alter table notes drop column version; + +drop table pixiv_proxies; + +alter table pool_updates drop constraint pool_updates_pool_id_fkey; +alter table pool_updates rename to pool_versions; +alter table pool_versions rename column user_id to updater_id; +alter table pool_versions rename column ip_addr to updater_ip_addr; +alter index index_pool_updates_on_pool_id rename to index_pool_versions_on_pool_id; +alter index index_pool_updates_on_user_id rename to index_pool_versions_on_updater_id; +create index index_pool_versions_on_updater_ip_addr on pool_versions (updater_ip_addr); +alter table pool_versions drop column id; +alter table pool_versions add column id serial primary key; + +alter table pools drop constraint pools_user_id_fkey; +alter table pools rename column user_id to creator_id; +alter table pools drop column is_public; +alter table pools add column post_ids text not null default ''; +alter index pools_user_id_idx rename to index_pools_on_creator_id; +-- update pools.post_ids + +drop table post_change_seq; + +alter table post_tag_histories rename to post_versions; +alter table post_versions drop constraint fk_post_tag_histories__post; +alter table post_versions drop constraint post_tag_histories_user_id_fkey; +alter table post_versions rename column created_at to updated_at; +alter table post_versions rename column user_id to updater_id; +alter table post_versions rename column ip_addr to updater_ip_addr; +alter table post_versions add column source text; +alter table post_versions add column add_tags text not null default ''; +alter table post_versions add column del_tags text not null default ''; +alter index idx_post_tag_histories__post rename to index_post_versions_on_post_id; +alter index index_post_tag_histories_on_user_id rename to index_post_versions_on_updater_id; +create index index_post_versions_on_updater_ip_addr on post_versions (updater_ip_addr); +alter table post_versions drop column id; +alter table post_versions add column id serial primary key; +-- update post_versions.add_tags +-- update post_versions.del_tags + +alter table post_votes drop constraint post_votes_post_id_fkey; +alter table post_votes drop constraint post_votes_user_id_fkey; + +drop trigger trg_posts_tags_index_update on posts; +alter table posts drop constraint fk_posts__user; +alter table posts drop constraint posts_approver_id_fkey; +alter table posts drop constraint posts_parent_id_fkey; +alter table posts add column up_score integer not null default 0; +alter table posts add column down_score integer not null default 0; +alter table posts rename column width to image_width; +alter table posts rename column height to image_height; +alter table posts rename column ip_addr to uploader_ip_addr; +alter table posts rename column user_id to uploader_id; +alter table posts rename column cached_tags to tag_string; +alter table posts add column is_pending boolean not null default false; +alter table posts add column is_flagged boolean not null default false; +alter table posts add column is_deleted boolean not null default false; +update posts set is_pending = true where status = 'pending'; +update posts set is_flagged = true where status = 'flagged'; +update posts set is_deleted = true where status = 'deleted'; +alter table posts drop column status; +alter table posts drop column sample_width; +alter table posts drop column sample_height; +alter table posts drop column change_seq; +alter table posts rename column tags_index to tag_index; +alter table posts rename column general_tag_count to tag_count_general; +alter table posts rename column artist_tag_count to tag_count_artist; +alter table posts rename column character_tag_count to tag_count_character; +alter table posts rename column copyright_tag_count to tag_count_copyright; +alter table posts add column fav_string text not null default ''; +alter table posts add column pool_string text not null default ''; +CREATE TRIGGER trigger_posts_on_tag_index_update BEFORE INSERT OR UPDATE ON posts FOR EACH ROW EXECUTE PROCEDURE tsvector_update_trigger('tag_index', 'public.danbooru', 'tag_string', 'fav_string', 'pool_string'); +alter index idx_posts__md5 rename to index_posts_on_md5; +alter index idx_posts__created_at rename to index_posts_on_created_at; +alter index idx_posts__last_commented_at rename to index_posts_on_last_commented_at; +alter index idx_posts__last_noted_at rename to index_posts_on_last_noted_at; +alter index idx_posts__users rename to index_posts_on_uploader_id; +alter index idx_posts_parent_id rename to index_posts_on_parent_id; +alter index index_posts_on_height rename to index_posts_on_image_height; +alter index index_posts_on_width rename to index_posts_on_image_width; +alter index index_posts_on_tags_index rename to index_posts_on_tag_index; +alter index posts_mpixels rename to index_posts_on_mpixels; +create index index_posts_on_uploader_ip_addr on posts (uploader_ip_addr); +-- update posts.fav_string +-- update posts.pool_string + +drop table server_keys; +drop table table_data; + +alter table tag_aliases drop constraint fk_tag_aliases__alias; +alter table tag_aliases drop constraint tag_aliases_creator_id_fkey; +alter table tag_aliases rename column name to antecedent_name; +alter table tag_aliases add column consequent_name varchar(255) not null default ''; +alter table tag_aliases add column status varchar(255) not null default 'active'; +alter table tag_aliases add column forum_topic_id integer; +alter table tag_aliases add column creator_ip_addr inet not null default '127.0.0.1'; +alter index idx_tag_aliases__name rename to index_tag_aliases_on_antecedent_name; +create index index_tag_aliases_on_consequent_name on tag_aliases (consequent_name); +update tag_aliases set status = 'active' where is_pending = false; +update tag_aliases set status = 'pending' where is_pending = true; +alter table tag_aliases drop column is_pending; +update tag_aliases set consequent_name = (select _.name from tags _ where _.id = tag_aliases.alias_id); +alter table tag_aliases drop column alias_id; + +alter table tag_implications drop constraint fk_tag_implications__child; +alter table tag_implications drop constraint fk_tag_implications__parent; +alter table tag_implications drop constraint tag_implications_creator_id_fkey; +alter table tag_implications add column antecedent_name varchar(255) not null default ''; +alter table tag_implications add column consequent_name varchar(255) not null default ''; +alter table tag_implications add column descendant_names text not null default ''; +alter table tag_implications add column creator_ip_addr inet not null default '127.0.0.1'; +alter table tag_implications add column status varchar(255) not null default 'active'; +update tag_implications set status = 'active' where is_pending = false; +update tag_implications set status = 'pending' where is_pending = true; +alter table tag_implications drop column is_pending; +update tag_implications set antecedent_name = (select _.name from tags _ where _.id = tag_implications.predicate_id); +update tag_implications set conseuqent_name = (select _.name from tags _ where _.id = tag_implications.consequent_id); +alter table tag_implications drop column consequent_id; +alter table tag_implications drop column predicate_id; + +alter table tag_subscriptions drop constraint tag_subscriptions_user_id_fkey; +alter table tag_subscriptions rename column user_id to creator_id; +alter table tag_subscriptions rename column cached_post_ids to post_ids; +alter table tag_subscriptions rename column is_visible_on_profile to is_public; +alter table tag_subscriptions add column created_at timestamp without time zone; +alter table tag_subscriptions add column updated_at timestamp without time zone; +alter table tag_subscriptions add column last_accessed_at timestamp without time zone; +alter index index_tag_subscriptions_on_user_id rename to index_tag_subscriptions_on_creator_id; + +alter table tags rename column tag_type to category; +alter table tags drop column is_ambiguous; +alter table tags rename column cached_related to related_tags; +alter table tags rename column cached_related_expires_on to related_tags_updated_at; +alter table tags add column created_at timestamp without time zone; +alter table tags add column updated_at timestamp without time zone; +alter index idx_tags__name rename to index_tags_on_name; +alter index idx_tags__post_count rename to index_tags_on_post_count; + +alter table test_janitors rename to janitor_trials; +alter table janitor_trials drop column id; +alter table janitor_trials add column id serial primary key; +alter table janitor_trials drop column promotion_date; +alter table janitor_trials drop column test_promotion_date; +alter table janitor_trials drop constraint test_janitors_user_id_fkey; +alter table janitor_trials rename column user_id to creator_id; +alter index index_test_janitors_on_user_id rename to index_janitor_trials_on_creator_id; + +alter table user_records rename to user_feedback; +alter table user_feedback drop constraint user_records_reported_by_fkey; +alter table user_feedback drop constraint user_records_user_id_fkey; +alter table user_feedback rename column reported_by to creator_id; +alter table user_feedback add column category varchar(255) not null default ''; +update user_feedback set category = 'negative' where is_positive = false; +update user_feedback set category = 'positive' where is_positive = true; +update user_feedback set category = 'neutral' where is_positive is null; +alter table user_feedback drop column is_positive; +create index index_user_feedback_on_user_id on user_feedback (user_id); +alter table user_feedback drop column id; +alter table user_feedback add column id serial primary key; + +-- process user_blacklisted_tags + +alter table users add column updated_at timestamp without time zone; +alter table users add column email_verification_key varchar(255); +alter table users rename column invited_by to inviter_id; +alter table users rename column last_forum_topic_read_at to last_forum_read_at; +alter table users rename column receive_dmails to receive_email_notifications; +alter table users add column is_banned boolean not null default false; +alter table users add column default_image_size varchar(255) not null default 'medium'; +alter table users add column favorite_tags text; +alter table users add column blacklisted_tags text; +alter table users add column time_zone varchar(255) not null default 'Eastern Time (US & Canada)'; +alter index idx_users__name rename to index_users_on_name; +create index index_users_on_email on users (email) where email is not null; +create index index_users_on_inviter_id on users (inviter_id) where inviter_id is not null; +-- update users.blacklisted_tags + +alter table wiki_page_versions drop constraint fk_wiki_page_versions__user; +alter table wiki_page_versions drop constraint fk_wiki_page_versions__wiki_page; +alter table wiki_page_versions rename column user_id to updater_id; +alter table wiki_page_versions rename column ip_addr to updater_ip_addr; +alter table wiki_page_versions drop column version; +alter table wiki_page_versions drop column text_search_index; +alter index idx_wiki_page_versions__wiki_page rename to index_wiki_page_versions_on_wiki_page_id; +alter index index_wiki_page_versions_on_user_id rename to index_wiki_page_versions_on_updater_id; + +alter table wiki_pages drop constraint fk_wiki_pages__user; + -- post processing drop table dmails_orig; drop table favorites_orig; +drop table pools_posts; +alter table post_versions drop column tags; +alter table users drop column show_samples; \ No newline at end of file