jpignata/temping

"Mysql2::Error: SAVEPOINT active_record_1 does not exist"

zben opened this issue · 5 comments

zben commented

I am getting an mysql error when using this gem. the project uses mysql2 gem. is there a conflict there using both mysql2 and sqlite connection in the test? I am using database_cleaner so config.use_transactional_fixtures = false.

Using Rails 3.2.13, mysql2 (0.3.11), sqlite3 (1.3.10) , ruby 1.9.2

Failure/Error: let(:person) { Person.create! }
     ActiveRecord::StatementInvalid:
       Mysql2::Error: SAVEPOINT active_record_1 does not exist: ROLLBACK TO SAVEPOINT active_record_1

I was able to run the following OK in rails c test though

 Temping.create :dog do
        with_columns do |t|
          t.string :name
          t.integer :age, :weight
        end
      end

      Dog.create

Are you using both sqlite and mysql in test environment?

I'm getting the same error, not using sqlite at all, just MySQL. Seem to get the same error whether using truncation or transaction for database cleaner.

I fixed this by calling DatabaseCleaner.clean AFTER Temping.teardown

To work around this issue, I've added a new type to force temping-based, non-transactional specs to leverage the truncation strategy:

RSpec.configure do |config|
  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, type: :temping) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after do
    Temping.teardown
  end

  config.append_after(:each) do
    DatabaseCleaner.clean
  end
end
describe 'foobar', type: :temping do
  # …
end

To anyone who's using Minitest:

I ended up turning off transactional tests (for this single test) and manually used DatabaseCleaner with the truncation strategy.

class ExampleTest < ActiveSupport::TestCase
  self.use_transactional_tests = false

  setup do
    DatabaseCleaner.strategy = :truncation
    DatabaseCleaner.start
    Temping.create :post do
      with_columns do |t|
        t.datetime :created_at
      end
    end
  end

  teardown do
    Temping.teardown
    DatabaseCleaner.clean
  end
end