Skip to content

Commit

Permalink
Merge pull request #11252 from tchak/fix-repetition-champs-migration-…
Browse files Browse the repository at this point in the history
…task

chore(migration): improve and fix task to remove unused champs and create rows
  • Loading branch information
tchak authored Jan 27, 2025
2 parents 975769a + 01ca426 commit dc37451
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,26 @@ def collection

def process(dossier)
Dossier.no_touching do
Champs::HeaderSectionChamp.where(dossier:).destroy_all
Champs::ExplicationChamp.where(dossier:).destroy_all
Champs::RepetitionChamp.where(dossier:, row_id: nil).destroy_all
champ_to_remove_ids = dossier
.champs
.filter { !_1.row? && (_1.repetition? || _1.header_section? || _1.explication?) }
.map(&:id)
dossier.champs.where(id: champ_to_remove_ids).destroy_all

Dossier.transaction { create_rows(dossier) }
create_rows(dossier)
end
end

def create_rows(dossier)
repetitions = dossier.revision.types_de_champ.filter(&:repetition?)
repetitions.each do |type_de_champ|
stable_id = type_de_champ.stable_id
row_ids = dossier.repetition_row_ids(type_de_champ)
row_ids.each do |row_id|
Champ.create_with(**type_de_champ.params_for_champ)
.create_or_find_by!(dossier:, stable_id:, row_id:, stream: 'main')
end
existing_row_ids = dossier.champs.filter(&:row?).to_set(&:row_id)
now = Time.zone.now
row_attributes = { created_at: now, updated_at: now, dossier_id: dossier.id }
new_rows = repetitions.flat_map do |type_de_champ|
row_ids = dossier.repetition_row_ids(type_de_champ).to_set - existing_row_ids
row_ids.map { type_de_champ.params_for_champ.merge(row_id: _1, **row_attributes) }
end
Champ.insert_all!(new_rows) if new_rows.present?
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@ module Maintenance
dossier.champs.create(**header_section.params_for_champ)
dossier.champs.create(**explication.params_for_champ)
dossier.champs.create(**repetition.params_for_champ)
dossier.reload
}

it { expect { subject }.not_to change { dossier.reload.updated_at } }
it { expect { subject }.to change { Champ.count }.by(-3) }
it { expect { subject }.to change { dossier.champs.count }.by(-3) }
end

describe "create rows" do
subject(:process) { described_class.process(dossier) }
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :repetition, children: [{}] }]) }
let(:dossier) { create(:dossier, :with_populated_champs, procedure:) }

before { dossier.champs.filter(&:row?).each(&:destroy!) }
before {
dossier.champs.filter(&:row?).each(&:destroy!)
dossier.reload
}

it { expect { subject }.not_to change { dossier.reload.updated_at } }
it { expect { subject }.to change { Champs::RepetitionChamp.count }.by(2) }
it { expect { subject }.to change { dossier.champs.where(type: 'Champs::RepetitionChamp').count }.by(2) }
end
end
end

0 comments on commit dc37451

Please sign in to comment.