Commit 6ca8461550e165e3c5eab6e45395486211502655

Authored by Rodrigo Souto
1 parent 9f22ba3e

custom-forms-migration: remove/avoid submissions without a form

The migration 20130823151900_associate_fields_to_alternatives.rb from
plugin CustomForms? was crashing due to submissions without a form on
the database. This is a database inconsistence since there shouldn't be
submissions without a form. To fix the problem I'm skipping the on the
migration which may crash with a rescue to allow new tries to execute
properly, creating a new migration to remove the submissions without
form from the database and adding the :dependent => :destroy on form
association with submission to avoid the problem in the future.

Some people bypassed the migration through manual ways. This people
should not worry since the cases which were crashing should not be
running at all.

ActionItem3130
plugins/custom_forms/db/migrate/20130823151900_associate_fields_to_alternatives.rb
... ... @@ -15,14 +15,18 @@ class AssociateFieldsToAlternatives < ActiveRecord::Migration
15 15 end
16 16  
17 17 CustomFormsPlugin::Answer.find_each do |answer|
18   - labels = []
19   - answer.value.split(',').each do |value|
20   - labels << answer.field.choices.invert[value]
21   - end
22   - labels.compact!
23   - if labels.present?
24   - answer.value = answer.field.alternatives.where('label IN (?)', labels).map(&:id).join(',')
25   - answer.save!
  18 + # Avoid crash due to database possible inconsistency on submissions without form
  19 + begin
  20 + labels = []
  21 + answer.value.split(',').each do |value|
  22 + labels << answer.field.choices.invert[value]
  23 + end
  24 + labels.compact!
  25 + if labels.present?
  26 + answer.value = answer.field.alternatives.where('label IN (?)', labels).map(&:id).join(',')
  27 + answer.save!
  28 + end
  29 + rescue
26 30 end
27 31 end
28 32  
... ...
plugins/custom_forms/db/migrate/20140505131703_remove_submissions_without_form.rb 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +class RemoveSubmissionsWithoutForm < ActiveRecord::Migration
  2 + def self.up
  3 + CustomFormsPlugin::Submission.find_each do |submission|
  4 + submission.destroy if submission.form.nil?
  5 + end
  6 + end
  7 +
  8 + def self.down
  9 + say "This migration is irreversible."
  10 + end
  11 +end
... ...
plugins/custom_forms/lib/custom_forms_plugin/form.rb
... ... @@ -4,7 +4,7 @@ class CustomFormsPlugin::Form &lt; Noosfero::Plugin::ActiveRecord
4 4 has_many :fields, :order => 'position', :class_name => 'CustomFormsPlugin::Field', :dependent => :destroy
5 5 accepts_nested_attributes_for :fields, :allow_destroy => true
6 6  
7   - has_many :submissions, :class_name => 'CustomFormsPlugin::Submission'
  7 + has_many :submissions, :class_name => 'CustomFormsPlugin::Submission', :dependent => :destroy
8 8  
9 9 serialize :access
10 10  
... ...
plugins/custom_forms/test/unit/custom_forms_plugin/form_test.rb
... ... @@ -260,4 +260,18 @@ class CustomFormsPlugin::FormTest &lt; ActiveSupport::TestCase
260 260 form2.destroy
261 261 assert_includes Task.canceled, task2
262 262 end
  263 +
  264 + should 'destroy submissions after form is destroyed' do
  265 + form = CustomFormsPlugin::Form.create!(:profile => fast_create(Profile), :name => 'Free Software')
  266 + s1 = CustomFormsPlugin::Submission.create!(:form => form, :profile => fast_create(Profile))
  267 + s2 = CustomFormsPlugin::Submission.create!(:form => form, :profile => fast_create(Profile))
  268 + form.destroy
  269 +
  270 + assert_raise ActiveRecord::RecordNotFound do
  271 + s1.reload
  272 + end
  273 + assert_raise ActiveRecord::RecordNotFound do
  274 + s2.reload
  275 + end
  276 + end
263 277 end
... ...