discourse/db/post_migrate/20220215015538_drop_user_stat_count_constraints.rb
Martin Brennan f9ec2b90a0
DEV: Drop user_stats count column constraints (#15949)
We added this constraint in 5bd55acf83
but it is causing problems in hosted sites and is catching the
issue too far down the line. This commit removes the constraint
for now, and also fixes an issue found with PostDestroyer
which wasn't using the UserStatCountUpdater when updating post_count
and thus was causing negative numbers to occur.
2022-02-16 12:49:11 +11:00

26 lines
675 B
Ruby

# frozen_string_literal: true
class DropUserStatCountConstraints < ActiveRecord::Migration[6.1]
def up
execute "ALTER TABLE user_stats DROP CONSTRAINT topic_count_positive"
execute "ALTER TABLE user_stats DROP CONSTRAINT post_count_positive"
end
def down
execute(<<~SQL)
UPDATE user_stats
SET post_count = 0
WHERE post_count < 0
SQL
execute(<<~SQL)
UPDATE user_stats
SET topic_count = 0
WHERE topic_count < 0
SQL
execute "ALTER TABLE user_stats ADD CONSTRAINT topic_count_positive CHECK (topic_count >= 0)"
execute "ALTER TABLE user_stats ADD CONSTRAINT post_count_positive CHECK (post_count >= 0)"
end
end