Add task to migrate enabled poll results and stats

This commit is contained in:
Javi Martín
2019-04-29 18:51:15 +02:00
parent 45a3d8daf0
commit 9ae0cbb254
3 changed files with 88 additions and 0 deletions

29
lib/migrations/reports.rb Normal file
View File

@@ -0,0 +1,29 @@
class Migrations::Reports
def migrate
migrate_polls
migrate_budgets
end
private
def migrate_polls
Poll.find_each do |poll|
next unless poll.report.new_record?
poll.report.update!(
results: poll.read_attribute(:results_enabled),
stats: poll.read_attribute(:stats_enabled),
advanced_stats: poll.read_attribute(:stats_enabled),
)
end
end
def migrate_budgets
Budget.find_each do |budget|
next unless budget.report.new_record?
budget.report.update!(results: true, stats: true, advanced_stats: true)
end
end
end

View File

@@ -0,0 +1,6 @@
namespace :stats_and_results do
desc "Migrates stats_enabled and results_enabled data to enabled reports"
task migrate_to_reports: :environment do
Migrations::Reports.new.migrate
end
end

View File

@@ -0,0 +1,53 @@
require "rails_helper"
describe Migrations::Reports do
describe "#migrate" do
it "ignores polls with existing reports" do
create(:poll, results_enabled: true, stats_enabled: true) do |poll|
poll.write_attribute(:results_enabled, false)
poll.write_attribute(:stats_enabled, false)
poll.save
end
Migrations::Reports.new.migrate
expect(Poll.last.results_enabled).to be true
expect(Poll.last.stats_enabled).to be true
expect(Poll.last.advanced_stats_enabled).to be nil
end
it "migrates polls with no reports" do
create(:poll) do |poll|
poll.write_attribute(:results_enabled, true)
poll.write_attribute(:stats_enabled, true)
poll.save
end
Migrations::Reports.new.migrate
expect(Poll.last.results_enabled).to be true
expect(Poll.last.stats_enabled).to be true
expect(Poll.last.advanced_stats_enabled).to be true
end
it "ignores budgets with existing reports" do
create(:budget, results_enabled: false, stats_enabled: false, advanced_stats_enabled: false)
Migrations::Reports.new.migrate
expect(Budget.last.results_enabled).to be false
expect(Budget.last.stats_enabled).to be false
expect(Budget.last.advanced_stats_enabled).to be false
end
it "enables results and stats for every budget" do
create(:budget)
Migrations::Reports.new.migrate
expect(Budget.last.results_enabled).to be true
expect(Budget.last.stats_enabled).to be true
expect(Budget.last.advanced_stats_enabled).to be true
end
end
end