Skip to content

Commit

Permalink
fix: private proposal fields (#52)
Browse files Browse the repository at this point in the history
* feat: Enable opendata download

* feat: Bump awesome and add rake task for migration

* feat: Add private fields rake task command

* lint: Fix rubocop offense
  • Loading branch information
Quentinchampenois authored and moustachu committed May 24, 2024
1 parent f6aaa79 commit a9f9058
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ GIT

GIT
remote: https://github.com/octree-gva/decidim-module-decidim_awesome.git
revision: 21ce064a26837ba43835621ddb6f6773b14c59e8
revision: afa824a79faa42f99eee5c0dcb4944e3a78b4a1d
branch: feat/awesome_decidim_private_fields
specs:
decidim-decidim_awesome (0.9.2)
Expand Down
2 changes: 0 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
mount Sidekiq::Web => "/sidekiq"
end

get "/open-data/download", to: redirect("/404")
post "/open-data/download", to: redirect("/404")
mount LetterOpenerWeb::Engine, at: "/letter_opener" if Rails.env.development?

mount Decidim::Core::Engine => "/"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

# This migration comes from decidim_decidim_awesome (originally 20230309124413)

class CreatePrivateProposalFields < ActiveRecord::Migration[5.2]
def up
create_table :decidim_awesome_private_proposal_fields do |t|
t.text :private_body, default: "<xml></xml>"

t.references :proposal, null: false, foreign_key: { to_table: :decidim_proposals_proposals }, index: { name: "decidim_awesome_private_proposal_fields_idx" }
t.timestamps
end
end
end
2 changes: 2 additions & 0 deletions lib/tasks/decidim_app.rake
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace :decidim_app do
task install: :environment do
puts "Running db:migrate"
Rake::Task["db:migrate"].invoke
Rake::Task["decidim:awesome:private_fields"].invoke
end

# This task is used to upgrade your decidim-app to the latest version
Expand All @@ -44,6 +45,7 @@ namespace :decidim_app do
task upgrade: :environment do
puts "Running db:migrate"
Rake::Task["db:migrate"].invoke
Rake::Task["decidim:awesome:private_fields"].invoke
end

desc "usage: bundle exec rails k8s:dump_db"
Expand Down
31 changes: 31 additions & 0 deletions lib/tasks/repair_data.rake
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,35 @@ namespace :decidim do
end
end
end

namespace :awesome do
desc "Migrate private fields from old proposals to new ones"
task private_fields: :environment do
Rails.logger.info("Migrating private fields from old proposals to new ones")

proposals = Decidim::Proposals::Proposal.where(
"private_body != ? AND private_body != ?",
"<xml></xml>", "{}"
).pluck(:id, :private_body)

Rails.logger.info("Preparing to migrate #{proposals.count} proposals")
creations = proposals.map do |proposal_id, private_body|
creation = Decidim::DecidimAwesome::PrivateProposalField.create(
proposal_id: proposal_id,
private_body: private_body
)

[proposal_id, creation]
end

if creations.any? { |_, creation| creation.invalid? }
proposal_ids = creations.select { |ary| ary.last.invalid? }.map(&:first)
Rails.logger.error("Failed to migrate #{proposal_ids.count} proposals")
Rails.logger.error("Failed proposals ID : #{proposal_ids.join(", ")}")
return
end

Rails.logger.info("Migrated #{creations.count} proposals")
end
end
end

0 comments on commit a9f9058

Please sign in to comment.