mercredi 15 juillet 2015

How to use FactoryGirl with ActiveRecord transaction rails

I Use Factory Girl a lot to create fake data in my database. But whenever I create a new factory and if a error occurs I end up with lot of inconsistent data since it does not support transaction no rollback occurs For example (snapshot of console)

INSERT INTO INVOICES (created_by, creation_date, fulfillment_branch_id, invoice_date, invoice_number, invoice_status, last_updated, last_updated_by, order_id, order_status, parent_order_id) VALUES ('xx.xx@xx.net', '2015-07-16 11:27:00', '1035280', NULL, NULL, 'Active', '2015-07-16 11:27:00', 'xx.xx@xx.net', 24, 'Invoiced', NULL)

SQL (0.8ms) INSERT INTO packages (created_at, created_by, shipment_id, size, status, updated_at, updated_by, value, weight) VALUES ('2015-07-16 11:27:00', 49, NULL, 'LBH', 'active', '2015-07-16 11:27:00', 49, 2448.0, 0.32950726052542545) ActiveRecord::StatementInvalid: Mysql2::Error: Column 'shipment_id' cannot be null: INSERT INTO packages (created_at, created_by, shipment_id, size, status, updated_at, updated_by, value, weight) VALUES ('2015-07-16 11:27:00', 49, NULL, 'LBH', 'active', '2015-07-16 11:27:00', 49, 2448.0, 0.32950726052542545) from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in query' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:inblock in execute' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:280:in block in log' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:ininstrument' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/db-charmer-1.9.0/lib/db_charmer/rails3/abstract_adapter/connection_name.rb:14:in instrument' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:275:inlog' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in execute' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:211:inexecute' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:234:in exec_insert' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:90:ininsert' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in insert' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/relation.rb:66:ininsert' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/persistence.rb:367:in create' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/timestamp.rb:58:increate' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/callbacks.rb:268:in block in create' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:414:in_run__221280857__create__997484455__callbacks' ... 58 levels... from /home/infibeam/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/1.9.1/observer.rb:194:in notify_observers' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/evaluation.rb:20:innotify' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/strategy/create.rb:13:in block in result' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/strategy/create.rb:9:intap' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/strategy/create.rb:9:in result' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/factory.rb:42:inrun' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/factory_runner.rb:23:in block in run' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activesupport-3.2.13/lib/active_support/notifications.rb:125:ininstrument' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/factory_runner.rb:22:in run' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:inblock in define_singular_strategy_method' from (irb):4 from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in start' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/railties-3.2.13/lib/rails/commands/console.rb:8:instart' from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/railties-3.2.13/lib/rails/commands.rb:41:in <top (required)>' from script/rails:6:inrequire' from scripFactoryGirl.reload

Can anyone explain where I should monkey patch to ensure factory runs transactionally?

Thank you

Aucun commentaire:

Enregistrer un commentaire