Remove obsolete rake tasks

These tasks dealt with data migrations or stats generations which were
done only once, so we don't need them anymore.

New CONSUL installations don't need these tasks, and existing CONSUL
installations will execute them when upgrading one release at a time.
This commit is contained in:
Javi Martín
2019-03-21 14:10:00 +01:00
parent 7d472b747e
commit 9d6bd7e788
17 changed files with 0 additions and 786 deletions

View File

@@ -13,22 +13,4 @@ namespace :budgets do
end
namespace :phases do
desc "Generates Phases for existing Budgets without them & migrates description_* attributes"
task generate_missing: :environment do
Budget.where.not(id: Budget::Phase.all.pluck(:budget_id).uniq.compact).each do |budget|
Budget::Phase::PHASE_KINDS.each do |phase|
Budget::Phase.create(
budget: budget,
kind: phase,
description: budget.send("description_#{phase}"),
prev_phase: budget.phases&.last,
starts_at: budget.phases&.last&.ends_at || Date.current,
ends_at: (budget.phases&.last&.ends_at || Date.current) + 1.month
)
end
end
end
end
end

View File

@@ -1,21 +0,0 @@
namespace :communities do
desc "Associate community to proposals and budget investments"
task associate_community: :environment do
Proposal.all.each do |proposal|
if proposal.community.blank?
community = Community.create
proposal.update(community_id: community.id)
end
end
Budget::Investment.all.each do |investment|
if investment.community.blank?
community = Community.create
investment.update(community_id: community.id)
end
end
end
end

View File

@@ -1,89 +0,0 @@
namespace :globalize do
def translatable_classes
[
AdminNotification,
Banner,
Milestone,
I18nContent,
Legislation::DraftVersion,
Legislation::Process,
Legislation::Question,
Legislation::QuestionOption,
Poll,
Poll::Question,
Poll::Question::Answer,
SiteCustomization::Page,
Widget::Card
]
end
def migrate_data
@errored = false
translatable_classes.each do |model_class|
logger.info "Migrating #{model_class} data"
fields = model_class.translated_attribute_names
model_class.find_each do |record|
fields.each do |field|
locale = if model_class == SiteCustomization::Page && record.locale.present?
record.locale
else
I18n.locale
end
translated_field = record.localized_attr_name_for(field, locale)
if record.send(translated_field).blank?
record.send(:"#{translated_field}=", record.untranslated_attributes[field.to_s])
end
end
begin
record.save!
rescue ActiveRecord::RecordInvalid
logger.warn "Failed to save #{model_class} with id #{record.id}"
@errored = true
end
end
end
end
def logger
@logger ||= Logger.new(STDOUT).tap do |logger|
logger.formatter = proc { |severity, _datetime, _progname, msg| "#{severity} #{msg}\n" }
end
end
def errored?
@errored
end
desc "Simulates migrating existing data to translation tables"
task simulate_migrate_data: :environment do
logger.info "Starting migrate data simulation"
ActiveRecord::Base.transaction do
migrate_data
raise ActiveRecord::Rollback
end
if errored?
logger.warn "Some database records will not be migrated"
else
logger.info "Migrate data simulation ended successfully"
end
end
desc "Migrates existing data to translation tables"
task migrate_data: :environment do
logger.info "Starting data migration"
migrate_data
logger.info "Finished data migration"
if errored?
logger.warn "Some database records couldn't be migrated; please check the log messages"
end
end
end

View File

@@ -1,13 +0,0 @@
namespace :homepage do
desc "Initialize feeds available in homepage"
task create_feeds: :environment do
%w(proposals debates processes).each do |kind|
Widget::Feed.create(kind: kind)
Setting["feature.homepage.widgets.feeds.proposals"] = true
Setting["feature.homepage.widgets.feeds.debates"] = true
Setting["feature.homepage.widgets.feeds.processes"] = true
end
end
end

View File

@@ -1,22 +0,0 @@
namespace :locales do
desc "Migrate all localization files to new structure for a given locale name as argument"
task :migrate_structure, [:locale] => [:environment] do |_t, args|
locale = args[:locale]
puts "Moving files for locale: #{locale}"
# This creates ./config/locales/en/ directory
system "mkdir ./config/locales/#{locale}"
# This moves from ./config/locales/en.yml to ./config/locales/en/general.yml
system "mv ./config/locales/#{locale}.yml ./config/locales/#{locale}/general.yml"
# This moves from ./config/locales/admin.en.yml to ./config/locales/en/admin.en.yml
system "mv ./config/locales/*.#{locale}.yml ./config/locales/#{locale}/"
# This moves from ./config/locales/en/admin.en.yml to ./config/locales/en/admin.yml
system "find ./config/locales/ -name \"*.#{locale}.yml\" -exec sh -c 'mv \"$1\" \"${1%.#{locale}.yml}.yml\"' _ {} \\;"
puts "Moved!"
end
end

View File

@@ -1,8 +0,0 @@
namespace :map_locations do
desc "Destroy all empty MapLocation instances found in the database"
task destroy: :environment do
MapLocation.where(longitude: nil, latitude: nil, zoom: nil).each do |map_location|
map_location.destroy
end
end
end

View File

@@ -1,106 +0,0 @@
namespace :milestones do
def generate_table_migration_sql(new_table:, old_table:, columns:)
from_cols = ["id", *columns.keys]
to_cols = ["id", *columns.values]
<<~SQL
INSERT INTO #{new_table} (#{to_cols.join(", ")})
SELECT #{from_cols.join(", ")} FROM #{old_table};
SQL
end
def migrate_table!(new_table:, old_table:, columns:)
puts "Migrating data from '#{old_table}' to '#{new_table}'..."
result = ActiveRecord::Base.connection.execute(
generate_table_migration_sql(old_table: old_table,
new_table: new_table,
columns: columns)
)
puts "#{result.cmd_tuples} rows affected"
end
def populate_column!(table:, column:, value:)
puts "Populating column '#{column}' from table '#{table}' with '#{value}'..."
result = ActiveRecord::Base.connection.execute(
"UPDATE #{table} SET #{column} = '#{value}';"
)
puts "#{result.cmd_tuples} rows affected"
end
def count_rows(table)
ActiveRecord::Base.connection.query("SELECT COUNT(*) FROM #{table};")[0][0].to_i
end
desc "Migrate milestones and milestone status data after making the model polymorphic"
task migrate: :environment do
# This script copies all milestone-related data from the old tables to
# the new ones (preserving all primary keys). All 3 of the new tables
# must be empty.
#
# To clear the new tables to test this script:
#
# DELETE FROM milestone_statuses;
# DELETE FROM milestones;
# DELETE FROM milestone_translations;
#
start = Time.now
ActiveRecord::Base.transaction do
migrate_table! old_table: "budget_investment_statuses",
new_table: "milestone_statuses",
columns: {"name" => "name",
"description" => "description",
"hidden_at" => "hidden_at",
"created_at" => "created_at",
"updated_at" => "updated_at"}
migrate_table! old_table: "budget_investment_milestones",
new_table: "milestones",
columns: {"investment_id" => "milestoneable_id",
"title" => "title",
"description" => "description",
"created_at" => "created_at",
"updated_at" => "updated_at",
"publication_date" => "publication_date",
"status_id" => "status_id"}
populate_column! table: "milestones",
column: "milestoneable_type",
value: "Budget::Investment"
migrate_table! old_table: "budget_investment_milestone_translations",
new_table: "milestone_translations",
columns: {"budget_investment_milestone_id" => "milestone_id",
"locale" => "locale",
"created_at" => "created_at",
"updated_at" => "updated_at",
"title" => "title",
"description" => "description"}
Image.where(imageable_type: "Budget::Investment::Milestone").
update_all(imageable_type: "Milestone")
Document.where(documentable_type: "Budget::Investment::Milestone").
update_all(documentable_type: "Milestone")
puts "Verifying that all rows were copied..."
{
"budget_investment_milestones" => "milestones",
"budget_investment_statuses" => "milestone_statuses",
"budget_investment_milestone_translations" => "milestone_translations"
}.each do |original_table, migrated_table|
ActiveRecord::Base.connection.execute(
"select setval('#{migrated_table}_id_seq', (select max(id) from #{migrated_table}));"
)
unless count_rows(original_table) == count_rows(migrated_table)
raise "Number of rows of old and new tables do not match! Rolling back transaction..."
end
end
end
puts "Finished in %.3f seconds" % (Time.now - start)
end
end

View File

@@ -1,6 +0,0 @@
namespace :polls do
desc "Adds created_at and updated_at values to existing polls"
task initialize_timestamps: :environment do
Poll.update_all(created_at: Time.current, updated_at: Time.current)
end
end

View File

@@ -1,42 +1,5 @@
namespace :settings do
desc "Changes Setting key per_page_code for per_page_code_head"
task per_page_code_migration: :environment do
per_page_code = Setting.where(key: "per_page_code").first
per_page_code_head = Setting.where(key: "per_page_code_head").first
Setting["per_page_code_head"] = per_page_code&.value.to_s if per_page_code_head.blank?
per_page_code.destroy if per_page_code.present?
end
desc "Create new Attached Documents feature setting"
task create_attached_documents_setting: :environment do
Setting["feature.allow_attached_documents"] = true
end
desc "Enable recommendations settings"
task enable_recommendations: :environment do
Setting["feature.user.recommendations"] = true
Setting["feature.user.recommendations_on_debates"] = true
Setting["feature.user.recommendations_on_proposals"] = true
end
desc "Enable Help page"
task enable_help_page: :environment do
Setting["feature.help_page"] = true
end
desc "Enable Featured proposals"
task enable_featured_proposals: :environment do
Setting["feature.featured_proposals"] = true
Setting["featured_proposals_number"] = 3
end
desc "Create new period to calculate hot_score"
task create_hot_score_period_setting: :environment do
Setting["hot_score_period_in_days"] = 31
end
desc "Remove deprecated settings"
task remove_deprecated_settings: :environment do
deprecated_keys = [

View File

@@ -1,8 +0,0 @@
namespace :slugs do
desc "Generate slug attribute for objects from classes that use Sluggable concern"
task generate: :environment do
%w(Budget Budget::Heading Budget::Group).each do |class_name|
class_name.constantize.find_each(&:generate_slug)
end
end
end

View File

@@ -1,10 +0,0 @@
namespace :users do
desc "Enable recommendations for existing users"
task enable_recommendations: :environment do
User.find_each do |user|
user.update(recommended_debates: true, recommended_proposals: true)
end
end
end

View File

@@ -1,10 +0,0 @@
namespace :web_sections do
desc "Generate web sections for banners"
task generate: :environment do
WebSection.create(name: "homepage")
WebSection.create(name: "debates")
WebSection.create(name: "proposals")
WebSection.create(name: "budgets")
WebSection.create(name: "help_page")
end
end

View File

@@ -1,43 +0,0 @@
require "rails_helper"
describe "Communities Rake" do
describe "#associate_community" do
let :run_rake_task do
Rake::Task["communities:associate_community"].reenable
Rake.application.invoke_task "communities:associate_community"
end
context "Associate community to Proposal" do
it "When proposal has not community_id" do
proposal = create(:proposal)
proposal.update(community_id: nil)
expect(proposal.community).to be_nil
run_rake_task
proposal.reload
expect(proposal.community).to be_present
end
end
context "Associate community to Budget Investment" do
it "When budget investment has not community_id" do
investment = create(:budget_investment)
investment.update(community_id: nil)
expect(investment.community).to be_nil
run_rake_task
investment.reload
expect(investment.community).to be_present
end
end
end
end

View File

@@ -1,162 +0,0 @@
require "rails_helper"
describe "Globalize tasks" do
describe "#migrate_data" do
let :run_rake_task do
Rake::Task["globalize:migrate_data"].reenable
Rake.application.invoke_task "globalize:migrate_data"
end
context "Original data with no translated data" do
let(:poll) do
create(:poll).tap do |poll|
poll.translations.delete_all
poll.update_column(:name, "Original")
poll.reload
end
end
it "copies the original data" do
expect(poll.send(:"name_#{I18n.locale}")).to be nil
expect(poll.name).to eq("Original")
run_rake_task
poll.reload
expect(poll.name).to eq("Original")
expect(poll.send(:"name_#{I18n.locale}")).to eq("Original")
end
end
context "Original data with blank translated data" do
let(:banner) do
create(:banner).tap do |banner|
banner.update_column(:title, "Original")
banner.translations.first.update_column(:title, "")
end
end
it "copies the original data" do
expect(banner.title).to eq("")
run_rake_task
banner.reload
expect(banner.title).to eq("Original")
expect(banner.send(:"title_#{I18n.locale}")).to eq("Original")
end
end
context "Original data with translated data" do
let(:notification) do
create(:admin_notification, title: "Translated").tap do |notification|
notification.update_column(:title, "Original")
end
end
it "maintains the translated data" do
expect(notification.title).to eq("Translated")
run_rake_task
notification.reload
expect(notification.title).to eq("Translated")
expect(notification.send(:"title_#{I18n.locale}")).to eq("Translated")
end
end
context "Custom page with a different locale and no translations" do
let(:page) do
create(:site_customization_page, locale: :fr).tap do |page|
page.translations.delete_all
page.update_column(:title, "en Français")
page.reload
end
end
it "copies the original data to both the page's locale" do
expect(page.title).to eq("en Français")
expect(page.title_fr).to be nil
expect(page.send(:"title_#{I18n.locale}")).to be nil
run_rake_task
page.reload
expect(page.title).to eq("en Français")
expect(page.title_fr).to eq("en Français")
expect(page.send(:"title_#{I18n.locale}")).to be nil
end
end
context "Custom page with a different locale and existing translations" do
let(:page) do
create(:site_customization_page, title: "In English", locale: :fr).tap do |page|
page.update_column(:title, "en Français")
end
end
it "copies the original data to the page's locale" do
expect(page.title_fr).to be nil
expect(page.title).to eq("In English")
run_rake_task
page.reload
expect(page.title).to eq("In English")
expect(page.title_fr).to eq("en Français")
expect(page.send(:"title_#{I18n.locale}")).to eq("In English")
end
end
context "Invalid data" do
let!(:valid_process) do
create(:legislation_process).tap do |process|
process.translations.delete_all
process.update_column(:title, "Title")
process.reload
end
end
let!(:invalid_process) do
create(:legislation_process).tap do |process|
process.translations.delete_all
process.update_column(:title, "")
process.reload
end
end
it "ignores invalid data and migrates valid data" do
expect(valid_process).to be_valid
expect(invalid_process).not_to be_valid
run_rake_task
expect(valid_process.translations.count).to eq 1
expect(valid_process.reload.title).to eq "Title"
expect(invalid_process.translations.count).to eq 0
expect(invalid_process.reload.title).to eq ""
end
end
context "locale with non-underscored name" do
before { I18n.locale = :"pt-BR" }
let!(:milestone) do
create(:milestone).tap do |milestone|
milestone.translations.delete_all
milestone.update_column(:title, "Português")
milestone.reload
end
end
it "runs the migration successfully" do
run_rake_task
expect(milestone.reload.title).to eq "Português"
end
end
end
end

View File

@@ -1,20 +0,0 @@
require "rails_helper"
describe "rake map_locations:destroy" do
before do
create(:map_location, :proposal_map_location)
empty_location = create(:map_location, :proposal_map_location)
empty_location.attributes = { longitude: nil, latitude: nil, zoom: nil }
empty_location.save(validate: false)
end
let :run_rake_task do
Rake.application.invoke_task("map_locations:destroy")
end
it "destroys empty locations" do
expect(MapLocation.all.size).to eq(2)
run_rake_task
expect(MapLocation.all.size).to eq(1)
end
end

View File

@@ -1,147 +0,0 @@
require "rails_helper"
describe "Milestones tasks" do
describe "#migrate" do
let :run_rake_task do
Rake::Task["milestones:migrate"].reenable
Rake.application.invoke_task "milestones:migrate"
end
let!(:investment) { create(:budget_investment) }
before do
ActiveRecord::Base.connection.execute(
"INSERT INTO budget_investment_statuses " +
"(name, description, hidden_at, created_at, updated_at) " +
"VALUES ('open', 'Good', NULL, '#{Time.current - 1.day}', '#{Time.current}');"
)
status_id = ActiveRecord::Base.connection.execute(
"SELECT MAX(id) FROM budget_investment_statuses;"
).to_a.first["max"]
milestone_attributes = {
investment_id: investment.id,
title: "First",
description: "Interesting",
publication_date: Date.yesterday,
status_id: status_id,
created_at: Time.current - 1.day,
updated_at: Time.current
}
ActiveRecord::Base.connection.execute(
"INSERT INTO budget_investment_milestones " +
"(#{milestone_attributes.keys.join(", ")}) " +
"VALUES (#{milestone_attributes.values.map { |value| "'#{value}'"}.join(", ")})"
)
end
it "migrates statuses" do
run_rake_task
expect(Milestone::Status.count).to be 1
status = Milestone::Status.first
expect(status.name).to eq "open"
expect(status.description).to eq "Good"
expect(status.hidden_at).to be nil
expect(status.created_at.to_date).to eq Date.yesterday
expect(status.updated_at.to_date).to eq Date.current
end
it "migrates milestones" do
run_rake_task
expect(Milestone.count).to be 1
milestone = Milestone.first
expect(milestone.milestoneable_id).to eq investment.id
expect(milestone.milestoneable_type).to eq "Budget::Investment"
expect(milestone.title).to eq "First"
expect(milestone.description).to eq "Interesting"
expect(milestone.publication_date).to eq Date.yesterday
expect(milestone.status_id).to eq Milestone::Status.first.id
expect(milestone.created_at.to_date).to eq Date.yesterday
expect(milestone.updated_at.to_date).to eq Date.current
end
it "Updates the primary key sequence correctly" do
run_rake_task
expect { create(:milestone) }.not_to raise_exception
end
context "Milestone has images and documents" do
let(:milestone_id) do
ActiveRecord::Base.connection.execute(
"SELECT MAX(id) FROM budget_investment_milestones;"
).to_a.first["max"]
end
let!(:image) do
create(:image, imageable_id: milestone_id).tap do |image|
image.update_column(:imageable_type, "Budget::Investment::Milestone")
end
end
let!(:document) do
create(:document, documentable_id: milestone_id).tap do |document|
document.update_column(:documentable_type, "Budget::Investment::Milestone")
end
end
it "migrates images and documents" do
run_rake_task
expect(Milestone.last.image).to eq image
expect(Milestone.last.documents).to eq [document]
end
end
context "Statuses had been deleted" do
before do
ActiveRecord::Base.connection.execute(
"INSERT INTO budget_investment_statuses " +
"(name, description, hidden_at, created_at, updated_at) " +
"VALUES ('deleted', 'Del', NULL, '#{Time.current - 1.day}', '#{Time.current}');"
)
ActiveRecord::Base.connection.execute(
"DELETE FROM budget_investment_statuses WHERE name='deleted'"
)
ActiveRecord::Base.connection.execute(
"INSERT INTO budget_investment_statuses " +
"(name, description, hidden_at, created_at, updated_at) " +
"VALUES ('new', 'New', NULL, '#{Time.current - 1.day}', '#{Time.current}');"
)
status_id = ActiveRecord::Base.connection.execute(
"SELECT MAX(id) FROM budget_investment_statuses;"
).to_a.first["max"]
milestone_attributes = {
investment_id: investment.id,
title: "Last",
description: "Different",
publication_date: Date.yesterday,
status_id: status_id,
created_at: Time.current - 1.day,
updated_at: Time.current
}
ActiveRecord::Base.connection.execute(
"INSERT INTO budget_investment_milestones " +
"(#{milestone_attributes.keys.join(", ")}) " +
"VALUES (#{milestone_attributes.values.map { |value| "'#{value}'"}.join(", ")})"
)
end
it "migrates the status id correctly" do
run_rake_task
expect(Milestone.last.status_id).to eq Milestone::Status.last.id
end
end
end
end

View File

@@ -35,72 +35,6 @@ describe Setting do
end
end
describe "#per_page_code_migration" do
let :run_rake_task do
Rake::Task["settings:per_page_code_migration"].reenable
Rake.application.invoke_task "settings:per_page_code_migration"
end
context "Neither per_page_code_head or per_page_code Settings exist" do
before do
Setting.where(key: "per_page_code").first&.destroy
Setting.where(key: "per_page_code_head").first&.destroy
run_rake_task
end
it "has per_page_code_head setting present and no per_page_code" do
expect(Setting.where(key: "per_page_code_head").count).to eq(1)
expect(Setting["per_page_code_head"]).to eq(nil)
expect(Setting.where(key: "per_page_code").count).to eq(0)
end
end
context "Both per_page_code_head or per_page_code Settings exist" do
before do
Setting["per_page_code"] = "per_page_code"
Setting["per_page_code_head"] = "per_page_code_head"
run_rake_task
end
it "has per_page_code_head setting present and no per_page_code" do
expect(Setting.where(key: "per_page_code_head").count).to eq(1)
expect(Setting["per_page_code_head"]).to eq("per_page_code_head")
expect(Setting.where(key: "per_page_code").count).to eq(0)
end
end
context "per_page_code_head exists, but per_page_code does not" do
before do
Setting.where(key: "per_page_code").first&.destroy
Setting["per_page_code_head"] = "per_page_code_head"
run_rake_task
end
it "has per_page_code_head setting present and no per_page_code" do
expect(Setting.where(key: "per_page_code_head").count).to eq(1)
expect(Setting["per_page_code_head"]).to eq("per_page_code_head")
expect(Setting.where(key: "per_page_code").count).to eq(0)
end
end
context "per_page_code_head does not exist, but per_page_code does" do
before do
Setting["per_page_code"] = "per_page_code"
Setting.where(key: "per_page_code_head").first&.destroy
run_rake_task
end
it "has per_page_code_head setting present and no per_page_code" do
expect(Setting.where(key: "per_page_code_head").count).to eq(1)
expect(Setting["per_page_code_head"]).to eq("per_page_code")
expect(Setting.where(key: "per_page_code").count).to eq(0)
end
end
end
describe "#rename_setting_keys" do
let :run_rake_task do