database_cleaner icon indicating copy to clipboard operation
database_cleaner copied to clipboard

cross-strategy function addition: randomizing initial insert_id

Open onyxraven opened this issue 11 years ago • 3 comments

An easy way to have invalid specs is to have model object's primary key IDs be the same, hiding mishandling of those ids when dealing with caching, etc.

Most (all?) of the backends which have autoincrement ids have the ability to set the starting point of that ID. Instead of resetting ids after truncate/delete, have the ability to randomize those ids.

I've currently got the very hacky test in my before(:suite) rspec block:

    DatabaseCleaner.connections.each do |c|
      s = c.create_strategy(:truncation)
      cxn = s.connection_class.connection
      if (cxn.send(:uses_sequence) rescue false)
        tables = s.send(:tables_to_truncate, cxn)
        tables.each do |table_name|
          cxn.execute("replace into sqlite_sequence (name,seq) values ('#{table_name}', #{rand(100000)});")
        end
      end
    end

It seems to do the job, obviously knows a lot about the internals of databasecleaner though, so its just a proof-of-concept. It's already pointed out a failing spec!

I'm happy to put together a PR, but wondering if I'm barking up the wrong tree here.

onyxraven avatar Sep 29 '15 21:09 onyxraven

@onyxraven I think randomizing the starting point shouldn't matter as long as all records get properly deleted/truncated. Could you provide a concrete scenario for this issue?

sirkosi avatar Dec 24 '15 14:12 sirkosi

@schmierkov In some cases, our app passes around IDs instead of full objects (queued items, etc), and assigns them. When every table potentially starts at id=1, accessing the wrong table/object can hide a broken spec when the IDs match up, but the tables do not. Randomizing the IDs has shown quite a few of those errors in the past, and seems like an easy way to raise those errors.

so for example, say a method mistakenly sends the wrong id:

class FirstTable < ActiveRecord::Base; end
class SecondTable < ActiveRecord::Base
  def do_work
    'work work'
  end
end

def use_second(second_id)
  second = SecondTable.find
  second.do_work
end

obj1 = FirstTable.new.save
obj2 = SecondTable.new.save

#mistake
output = use_second(obj1.id)

expect(output).to eq('work work')

This spec should throw a ActiveRecord::RecordNotFound, but won't because both obj1.id and obj2.id will == 1.

This example is somewhat contrived, but can mimic getting an object ID from another source (say a cache, api call, etc) without a specific class information.

onyxraven avatar Dec 28 '15 17:12 onyxraven

@onyxraven I think this problem is very particular to your application. The scenario that you are describing shouldn't normally happen.

The only way that this scenario would happen is if you have a configuration like this one:

DatabaseCleaner.strategy = :truncation, {:except => %w[first_tables second_tables]}

That means that records in FirstTable and SecondTable don't get cleaned. I strongly suggest users not to do that, because they will end up with random problems due to a dirty database.

Good candidates for the except option are tables that don't change like countries or states

I'm going to close this issue, but feel free to open an issue with a description of the invalid specs that you mentioned in your initial post.

etagwerker avatar Dec 31 '15 14:12 etagwerker