Skip to content

Commit

Permalink
Update migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
entantoencuanto committed Dec 5, 2024
1 parent abbd87d commit 2687f78
Show file tree
Hide file tree
Showing 29 changed files with 90 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

# This migration comes from decidim (originally 20181025082245)
class AddTimestampsToComponents < ActiveRecord::Migration[5.2]
class Component < ApplicationRecord
self.table_name = :decidim_components
end

def change
add_timestamps :decidim_components, null: true

# rubocop:disable Rails/SkipsModelValidations
Decidim::Component.update_all(created_at: Time.current, updated_at: Time.current)
Component.update_all(created_at: Time.current, updated_at: Time.current)
# rubocop:enable Rails/SkipsModelValidations

change_column_null :decidim_components, :created_at, false
change_column_null :decidim_components, :updated_at, false
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def change
add_index :decidim_forms_answers, :session_token

Answer.find_each do |answer|
answer.session_token = Digest::MD5.hexdigest("#{answer.decidim_user_id}-#{Rails.application.secrets.secret_key_base}")
answer.session_token = Digest::MD5.hexdigest("#{answer.decidim_user_id}-#{Rails.application.secret_key_base}")
answer.save!
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def change
reversible do |dir|
dir.up do
Decidim::ParticipatoryProcess.reset_column_information
Decidim::ParticipatoryProcess.find_each do |record|
Decidim::ParticipatoryProcess.unscoped.find_each do |record|
record.class.reset_counters(record.id, :follows)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# This migration comes from decidim_assemblies (originally 20190215093700)
class ResetNegativeChildrenCountCounters < ActiveRecord::Migration[5.2]
def change
ids = Decidim::Assembly.where("children_count < 0").pluck(:id)
ids.each { |id| Decidim::Assembly.reset_counters(id, :children_count) }
ids = Decidim::Assembly.unscoped.where("children_count < 0").pluck(:id)
ids.each { |id| Decidim::Assembly.unscoped.reset_counters(id, :children_count) }
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def change
reversible do |dir|
dir.up do
Decidim::Assembly.reset_column_information
Decidim::Assembly.find_each do |record|
Decidim::Assembly.unscoped.find_each do |record|
record.class.reset_counters(record.id, :follows)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class AddPublishedAtToProposals < ActiveRecord::Migration[5.1]
def up
add_column :decidim_proposals_proposals, :published_at, :datetime, index: true
# rubocop:disable Rails/SkipsModelValidations
Decidim::Proposals::Proposal.update_all("published_at = updated_at")
Decidim::Proposals::Proposal.unscoped.update_all("published_at = updated_at")
# rubocop:enable Rails/SkipsModelValidations
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# This migration comes from decidim_proposals (originally 20181016132225)
class AddOrganizationAsAuthor < ActiveRecord::Migration[5.2]
def change
official_proposals = Decidim::Proposals::Proposal.find_each.select do |proposal|
official_proposals = Decidim::Proposals::Proposal.unscoped.find_each.select do |proposal|
proposal.coauthorships.count.zero?
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def up
)
end
# update new `decidim_proposals_proposal.endorsements_count` counter cache
Decidim::Proposals::Proposal.select(:id).all.find_each do |proposal|
Decidim::Proposals::Proposal.unscoped.select(:id).all.find_each do |proposal|
Decidim::Proposals::Proposal.reset_counters(proposal.id, :endorsements)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ def change
add_column :decidim_proposals_proposals, :comments_count, :integer, null: false, default: 0, index: true
add_column :decidim_proposals_collaborative_drafts, :comments_count, :integer, null: false, default: 0, index: true
Decidim::Proposals::Proposal.reset_column_information
Decidim::Proposals::Proposal.find_each(&:update_comments_count)
Decidim::Proposals::CollaborativeDraft.reset_column_information
Decidim::Proposals::CollaborativeDraft.find_each(&:update_comments_count)
Decidim::Proposals::Proposal.unscoped.find_each(&:update_comments_count)
Decidim::Proposals::CollaborativeDraft.unscoped.reset_column_information
Decidim::Proposals::CollaborativeDraft.unscoped.find_each(&:update_comments_count)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def change
reversible do |dir|
dir.up do
Decidim::Proposals::Proposal.reset_column_information
Decidim::Proposals::Proposal.find_each do |record|
Decidim::Proposals::Proposal.unscoped.find_each do |record|
record.class.reset_counters(record.id, :follows)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def up

CustomProposal.withdrawn.find_each do |proposal|
proposal.withdrawn_at = proposal.updated_at
proposal.state = :not_answered
proposal.save!
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def change
t.string :token, null: false
t.references :decidim_component, index: true, null: false
t.integer :proposals_count, default: 0, null: false
t.string :css_class
t.string :bg_color, default: "#F6F8FA", null: false
t.string :text_color, default: "#4B5058", null: false
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# This migration comes from decidim_proposals (originally 20240110203504)
class CreateDefaultProposalStates < ActiveRecord::Migration[6.1]
class Proposal < ApplicationRecord
class CustomProposal < ApplicationRecord
belongs_to :proposal_state,
class_name: "Decidim::Proposals::ProposalState",
foreign_key: "decidim_proposals_proposal_state_id",
Expand All @@ -15,13 +15,16 @@ class Proposal < ApplicationRecord
end

def up
Decidim::Component.where(manifest_name: "proposals").find_each do |component|
CustomProposal.reset_column_information
Decidim::Proposals::ProposalState.reset_column_information
Decidim::Component.unscoped.where(manifest_name: "proposals").find_each do |component|
admin_user = component.organization.admins.first
Decidim::Proposals.create_default_states!(component, admin_user)

default_states = Decidim::Proposals.create_default_states!(component, admin_user)
CustomProposal.where(decidim_component_id: component.id).find_each do |proposal|
next if proposal.old_state == "not_answered"

Proposal.where(decidim_component_id: component.id).find_each do |proposal|
proposal.update!(proposal_state: default_states.dig(proposal.old_state.to_sym, :object))
proposal.update!(proposal_state: Decidim::Proposals::ProposalState.where(component:, token: proposal.old_state).first!)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ def self.colors
def up
colors = ProposalState.colors

add_column :decidim_proposals_proposal_states, :bg_color, :string, default: colors[:gray][:background], null: false
add_column :decidim_proposals_proposal_states, :text_color, :string, default: colors[:gray][:foreground], null: false
remove_column :decidim_proposals_proposal_states, :css_class

# rubocop:disable Rails/SkipsModelValidations
ProposalState.where(token: :accepted).update_all(
bg_color: colors[:green][:background], text_color: colors[:green][:foreground]
Expand All @@ -48,8 +44,6 @@ def up
end

def down
remove_column :decidim_proposals_proposal_states, :bg_color
remove_column :decidim_proposals_proposal_states, :text_color
add_column :decidim_proposals_proposal_states, :css_class, :string
raise ActiveRecord::IrreversibleMigration
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def change
reversible do |dir|
dir.up do
Decidim::Proposals::Proposal.reset_column_information
Decidim::Proposals::Proposal.find_each do |record|
Decidim::Proposals::Proposal.unscoped.find_each do |record|
Decidim::Proposals::Proposal.reset_counters(record.id, :valuation_assignments)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class AddQuestionnaireToExistingMeetings < ActiveRecord::Migration[5.2]
def change
Decidim::Meetings::Meeting.transaction do
Decidim::Meetings::Meeting.find_each do |meeting|
Decidim::Meetings::Meeting.unscoped.find_each do |meeting|
if meeting.component.present? && meeting.questionnaire.blank?
meeting.update!(
questionnaire: Decidim::Forms::Questionnaire.new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ class AddCommentableCounterCacheToMeetings < ActiveRecord::Migration[5.2]
def change
add_column :decidim_meetings_meetings, :comments_count, :integer, null: false, default: 0
Decidim::Meetings::Meeting.reset_column_information
Decidim::Meetings::Meeting.find_each(&:update_comments_count)
Decidim::Meetings::Meeting.unscoped.find_each(&:update_comments_count)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def up
reset_column_information

PaperTrail.request(enabled: false) do
Decidim::Meetings::Meeting.find_each do |meeting|
Decidim::Meetings::Meeting.unscoped.find_each do |meeting|
next if meeting.component.nil?
# Only user-created meetings have this problem
next if meeting.official?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def change
reversible do |dir|
dir.up do
Decidim::Meetings::Meeting.reset_column_information
Decidim::Meetings::Meeting.find_each do |record|
Decidim::Meetings::Meeting.unscoped.find_each do |record|
record.class.reset_counters(record.id, :follows)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ class AddCommentableCounterCacheToProjects < ActiveRecord::Migration[5.2]
def change
add_column :decidim_budgets_projects, :comments_count, :integer, null: false, default: 0, index: true
Decidim::Budgets::Project.reset_column_information
Decidim::Budgets::Project.find_each(&:update_comments_count)
Decidim::Budgets::Project.unscoped.find_each(&:update_comments_count)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def change
reversible do |dir|
dir.up do
Decidim::Budgets::Project.reset_column_information
Decidim::Budgets::Project.find_each do |record|
Decidim::Budgets::Project.unscoped.find_each do |record|
record.class.reset_counters(record.id, :follows)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ class AddCommentableCounterCacheToResults < ActiveRecord::Migration[5.2]
def change
add_column :decidim_accountability_results, :comments_count, :integer, null: false, default: 0, index: true
Decidim::Accountability::Result.reset_column_information
Decidim::Accountability::Result.find_each(&:update_comments_count)
Decidim::Accountability::Result.unscoped.find_each(&:update_comments_count)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def change
Decidim::Debates::Debate.reset_column_information

# rubocop:disable Rails/SkipsModelValidations
Decidim::Debates::Debate.includes(:comments).find_each do |debate|
Decidim::Debates::Debate.unscoped.includes(:comments).find_each do |debate|
debate.update_columns(comments_count: debate.comments.not_hidden.count)
end
# rubocop:enable Rails/SkipsModelValidations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def change

# rubocop:disable Rails/SkipsModelValidations
Decidim::Debates::Debate.reset_column_information
Decidim::Debates::Debate.includes(comments: [:author, :user_group]).find_each do |debate|
Decidim::Debates::Debate.unscoped.includes(comments: [:author, :user_group]).find_each do |debate|
last_comment = debate.comments.order("created_at DESC").first
next unless last_comment

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def change
reversible do |dir|
dir.up do
Decidim::Debates::Debate.reset_column_information
Decidim::Debates::Debate.find_each do |record|
Decidim::Debates::Debate.unscoped.find_each do |record|
record.class.reset_counters(record.id, :follows)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ class AddCommentableCounterCacheToPosts < ActiveRecord::Migration[5.2]
def change
add_column :decidim_blogs_posts, :comments_count, :integer, null: false, default: 0, index: true
Decidim::Blogs::Post.reset_column_information
Decidim::Blogs::Post.find_each(&:update_comments_count)
Decidim::Blogs::Post.unscoped.find_each(&:update_comments_count)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def change
reversible do |dir|
dir.up do
Decidim::Blogs::Post.reset_column_information
Decidim::Blogs::Post.find_each do |record|
Decidim::Blogs::Post.unscoped.find_each do |record|
record.class.reset_counters(record.id, :follows)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def change
reversible do |dir|
dir.up do
Decidim::Conference.reset_column_information
Decidim::Conference.find_each do |record|
Decidim::Conference.unscoped.find_each do |record|
record.class.reset_counters(record.id, :follows)
end
end
Expand Down
Loading

0 comments on commit 2687f78

Please sign in to comment.