springtestdbunit / spring-test-dbunit

Integration between the Spring testing framework and DBUnit
http://springtestdbunit.github.com/spring-test-dbunit/
Apache License 2.0
476 stars 238 forks source link

Reset BIGSERIAL counter on TRUNCATE_TABLE in PostgreSQL #131

Open raderio opened 7 years ago

raderio commented 7 years ago

CREATE TABLE accounts (id BIGSERIAL NOT NULL PRIMARY KEY);

@DatabaseTearDown(value = "clear.xml", type = TRUNCATE_TABLE)

<dataset>
    <accounts/>
</dataset>

@ExpectedDatabase

<dataset>
    <accounts id="1" />
</dataset>

When I run first time everything is ok. But on second time I have an error

junit.framework.ComparisonFailure: value (table=accounts, row=0, col=id) expected:<[1]> but was:<[2]>

raderio commented 7 years ago

I can reset it by running TRUNCATE TABLE accounts RESTART IDENTITY;

How to make dbunit to do that?

lvv83 commented 6 years ago

Hi.

On my case dbunit inserts left a table with sequence in wrong state, because dbunit doesn't call Postgre nextval() function. And after this we can't use any tools or libs such as PgAdmin or Hibernate with identity generation.

Solution based on idea from this post. We need to call setval() function after dbunit inserts.

Steps:

Example gist I think you can easily adapt it for your scenario.