Bug 1588417 - unable to migrate from 5.6 to 5.9 due to to a database validation error
Summary: unable to migrate from 5.6 to 5.9 due to to a database validation error
Keywords:
Status: CLOSED CURRENTRELEASE
Alias: None
Product: Red Hat CloudForms Management Engine
Classification: Red Hat
Component: Appliance
Version: 5.9.0
Hardware: All
OS: All
high
high
Target Milestone: GA
: 5.10.0
Assignee: Brandon Dunne
QA Contact: luke couzens
URL:
Whiteboard:
Depends On:
Blocks: 1561041 1591450
TreeView+ depends on / blocked
 
Reported: 2018-06-07 09:42 UTC by Felix Dewaleyne
Modified: 2021-09-09 14:26 UTC (History)
10 users (show)

Fixed In Version: 5.10.0.0
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
: 1591450 (view as bug list)
Environment:
Last Closed: 2019-02-11 14:08:13 UTC
Category: ---
Cloudforms Team: ---
Target Upstream Version:
Embargoed:


Attachments (Terms of Use)

Description Felix Dewaleyne 2018-06-07 09:42:20 UTC
Description of problem:
unable to migrate from 5.6 to 5.9 due to to a database validation error 

Version-Release number of selected component (if applicable):
migration of a central region from 4.1 to 4.6 (5.6 to 5.9)

How reproducible:
all the time on customer environment

Steps to Reproduce:
1.import database backup provided internally
2.migrate to 5.9 using the official steps
3.start the appliance

Actual results:
[----] I, [2018-04-26T11:47:56.114474 #17214:ccb118]  INFO -- : MIQ(CustomizationTemplate.seed) Updating {:name=>"SSH key addition template", :type=>"CustomizationTemplateCloudInit", :description=>"This template enables placing ssh public key in authorized keys"}
[----] E, [2018-04-26T11:47:56.569899 #17214:ccb118] ERROR -- : [ActiveRecord::RecordInvalid]: Validation failed: Name has already been taken  Method:[block in method_missing]
[----] E, [2018-04-26T11:47:56.570134 #17214:ccb118] ERROR -- : /opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/validations.rb:78:in `raise_validation_error'
/opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/validations.rb:50:in `save!'
/opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/attribute_methods/dirty.rb:30:in `save!'

Expected results:
migration passes

Additional info:

We fail in this method:

	class CustomizationTemplate < ApplicationRecord
	- - - - - 8< - - - - - 
	  def self.seed
	    return unless self == base_class # Prevent subclasses from seeding

	    current = where(:system => true).index_by(&:name)

	    seed_data.each do |s|
	      log_attrs = s.slice(:name, :type, :description)

	      rec = current.delete(s[:name])
	      if rec.nil?
		_log.info("Creating #{log_attrs.inspect}")
		create!(s)
	      else
		rec.attributes = s.except(:type)
		if rec.changed?
		  _log.info("Updating #{log_attrs.inspect}")
		  rec.save!
		end
	      end
	    end

	    current.values.each do |rec|
	      log_attrs = rec.attributes.slice("id", "name", "type", "description").symbolize_keys
	      _log.info("Deleting #{log_attrs.inspect}")
	      rec.destroy
	    end
	  end

In this case, the rec.save! method fails, saving the record of the existing CustomizationTemplate.  The error thrown is:

	/opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/validations.rb:78:in `raise_validation_error': Validation failed: Name has already been taken (ActiveRecord::RecordInvalid)

So a field, `Name` is identical between two records in the database for CustomizationTemplate; we can see which one in the 'Updating' line right before it:

	[----] I, [2018-04-26T11:47:56.114474 #17214:ccb118]  INFO -- : MIQ(CustomizationTemplate.seed) Updating {:name=>"SSH key addition template", :type=>"CustomizationTemplateCloudInit", :description=>"This template enables placing ssh public key in authorized keys"}

As the :name field is 'SSH key addition template' it is likely that there is an attempt to import the exact same default template:

	# psql -U root -d vmdb_production

	vmdb_production=# SELECT name,type FROM customization_templates;
		   name            |              type              
	---------------------------+--------------------------------
	 Basic root pass template  | CustomizationTemplateCloudInit
	 SSH key addition template | CustomizationTemplateCloudInit		<----duplicated template?
	 oVirt cloud-init          | CustomizationTemplateCloudInit
	(3 rows)

Comment 2 Felix Dewaleyne 2018-06-07 10:08:57 UTC
full trace :

[----] I, [2018-04-26T11:47:56.114474 #17214:ccb118]  INFO -- : MIQ(CustomizationTemplate.seed) Updating {:name=>"SSH key addition template", :type=>"CustomizationTemplateCloudInit", :description=>"This template enables placing ssh public key in authorized keys"}
[----] E, [2018-04-26T11:47:56.569899 #17214:ccb118] ERROR -- : [ActiveRecord::RecordInvalid]: Validation failed: Name has already been taken  Method:[block in method_missing]
[----] E, [2018-04-26T11:47:56.570134 #17214:ccb118] ERROR -- : /opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/validations.rb:78:in `raise_validation_error'
/opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/validations.rb:50:in `save!'
/opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/attribute_methods/dirty.rb:30:in `save!'
/opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/transactions.rb:324:in `block in save!'
/opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/transactions.rb:395:in `block in with_transaction_returning_status'
/opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/database_statements.rb:230:in `transaction'
/opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/transactions.rb:211:in `transaction'
/opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/transactions.rb:392:in `with_transaction_returning_status'
/opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/transactions.rb:324:in `save!'
/opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/suppressor.rb:45:in `save!'
/var/www/miq/vmdb/app/models/customization_template.rb:37:in `block in seed'
/var/www/miq/vmdb/app/models/customization_template.rb:26:in `each'
/var/www/miq/vmdb/app/models/customization_template.rb:26:in `seed'
/var/www/miq/vmdb/lib/evm_database.rb:80:in `block (2 levels) in seed'
/var/www/miq/vmdb/lib/evm_database.rb:69:in `each'
/var/www/miq/vmdb/lib/evm_database.rb:69:in `block in seed'
/var/www/miq/vmdb/lib/extensions/ar_table_lock.rb:21:in `block (2 levels) in with_lock'
/opt/rh/rh-ruby23/root/usr/share/ruby/timeout.rb:91:in `block in timeout'
/opt/rh/rh-ruby23/root/usr/share/ruby/timeout.rb:33:in `block in catch'
/opt/rh/rh-ruby23/root/usr/share/ruby/timeout.rb:33:in `catch'
/opt/rh/rh-ruby23/root/usr/share/ruby/timeout.rb:33:in `catch'
/opt/rh/rh-ruby23/root/usr/share/ruby/timeout.rb:106:in `timeout'
/var/www/miq/vmdb/lib/extensions/ar_table_lock.rb:21:in `block in with_lock'
/opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `block in transaction'
/opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/transaction.rb:189:in `within_new_transaction'
/opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `transaction'
/opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/transactions.rb:211:in `transaction'
/var/www/miq/vmdb/lib/extensions/ar_table_lock.rb:15:in `with_lock'
/var/www/miq/vmdb/lib/evm_database.rb:68:in `seed'
/var/www/miq/vmdb/lib/evm_database.rb:56:in `seed_last'
/var/www/miq/vmdb/app/models/miq_server.rb:229:in `start'
/var/www/miq/vmdb/lib/workers/evm_server.rb:27:in `start'
/var/www/miq/vmdb/lib/workers/evm_server.rb:48:in `start'
/var/www/miq/vmdb/lib/workers/bin/evm_server.rb:4:in `<main>'
/opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/validations.rb:78:in `raise_validation_error': Validation failed: Name has already been taken (ActiveRecord::RecordInvalid)
        from /opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/validations.rb:50:in `save!'
        from /opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/attribute_methods/dirty.rb:30:in `save!'
        from /opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/transactions.rb:324:in `block in save!'
        from /opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/transactions.rb:395:in `block in with_transaction_returning_status'
        from /opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/database_statements.rb:230:in `transaction'
        from /opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/transactions.rb:211:in `transaction'
        from /opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/transactions.rb:392:in `with_transaction_returning_status'
        from /opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/transactions.rb:324:in `save!'
        from /opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/suppressor.rb:45:in `save!'
        from /var/www/miq/vmdb/app/models/customization_template.rb:37:in `block in seed'
        from /var/www/miq/vmdb/app/models/customization_template.rb:26:in `each'
        from /var/www/miq/vmdb/app/models/customization_template.rb:26:in `seed'
        from /var/www/miq/vmdb/lib/evm_database.rb:80:in `block (2 levels) in seed'
        from /var/www/miq/vmdb/lib/evm_database.rb:69:in `each'
        from /var/www/miq/vmdb/lib/evm_database.rb:69:in `block in seed'
        from /var/www/miq/vmdb/lib/extensions/ar_table_lock.rb:21:in `block (2 levels) in with_lock'
        from /opt/rh/rh-ruby23/root/usr/share/ruby/timeout.rb:91:in `block in timeout'
        from /opt/rh/rh-ruby23/root/usr/share/ruby/timeout.rb:33:in `block in catch'
        from /opt/rh/rh-ruby23/root/usr/share/ruby/timeout.rb:33:in `catch'
        from /opt/rh/rh-ruby23/root/usr/share/ruby/timeout.rb:33:in `catch'
        from /opt/rh/rh-ruby23/root/usr/share/ruby/timeout.rb:106:in `timeout'
        from /var/www/miq/vmdb/lib/extensions/ar_table_lock.rb:21:in `block in with_lock'
        from /opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `block in transaction'
        from /opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/transaction.rb:189:in `within_new_transaction'
        from /opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `transaction'
        from /opt/rh/cfme-gemset/gems/activerecord-5.0.6/lib/active_record/transactions.rb:211:in `transaction'
        from /var/www/miq/vmdb/lib/extensions/ar_table_lock.rb:15:in `with_lock'
        from /var/www/miq/vmdb/lib/evm_database.rb:68:in `seed'
        from /var/www/miq/vmdb/lib/evm_database.rb:56:in `seed_last'
        from /var/www/miq/vmdb/app/models/miq_server.rb:229:in `start'
        from /var/www/miq/vmdb/lib/workers/evm_server.rb:27:in `start'
        from /var/www/miq/vmdb/lib/workers/evm_server.rb:48:in `start'
        from /var/www/miq/vmdb/lib/workers/bin/evm_server.rb:4:in `<main>'

Comment 9 CFME Bot 2018-06-13 14:36:45 UTC
New commit detected on ManageIQ/manageiq/master:

https://github.com/ManageIQ/manageiq/commit/0c2bf68625ccf36d421dc139bf7b81d42a6ad39f
commit 0c2bf68625ccf36d421dc139bf7b81d42a6ad39f
Author:     Joe Rafaniello <jrafanie>
AuthorDate: Thu Jun  7 16:37:19 2018 -0400
Commit:     Joe Rafaniello <jrafanie>
CommitDate: Thu Jun  7 16:37:19 2018 -0400

    Allow duplicate nil pxe_image_types during seed

    https://bugzilla.redhat.com/show_bug.cgi?id=1588417

    We want to disallow the same name template in different regions with the
    same pxe_image_type.  Normally, region id sequences prevent this from
    happening but we were treating nil pxe_image_types as the same.
    Therefore you could have the "same" pxe_image_type in different regions
    for the same name template.

 app/models/customization_template.rb | 6 +-
 spec/models/customization_template_spec.rb | 57 +
 2 files changed, 62 insertions(+), 1 deletion(-)

Comment 10 Joe Rafaniello 2018-06-13 16:06:29 UTC
https://github.com/ManageIQ/manageiq/pull/17544 has been merged and marked for backport to 5.9 along with the dependent PR: https://github.com/ManageIQ/manageiq/pull/16775

If a hotfix is needed, please request them through the normal process.

Comment 12 luke couzens 2018-06-26 08:41:49 UTC
Verified in 5.10.0.1


Note You need to log in before you can comment on or make changes to this bug.