Fixed #2333 - Object country already exists

by adding the corresponding country attribute to the existing country column
This commit is contained in:
Billy Zhou 2018-11-08 20:53:41 +08:00 committed by Martin Edenhofer
parent f711e1019e
commit cd65dcdb40
3 changed files with 108 additions and 0 deletions

View file

@ -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

View file

@ -964,6 +964,41 @@ ObjectManager::Attribute.add(
position: 1300, 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( ObjectManager::Attribute.add(
force: true, force: true,
object: 'User', object: 'User',

View file

@ -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