Search code examples
mysqlrubyrspecsinatradatamapper

Is there a way to stop increasing the auto increment value in mysql when running specs?


I have about 100 specs, In a Sinatra project I'm currently developing. For testing I'm currently using rspec + DatabaseCleaner with the :transaction strategy.

Some of those specs require creating new records in the database and thus they increase the auto increment table value in mysql. Is there a way I can run my tests with the transaction strategy, without increasing the auto increment value using DatabaseCleaner or another gem?

Edit: I would like for auto_increment to have the value it had before the tests ran.


Solution

  • I am assuming that at the time of test no record will insert from production or outside test.

    First get last auto_id from your table by below command-

    select max(id) from mytable;
    

    Suppose it was 240125

    Now start your test and after completion your test do as per below-

    First delete all newly created auto_ids-

    delete from mytable where id>240125;
    

    Now set auto_increment from this possition.

    alter table mytable AUTO_INCREMENT=240126;