{ Josh Rendek }

<3 Ruby & Go

I was writing some cucumber features for reru_scrum when I ran into an issue with destroying user records and Mysql2 throwing a Lock error.

The full error:

1 Mysql2::Error: Lock wait timeout exceeded; try restarting transaction: UPDATE `users` SET `last_sign_in_at` = '2011-11-22 00:06:32', `current_sign_in_at` = '2011-11-22 00:11:28', `sign_in_count` = 3, `updated_at` = '2011-11-22 00:11:28' WHERE `users`.`id` = 1

A simple solution is to use the database_cleaner gem.

Inside your features/support/env.rb file:

1 begin
2   require 'database_cleaner'
3   require 'database_cleaner/cucumber'
4   DatabaseCleaner.strategy = :truncation
5 rescue NameError
6   raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
7 end

A good idea is to create the before and after hooks to use the DatabaseCleaner.start and DatabaseCleaner.clean methods.

Inside features/support/hooks.rb:

1 Before do
2   DatabaseCleaner.start
3 end
4 
5 After do |scenario|
6   DatabaseCleaner.clean
7 end

You should then be able to run your features and have your database cleaned between steps.

comments powered by Disqus