Fix PostgreSQL testkit "relation already exists" failures caused by hardcoded public schema#210
Fix PostgreSQL testkit "relation already exists" failures caused by hardcoded public schema#210Copilot wants to merge 4 commits into
public schema#210Conversation
…sts failures Replace hardcoded "public" schema in localTables and localSequences with conn.getSchema so test cleanup works in the actual schema used by the test connection, not just the public schema. Agent-Logs-Url: https://github.com/slick/slick-testkit-example/sessions/3df518b0-3096-4636-a594-ae4203fc8b82 Co-authored-by: nafg <98384+nafg@users.noreply.github.com>
public schema
Agent-Logs-Url: https://github.com/slick/slick-testkit-example/sessions/4f000db6-e9d3-4b00-926d-f8f5804a66d9 Co-authored-by: nafg <98384+nafg@users.noreply.github.com>
|
@copilot explain how this works. When is schema not "public"? |
In those cases the hardcoded |
@copilot sure but where in this codebase does that happen and why? |
With the current configuration ( The change is defensive: |
…ema-agnostic fix Agent-Logs-Url: https://github.com/slick/slick-testkit-example/sessions/36fdd8de-70ae-497a-acbb-2c75eabb5ac1 Co-authored-by: nafg <98384+nafg@users.noreply.github.com>
Good call — switched to Regarding #120: that's a Renovate dependency-upgrade PR (pgjdbc 42.7.4 → 42.7.11), not a separate bug report. I haven't run the full test suite against 42.7.11 in this environment since pgjdbc 42.7.4 is the version wired in |
Replace the hardcoded
"public"schema filter inlocalTablesandlocalSequenceswithnullfor both catalog and schema parameters ingetTables. Passingnullmeans "no filter", so tables and sequences are found regardless of which schema they land in. System catalog objects are still excluded by the existing type-column filter ("TABLE"/"SEQUENCE"), so only user-created objects are matched and cleaned up between test runs.Also removes the pointless
Option(...).orNullwrapper that was present in the original code.Original prompt
The CI job failed in repository
slick/slick-testkit-examplefor pull request #120. Please create a pull request that fixes the failing PostgreSQL testkit job.Observed failure details from the job logs:
ERROR: relation "a" already exists,relation "users" already exists,relation "t" already exists, etc.ForeignKeyTest.testMultiColumn[mypostgres],JoinTest.testJoin[mypostgres],JdbcMapperTest.testMappedEntity[mypostgres],PrimaryKeyTest.test[mypostgres],TransactionTest.testTransactions[mypostgres], and others.Could not parse default value Some('2023-12-17'::date) for column slick_test.Data_Date.DATA.73485470109in workflow.github/workflows/ci.ymlon run25080894428.Relevant repository files:
.github/workflows/ci.ymlrunsdocker compose up -dand thensbt '++ ${{ matrix.scala }}' test.build.sbtalready hasTest / parallelExecution := false, so this does not appear to be a test parallelism problem.src/test/scala/slick/examples/testkit/MyPostgresTest.scaladefines a customExternalJdbcTestDB("mypostgres").localTablesandlocalSequencesboth call_.conn.getMetaData.getTables("", "public", null, null)and then filter by type.Likely root cause:
The custom test DB metadata lookup is hard-coded to the
publicschema, which likely prevents Slick testkit from discovering and cleaning up tables/sequences created in the actual schema used by the tests. As a result, objects are left behind and later tests fail withrelation already existserrors.Please:
src/test/scala/slick/examples/testkit/MyPostgresTest.scala.public.Suggested direction:
conn.getSchema) or a broader lookup that does not hard-codepublic.relation already existsfailures in CI.The following is the prior conversation context from the user's chat exploration (may be truncated):
User: Please find a solution for this failing job. Use the logs, job definition, and any referenced files where the failure occurred. Keep your response focused on the solution and include code suggestions when appropriate.
Assistant: The fix is to make the Postgres testkit isolate test objects by schema instead of hard-coding
public.What is failing
Most failures are collisions like:
ERROR: relation "a" already existsERROR: relation "users" already existsERROR: relation "t1" already existsThose are strong signs that the testkit cleanup/introspection is looking in the wrong schema, so tables created by one test are not being tracked and dropped correctly before the next test runs.
There is also one metadata failure:
Could not parse default value Some('2023-12-17'::date) ...That is likely a secondary effect of schema/model introspection mismatch, because this custom test DB currently assumes everything lives in
public.Root cause
In
src/test/scala/slick/examples/testkit/MyPostgresTest.scala, both overrides query only thepublicschema:getTables("", "public", null, null)at lines 29 and 37Source: https://github.com/slick/slick-testkit-example/blob/bb244cd0feaa0746a32d8187dc87bdffb4526dfc/src/test/scala/slick/examples/testkit/MyPostgresTest.scala
That is too restrictive for Slick testkit. Testkit commonly uses its own schema/database namespace (for this run the logs mention
slick_test), and if object discovery only scanspublic, cleanup misses created tables/sequences. Then later tests try to recreate them and fail with “already exists”.Solution
Stop restricting metadata discovery to
public. Either:The simplest fix is to pass
nullfor the schema pattern.Suggested code change
Replace the two
getTables("", "public", null, null)calls withgetTables("", null, null, null).