From 8f98e8e03c2075a7f3679af64ba02e0eba10c632 Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 26 May 2017 13:54:17 -0500 Subject: [PATCH] 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'); +