Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resetting autoincrement in h2

I'm testing a controller that returns a json response but the tests fail after the first time because the h2 database does not reset the auto increment id. Using fixtures or creating objects manually has a same problem.

@Before
public void setUp() {
    Fixtures.deleteAllModels();
    Fixtures.loadModels("data.yaml");
}

How to solve this problem?

like image 580
marsellvov Avatar asked Apr 08 '12 18:04

marsellvov


2 Answers

If you are using Spring, I found this blog post which suggests using @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)

So that each test uses a new application context, thereby resetting the db increments.

like image 180
Benjamin Slabbert Avatar answered Oct 05 '22 05:10

Benjamin Slabbert


Start your play app, fire up browser with this url (if you run play app locally):

http://localhost:9000/@db

Enter your h2 db, and type the command below and run:

ALTER TABLE <table_name> ALTER COLUMN <column_name> RESTART WITH 1

If you'd like to do this programmatically, Fixtures.executeSQL() might be useful

For more information, check http://www.h2database.com/html/grammar.html#alter_table_alter

like image 23
dwi2 Avatar answered Oct 05 '22 05:10

dwi2