H2Plugin seems to initialize sequences with wrong sequence number (off by one) #621
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi everyone,
currently we use H2 in memory for testing scenarios in combination with DBUnit (while using PostgresQL in production). During this we encountered an issue with the way, that H2Plugin seems to initialize primary-key-providing sequences, when entities are already present in the database. Basically when no sequence exists, H2Plugin tries to create a new sequence based on the current PK values by using MAX(pk) as the initial value.
Unfortunately the initial value given to H2 is actually the next value returned by the sequence (i.e. the sequence is initialized with val-1), so the first primary key generation after the initialization will give an existing primary key value instead of a new one.
An example flow (to be tested on the H2 console) simulating this is:
This can be fixed simply by adding one to the initial H2 sequence value and thus complying with the H2 semantics, which is contained in this pull request. Most likely this will only affect testing and not live environments due to H2 deployments with existing records are seldomly encountered there.
Let me know, if this does not conform to any guidelines, code styles or if there are other issues with this patch :-) && Greetings
Dennis