-
Notifications
You must be signed in to change notification settings - Fork 37
TAN-5720 Inconsistent custom fields ordering (Part 2) #12401
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
base: master
Are you sure you want to change the base?
Changes from 9 commits
1f69aa2
599078e
071c0fa
98c0905
429c576
f7507c2
913171a
a3e1692
5cfe6e7
652118b
db910c7
f4c0467
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| class AddUniquenessConstraintToCustomFieldsOrdering < ActiveRecord::Migration[7.1] | ||
| disable_ddl_transaction! | ||
|
|
||
| def change | ||
| # Input forms | ||
| add_index :custom_fields, %i[resource_id ordering], | ||
| unique: true, | ||
| name: 'index_custom_fields_on_resource_id_and_ordering_unique', | ||
| algorithm: :concurrently | ||
|
|
||
| # Registration fields (no resource_id) | ||
| add_index :custom_fields, :ordering, | ||
| unique: true, | ||
| where: 'resource_id IS NULL', | ||
| algorithm: :concurrently | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -160,7 +160,7 @@ def update_fields!(page_temp_ids_to_ids_mapping, option_temp_ids_to_ids_mapping) | |
| end | ||
| update_statements! field, statements_params, errors, index if statements_params | ||
| relate_map_config_to_field(field, field_params, errors, index) | ||
| field.set_list_position(index) | ||
| field.move_to_bottom | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this work fine where new fields are added in the middle of the form? I have it in my mind that we changed update_all a while back so that it was not making updates to every field, every time. ie the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jamesspeake I wasn't aware of this change. Will do more testing around these cases (new field in the middle and the other field) and will likely use a different method to insert at that position. But it would have to be a different method because |
||
| count_fields(field) | ||
| end | ||
| raise UpdateAllFailedError, errors if errors.present? | ||
|
|
||
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| namespace :fix_existing_tenants do | ||
| desc 'Fix custom fields orderings and make sure the first field is a page.' | ||
| task :custom_fields_orderings, [] => [:environment] do | ||
| reporter = ScriptReporter.new | ||
| Tenant.all.each do |tenant| | ||
| tenant.switch do | ||
| # rubocop:disable Performance/CollectionLiteralInLoop | ||
| ([nil] + CustomForm.all).each do |form| | ||
| fields = CustomField.where(resource_id: form&.id).order(:ordering) | ||
|
|
||
| # Check if ordering is correct | ||
| next if fields.pluck(:ordering) == (0...fields.size).to_a | ||
|
|
||
| # Fix orderings | ||
| fields.each.with_index do |field, index| | ||
| field.update_column(:ordering, index) | ||
| end | ||
| reporter.add_change( | ||
| nil, | ||
| 'Fixed orderings', | ||
| context: { tenant: tenant.host, form: form&.id } | ||
| ) | ||
|
|
||
| # First field must be a page | ||
| next if !form | ||
|
|
||
| fields = CustomField.where(resource_id: form.id).order(:ordering) | ||
| next if fields.first.page? | ||
|
|
||
| first_page = fields.find(&:page?) | ||
| if !first_page | ||
| reporter.add_error( | ||
| 'Form has no pages!', | ||
| context: { tenant: tenant.host, form: form.id } | ||
| ) | ||
| next | ||
| end | ||
|
|
||
| first_page.move_to_top | ||
| reporter.add_change( | ||
| nil, | ||
| 'Page moved to top', | ||
| context: { tenant: tenant.host, form: form.id, page: first_page.id } | ||
| ) | ||
| end | ||
| # rubocop:enable Performance/CollectionLiteralInLoop | ||
| end | ||
| end | ||
| reporter.report!('fix_custom_fields_orderings_report.json', verbose: true) | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| require 'rails_helper' | ||
|
|
||
| describe 'fix_existing_tenants:custom_fields_orderings rake task' do | ||
| before { load_rake_tasks_if_not_loaded } | ||
|
|
||
| it 'Fixes the orderings' do | ||
| form1 = create(:custom_form) | ||
| create(:custom_field, resource: form1, input_type: 'number', key: 'field2') | ||
| create(:custom_field_page, resource: form1, key: 'field1').update_column(:ordering, 0) | ||
| create(:custom_field, resource: form1, input_type: 'number', key: 'field3').update_column(:ordering, 3) | ||
| create(:custom_field, resource: form1, input_type: 'text', key: 'field4').update_column(:ordering, 3) | ||
| create(:custom_field_page, resource: form1, key: 'field5').update_column(:ordering, 4) | ||
|
|
||
| form2 = create(:custom_form) | ||
| create(:custom_field_page, resource: form2, key: 'field1') | ||
| create(:custom_field, resource: form2, input_type: 'number', key: 'field2') | ||
| create(:custom_field, resource: form2, input_type: 'text', key: 'field3') | ||
| create(:custom_field_page, resource: form2, key: 'field4') | ||
|
|
||
| create(:custom_field, resource_id: nil, resource_type: 'User', input_type: 'text', key: 'field1') | ||
| create(:custom_field, resource_id: nil, resource_type: 'User', input_type: 'number', key: 'field2').update_column(:ordering, 2) | ||
|
|
||
| Rake::Task['fix_existing_tenants:custom_fields_orderings'].invoke | ||
|
|
||
| expect(form1.reload.custom_fields.pluck(:key, :ordering)).to eq([ | ||
| ['field1', 0], | ||
| ['field2', 1], | ||
| ['field3', 2], | ||
| ['field4', 3], | ||
| ['field5', 4] | ||
| ]) | ||
|
|
||
| expect(form2.reload.custom_fields.pluck(:key, :ordering)).to eq([ | ||
| ['field1', 0], | ||
| ['field2', 1], | ||
| ['field3', 2], | ||
| ['field4', 3] | ||
| ]) | ||
|
|
||
| expect(CustomField.where(resource_id: nil).order(:ordering).pluck(:key, :ordering)).to eq([ | ||
| ['field1', 0], | ||
| ['field2', 1] | ||
| ]) | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Learn something every day. Did not know that you could have a conditional on an index
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it needed though? Does the first index not create an index on
[null, 1]for example?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jamesspeake Apparently uniqueness does not apply to null values. Duplicates like [null, 1] and [null, 1] will be allowed if we would make one index. That's why a separate index is needed to deal with the registration forms.