From 99310f908daf94b904820ba97a1661abec3a00ba Mon Sep 17 00:00:00 2001
From: quentinchampenois <26109239+Quentinchampenois@users.noreply.github.com>
Date: Thu, 21 Mar 2024 12:11:53 +0100
Subject: [PATCH] feat: Bump awesome and add rake task for migration
---
Gemfile.lock | 2 +-
...proposal_fields.decidim_decidim_awesome.rb | 13 ++++++++
lib/tasks/repair_data.rake | 31 +++++++++++++++++++
3 files changed, 45 insertions(+), 1 deletion(-)
create mode 100644 db/migrate/20240314092535_create_private_proposal_fields.decidim_decidim_awesome.rb
diff --git a/Gemfile.lock b/Gemfile.lock
index 82575858..8eda9360 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -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)
diff --git a/db/migrate/20240314092535_create_private_proposal_fields.decidim_decidim_awesome.rb b/db/migrate/20240314092535_create_private_proposal_fields.decidim_decidim_awesome.rb
new file mode 100644
index 00000000..fe8cee4b
--- /dev/null
+++ b/db/migrate/20240314092535_create_private_proposal_fields.decidim_decidim_awesome.rb
@@ -0,0 +1,13 @@
+# 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: ''
+
+ 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
diff --git a/lib/tasks/repair_data.rake b/lib/tasks/repair_data.rake
index 9d0a527a..512b28a6 100644
--- a/lib/tasks/repair_data.rake
+++ b/lib/tasks/repair_data.rake
@@ -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 != ?",
+ "", "{}"
+ ).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