Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uuid_generate_v4 generates duplicate UUIDs during tests #28015

Closed
2 tasks done
amatosg opened this issue Nov 27, 2024 · 2 comments
Closed
2 tasks done

uuid_generate_v4 generates duplicate UUIDs during tests #28015

amatosg opened this issue Nov 27, 2024 · 2 comments
Milestone

Comments

@amatosg
Copy link
Contributor

amatosg commented Nov 27, 2024

Overview of the issue

I have this changeSet:

<changeSet id="add-uuid-extension" author="alejandro">
  <preConditions onFail="MARK_RAN">
    <not>
      <sqlCheck expectedResult="1">
        SELECT COUNT(*) FROM pg_extension WHERE extname = 'uuid-ossp';
      </sqlCheck>
    </not>
  </preConditions>
  <sql>
      CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
  </sql>
</changeSet>
<changeSet id="add-uuid-generate-v4-function" author="alejandro">
    <preConditions onFail="MARK_RAN">
        <not>
            <sqlCheck expectedResult="0">
                SELECT COUNT(*) FROM pg_proc WHERE proname = 'uuid_generate_v4';
            </sqlCheck>
        </not>
    </preConditions>
    <sql>
        CREATE OR REPLACE FUNCTION uuid_generate_v4() RETURNS uuid
        LANGUAGE c IMMUTABLE STRICT
        AS '$libdir/uuid-ossp', 'uuid_generate_v4';
    </sql>
</changeSet>

and I created this test:

@Test
@Transactional
void testUniqueUUIDGeneration() {
    Set<String> uuids = new HashSet<>();
    for (int i = 0; i < 1000; i++) {
        String uuid = em.createNativeQuery("SELECT uuid_generate_v4()")
            .getSingleResult().toString();
        System.out.println(uuid);
        assertTrue(uuids.add(uuid), "Duplicate UUID found after " + i + " tries: " + uuid);
    }
}

The test fails after generating 5 (always 5) UUIDs. This could be tested with the following test:

@ParameterizedTest(name = "Generating {0} UUIDs")
@ValueSource(ints = {1, 2, 3, 4, 5, 6})
@Transactional
void testUniqueUUIDGeneration(Integer input) {
    Set<String> uuids = new HashSet<>();
    for (int i = 0; i < input; i++) {
        em.clear();
        String uuid = em
            .createNativeQuery("SELECT uuid_generate_v4()")
            .getSingleResult()
            .toString(); // Llama a uuid_generate_v4() desde PostgreSQL
        System.out.println(uuid);
        assertTrue(uuids.add(uuid), "Duplicate UUID found after " + i + " tries: " + uuid);
    }
}

And failing after generating 5 UUIDs:
image

Motivation for or Use Case

This is a bug because UUIDs should be unique.

Reproduce the error

create the changeLog and run the test. The result will be something like this:

91774e9e-716a-4ab2-beea-9d64b9bd767a
7483e971-304f-4928-a7af-1c9db81faa07
1512207c-3486-4a9a-8620-f7f944315b80
1438e32f-a038-4900-910d-18d3ee04c60d
323c5670-0efa-48c4-9394-9f332b0b8ffc
323c5670-0efa-48c4-9394-9f332b0b8ffc

org.opentest4j.AssertionFailedError: Duplicate UUID found after 5 tries: 323c5670-0efa-48c4-9394-9f332b0b8ffc
Related issues

None found

Suggest a Fix
JHipster Version(s)

v8.7.3

Browsers and Operating System

EndevourOS

  • Tickets opened without reproduction steps or that doesn't follows the template recommendation will be closed.
  • Checking this box is mandatory (this is just to show you read everything)
@mshima
Copy link
Member

mshima commented Nov 29, 2024

That is not generated by JHipster.

@mshima mshima closed this as completed Nov 29, 2024
@amatosg
Copy link
Contributor Author

amatosg commented Dec 4, 2024

@mshima you're right. It wasn't generated by jhipster, sorry about that.

I'm leaving a comment just in case anyone encounters the same issue I had:

When the extension "uuid-ossp" is created using liquibase, it will generated duplicated UUIDs even if you run queries directly in the database.

PostgreSQL has its own method to generate UUIDs so there is no need to install said extension. Replacing any calls from uuid_generate_v4() with gen_random_uuid() will solve the problem.

@mraible mraible added this to the 8.8.0 milestone Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants