diff --git a/app/models/post_vote.rb b/app/models/post_vote.rb index 777e55427..45bb3e2e7 100644 --- a/app/models/post_vote.rb +++ b/app/models/post_vote.rb @@ -12,6 +12,8 @@ class PostVote < ApplicationRecord scope :negative, -> { where("post_votes.score < 0") } scope :public_votes, -> { positive.where(user: User.has_public_favorites) } + deletable + def self.visible(user) user.is_admin? ? all : where(user: user).or(public_votes) end diff --git a/db/migrate/20211121080239_add_is_deleted_to_post_votes.rb b/db/migrate/20211121080239_add_is_deleted_to_post_votes.rb new file mode 100644 index 000000000..ebf5c3bd7 --- /dev/null +++ b/db/migrate/20211121080239_add_is_deleted_to_post_votes.rb @@ -0,0 +1,9 @@ +class AddIsDeletedToPostVotes < ActiveRecord::Migration[6.1] + disable_ddl_transaction! + + def change + add_column :post_votes, :is_deleted, :boolean, default: false, null: :false + add_index :post_votes, :is_deleted, where: "is_deleted = TRUE", algorithm: :concurrently + add_index :post_votes, [:user_id, :post_id], unique: true, where: "is_deleted = FALSE", algorithm: :concurrently + end +end diff --git a/db/structure.sql b/db/structure.sql index 24a78d728..da30deb53 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1672,7 +1672,8 @@ CREATE TABLE public.post_votes ( user_id integer NOT NULL, score integer NOT NULL, created_at timestamp without time zone NOT NULL, - updated_at timestamp without time zone NOT NULL + updated_at timestamp without time zone NOT NULL, + is_deleted boolean DEFAULT false ); @@ -4213,6 +4214,13 @@ CREATE INDEX index_post_versions_on_version ON public.post_versions USING btree CREATE INDEX index_post_votes_on_created_at ON public.post_votes USING btree (created_at); +-- +-- Name: index_post_votes_on_is_deleted; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_post_votes_on_is_deleted ON public.post_votes USING btree (is_deleted) WHERE (is_deleted = true); + + -- -- Name: index_post_votes_on_post_id; Type: INDEX; Schema: public; Owner: - -- @@ -4227,6 +4235,13 @@ CREATE INDEX index_post_votes_on_post_id ON public.post_votes USING btree (post_ CREATE INDEX index_post_votes_on_user_id ON public.post_votes USING btree (user_id); +-- +-- Name: index_post_votes_on_user_id_and_post_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE UNIQUE INDEX index_post_votes_on_user_id_and_post_id ON public.post_votes USING btree (user_id, post_id) WHERE (is_deleted = false); + + -- -- Name: index_posts_on_approver_id; Type: INDEX; Schema: public; Owner: - -- @@ -5057,6 +5072,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20211015223510'), ('20211018045429'), ('20211018062916'), -('20211023225730'); +('20211023225730'), +('20211121080239');