From 8f98e8e03c2075a7f3679af64ba02e0eba10c632 Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 26 May 2017 13:54:17 -0500 Subject: [PATCH 1/2] posts: redefine source column to be non-NULL (fixes #3090). --- app/logical/post_query_builder.rb | 4 ++-- ...26183928_change_source_to_non_null_on_posts.rb | 15 +++++++++++++++ db/structure.sql | 4 +++- 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20170526183928_change_source_to_non_null_on_posts.rb diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index 22353c57a..8a0fa072e 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -175,7 +175,7 @@ class PostQueryBuilder # URLs unchanged. This is to ease database load for Pixiv source searches. if q[:source] if q[:source] == "none%" - relation = relation.where("(posts.source = '' OR posts.source IS NULL)") + relation = relation.where("posts.source = ''") elsif q[:source] == "http%" relation = relation.where("(lower(posts.source) like ?)", "http%") elsif q[:source] =~ /^(?:https?:\/\/)?%\.?pixiv(?:\.net(?:\/img)?)?(?:%\/img\/|%\/|(?=%$))(.+)$/i @@ -189,7 +189,7 @@ class PostQueryBuilder if q[:source_neg] if q[:source_neg] == "none%" - relation = relation.where("(posts.source != '' AND posts.source IS NOT NULL)") + relation = relation.where("posts.source != ''") elsif q[:source_neg] == "http%" relation = relation.where("(lower(posts.source) not like ?)", "http%") elsif q[:source_neg] =~ /^(?:https?:\/\/)?%\.?pixiv(?:\.net(?:\/img)?)?(?:%\/img\/|%\/|(?=%$))(.+)$/i diff --git a/db/migrate/20170526183928_change_source_to_non_null_on_posts.rb b/db/migrate/20170526183928_change_source_to_non_null_on_posts.rb new file mode 100644 index 000000000..23030b5cc --- /dev/null +++ b/db/migrate/20170526183928_change_source_to_non_null_on_posts.rb @@ -0,0 +1,15 @@ +class ChangeSourceToNonNullOnPosts < ActiveRecord::Migration + def up + Post.without_timeout do + change_column_null(:posts, :source, false, "") + change_column_default(:posts, :source, "") + end + end + + def down + Post.without_timeout do + change_column_null(:posts, :source, true) + change_column_default(:posts, :source, nil) + end + end +end diff --git a/db/structure.sql b/db/structure.sql index 1df387bc2..23da65238 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -2773,7 +2773,7 @@ CREATE TABLE posts ( up_score integer DEFAULT 0 NOT NULL, down_score integer DEFAULT 0 NOT NULL, score integer DEFAULT 0 NOT NULL, - source character varying, + source character varying DEFAULT ''::character varying NOT NULL, md5 character varying NOT NULL, rating character(1) DEFAULT 'q'::bpchar NOT NULL, is_note_locked boolean DEFAULT false NOT NULL, @@ -7547,3 +7547,5 @@ INSERT INTO schema_migrations (version) VALUES ('20170515235205'); INSERT INTO schema_migrations (version) VALUES ('20170519204506'); +INSERT INTO schema_migrations (version) VALUES ('20170526183928'); + From a401b1f5707474090e4dcd8a1d94b99f959736c0 Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 26 May 2017 15:02:13 -0500 Subject: [PATCH 2/2] posts: fix nil source tests; fix source:none metatag. --- app/models/post.rb | 2 +- test/unit/post_test.rb | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 0edeb5ff5..10c3a863b 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -818,7 +818,7 @@ class Post < ActiveRecord::Base end when /^source:none$/i - self.source = nil + self.source = "" when /^source:"(.*)"$/i self.source = $1 diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index 8563d0b71..f2785ba1b 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -848,7 +848,7 @@ class PostTest < ActiveSupport::TestCase should "clear the source with source:none" do @post.update(:source => "foobar") @post.update(:tag_string => "source:none") - assert_nil(@post.source) + assert_equal("", @post.source) end should "set the pixiv id with source:https://img18.pixiv.net/img/evazion/14901720.png" do @@ -1204,7 +1204,7 @@ class PostTest < ActiveSupport::TestCase end should "merge any parent, source, and rating changes that were made after loading the initial set" do - post = FactoryGirl.create(:post, :parent => nil, :source => nil, :rating => "q") + post = FactoryGirl.create(:post, :parent => nil, :source => "", :rating => "q") parent_post = FactoryGirl.create(:post) # user a changes rating to safe, adds parent @@ -2349,7 +2349,7 @@ class PostTest < ActiveSupport::TestCase context "a post that has been updated" do setup do PostArchive.sqs_service.stubs(:merge?).returns(false) - @post = FactoryGirl.create(:post, :rating => "q", :tag_string => "aaa", :source => nil) + @post = FactoryGirl.create(:post, :rating => "q", :tag_string => "aaa", :source => "") @post.update_attributes(:tag_string => "aaa bbb ccc ddd") @post.update_attributes(:tag_string => "bbb xxx yyy", :source => "xyz") @post.update_attributes(:tag_string => "bbb mmm yyy", :source => "abc") @@ -2362,7 +2362,7 @@ class PostTest < ActiveSupport::TestCase should "correctly revert all fields" do assert_equal("aaa bbb ccc ddd", @post.tag_string) - assert_nil(@post.source) + assert_equal("", @post.source) assert_equal("q", @post.rating) end end