Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: private proposal fields #52

Merged
merged 4 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading