0

I know that I shouldn't be coding to specific ids, but sometimes it just makes life a lot easier. I am using DatabaseCleaner with RSpec and Postgres and I am wondering if it is possible to reset ids in between specs when using the transaction strategy?

Here is my current spec_helper:

RSpec.configure do |config|

  config.expect_with :rspec do |c|
    c.syntax = [:should, :expect]
  end

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
    DatabaseCleaner.strategy = :transaction
    load_seed_data
  end

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

  config.after(:all) do
    DatabaseCleaner.clean
  end

  config.example_status_persistence_file_path = "./spec/failures.txt"
  config.use_transactional_fixtures = true    
  config.infer_base_class_for_anonymous_controllers = false    
  config.order = "random"

  config.include FactoryGirl::Syntax::Methods
  config.include RSpec::Rails::RequestExampleGroup
  config.include Requests::JsonHelpers
  config.include Requests::Extensions

end
Eli Duke
  • 454
  • 1
  • 3
  • 14
  • 2
    `ActiveRecord::Base.connection.tables.each { |t| ActiveRecord::Base.connection.reset_pk_sequence!(t) }` ?? – XML Slayer Aug 06 '15 at 17:54
  • So there isn't an official DatabaseCleaner way to do it? – Eli Duke Aug 06 '15 at 17:55
  • 3
    I know you acknowledge this, but you really shouldn't be trying to do this. You're going to run into more headaches down the road than you'll be solving in the short-run. I'm sure myself and others would be happy to help you figure out a proper solution to this if you posted another question :) – Ian Selby Aug 06 '15 at 17:57
  • 1
    And no, database cleaner doesn't have a way to do. – Ian Selby Aug 06 '15 at 17:58
  • @IanSelby Ok. Thanks for the clarification. I will figure out another way to do what I need to do. I know that hard-coding ids is wrong on many levels, but I'm only hard-coding SOME ids in the test suite, so it didn't seem like a big deal. I'll figure out a work-around. – Eli Duke Aug 06 '15 at 18:02

1 Answers1

0

This is not possible because autoincrement is realized through a mechanism called sequence, and they're not reverted in a transaction: https://stackoverflow.com/a/2095949/299774

Greg
  • 5,862
  • 1
  • 25
  • 52