简体   繁体   English

运行规范时,有没有办法停止增加mysql中的自动增量值?

[英]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. 我有大约100个规格,我正在开发一个Sinatra项目。 For testing I'm currently using rspec + DatabaseCleaner with the :transaction strategy. 为了进行测试,我目前使用带有:transaction策略的rspec + DatabaseCleaner。

Some of those specs require creating new records in the database and thus they increase the auto increment table value in mysql. 这些规范中的某些要求在数据库中创建新记录,因此会增加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? 有没有一种方法可以使用事务策略运行测试,而无需使用DatabaseCleaner或其他gem增加自动增量值?

Edit: I would like for auto_increment to have the value it had before the tests ran. 编辑:我希望auto_increment具有测试运行之前的值。

Reset the value to some known value after tests. 测试后将值重置为某个已知值。 Have a look here... 在这里看看...

How to reset AUTO_INCREMENT in MySQL? 如何在MySQL中重置AUTO_INCREMENT?

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- 首先通过以下命令从表中获取最后一个auto_id:

select max(id) from mytable;

Suppose it was 240125 假设是240125

Now start your test and after completion your test do as per below- 现在开始测试,并在完成测试后按照以下步骤进行:

First delete all newly created auto_ids- 首先删除所有新创建的auto_ids-

delete from mytable where id>240125;

Now set auto_increment from this possition. 现在从此位置设置auto_increment。

alter table mytable AUTO_INCREMENT=240126;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM