Fixes #2664 - ObjectManager::Attribute names collide with reserved application words ( e.g. url
) and break functionalities
This commit is contained in:
parent
59f9f1a1b6
commit
106bfd56da
3 changed files with 36 additions and 5 deletions
|
@ -901,7 +901,7 @@ is certain attribute used by triggers, overviews or schedulers
|
||||||
raise 'At least one letters is needed' if !name.match?(/[a-z]/)
|
raise 'At least one letters is needed' if !name.match?(/[a-z]/)
|
||||||
|
|
||||||
# do not allow model method names as attributes
|
# do not allow model method names as attributes
|
||||||
reserved_words = %w[destroy true false integer select drop create alter index table varchar blob date datetime timestamp]
|
reserved_words = %w[destroy true false integer select drop create alter index table varchar blob date datetime timestamp url icon initials avatar permission validate subscribe unsubscribe translate search]
|
||||||
raise "#{name} is a reserved word, please choose a different one" if name.match?(/^(#{reserved_words.join('|')})$/)
|
raise "#{name} is a reserved word, please choose a different one" if name.match?(/^(#{reserved_words.join('|')})$/)
|
||||||
|
|
||||||
# fixes issue #2236 - Naming an attribute "attribute" causes ActiveRecord failure
|
# fixes issue #2236 - Naming an attribute "attribute" causes ActiveRecord failure
|
||||||
|
|
29
db/migrate/20190724000001_rename_reserved_words.rb
Normal file
29
db/migrate/20190724000001_rename_reserved_words.rb
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
class RenameReservedWords < ActiveRecord::Migration[5.1]
|
||||||
|
def up
|
||||||
|
return if !Setting.find_by(name: 'system_init_done')
|
||||||
|
|
||||||
|
models = ObjectManager.list_objects.map(&:underscore).map { |object| object.tr('_', '/') }.map(&:classify).map(&:constantize)
|
||||||
|
|
||||||
|
reserved_words = %w[url icon initials avatar permission validate subscribe unsubscribe translate search]
|
||||||
|
models.each do |model|
|
||||||
|
|
||||||
|
reserved_words.each do |reserved_word|
|
||||||
|
|
||||||
|
next if ActiveRecord::Base.connection.columns(model.table_name).map(&:name).exclude?(reserved_word)
|
||||||
|
|
||||||
|
sanitized_name = "_#{reserved_word}"
|
||||||
|
|
||||||
|
ActiveRecord::Migration.rename_column(model.table_name.to_sym, reserved_word.to_sym, sanitized_name.to_sym)
|
||||||
|
model.reset_column_information
|
||||||
|
|
||||||
|
attribute = ObjectManager::Attribute.get(
|
||||||
|
object: model.to_app_model,
|
||||||
|
name: reserved_word,
|
||||||
|
)
|
||||||
|
next if !attribute
|
||||||
|
|
||||||
|
attribute.update!(name: sanitized_name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -57,10 +57,12 @@ RSpec.describe ObjectManager::Attribute, type: :model do
|
||||||
end.to raise_error 'attribute is a reserved word, please choose a different one'
|
end.to raise_error 'attribute is a reserved word, please choose a different one'
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'rejects Zammad reserved word "table"' do
|
%w[destroy true false integer select drop create alter index table varchar blob date datetime timestamp url icon initials avatar permission validate subscribe unsubscribe translate search].each do |reserved_word|
|
||||||
expect do
|
it "rejects Zammad reserved word '#{reserved_word}'" do
|
||||||
ObjectManager::Attribute.add attributes_for :object_manager_attribute_text, name: 'table'
|
expect do
|
||||||
end.to raise_error 'table is a reserved word, please choose a different one'
|
ObjectManager::Attribute.add attributes_for :object_manager_attribute_text, name: reserved_word
|
||||||
|
end.to raise_error "#{reserved_word} is a reserved word, please choose a different one"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'rejects duplicate attribute name of conflicting types' do
|
it 'rejects duplicate attribute name of conflicting types' do
|
||||||
|
|
Loading…
Reference in a new issue