diff --git a/db/migrate/20181108123847_add_country_attribute_to_users.rb b/db/migrate/20181108123847_add_country_attribute_to_users.rb new file mode 100644 index 000000000..e3d6efd0f --- /dev/null +++ b/db/migrate/20181108123847_add_country_attribute_to_users.rb @@ -0,0 +1,50 @@ +# Fixes issue #2333 - Object country already exists +# The country column already exists in the database, but there is no corresponding ObjectManager::Attribute for it +# This migration adds the User.country attribute if and only if it does not exist already +class AddCountryAttributeToUsers < ActiveRecord::Migration[5.1] + def up + # return if it's a new setup + return if !Setting.find_by(name: 'system_init_done') + + # return if the country attribute already exists + current_country_attribute = ObjectManager::Attribute.find_by(object_lookup_id: ObjectLookup.by_name('User'), name: 'country') + return if current_country_attribute.present? + + ObjectManager::Attribute.add( + force: true, + object: 'User', + name: 'country', + display: 'Country', + data_type: 'input', + data_option: { + type: 'text', + maxlength: 100, + null: true, + item_class: 'formGroup--halfSize', + }, + editable: true, + active: false, + screens: { + signup: {}, + invite_agent: {}, + invite_customer: {}, + edit: { + '-all-' => { + null: true, + }, + }, + view: { + '-all-' => { + shown: true, + }, + }, + }, + to_create: false, + to_migrate: false, + to_delete: false, + position: 1325, + created_by_id: 1, + updated_by_id: 1, + ) + end +end diff --git a/db/seeds/object_manager_attributes.rb b/db/seeds/object_manager_attributes.rb index fd0f0df44..7eb41f229 100644 --- a/db/seeds/object_manager_attributes.rb +++ b/db/seeds/object_manager_attributes.rb @@ -964,6 +964,41 @@ ObjectManager::Attribute.add( position: 1300, ) +ObjectManager::Attribute.add( + force: true, + object: 'User', + name: 'country', + display: 'Country', + data_type: 'input', + data_option: { + type: 'text', + maxlength: 100, + null: true, + item_class: 'formGroup--halfSize', + }, + editable: true, + active: false, + screens: { + signup: {}, + invite_agent: {}, + invite_customer: {}, + edit: { + '-all-' => { + null: true, + }, + }, + view: { + '-all-' => { + shown: true, + }, + }, + }, + to_create: false, + to_migrate: false, + to_delete: false, + position: 1325, +) + ObjectManager::Attribute.add( force: true, object: 'User', diff --git a/spec/db/migrate/issue_2333_object_country_already_exists.rb b/spec/db/migrate/issue_2333_object_country_already_exists.rb new file mode 100644 index 000000000..bc09e56b2 --- /dev/null +++ b/spec/db/migrate/issue_2333_object_country_already_exists.rb @@ -0,0 +1,23 @@ +require 'rails_helper' + +RSpec.describe AddCountryAttributeToUsers, type: :db_migration do + + context 'AddCountryAttributeToUsers migration' do + + def country_attribute + ObjectManager::Attribute.find_by(object_lookup_id: ObjectLookup.by_name('User'), name: 'country') + end + + it 'preserves the existing country attribute' do + expect { migrate } + .not_to(change { country_attribute.present? }) + end + + it 'adds the country attribute when it is not present' do + country_attribute.delete + expect { migrate } + .to change { country_attribute.present? } + .from( false ).to( true ) + end + end +end