database-rider: Foreign key constraint violation during `clearDatabase`
Short problem description
Unable to clear database completely because of a foreign key constraints violation.
Software versions
- postgres: 11.5
- database-rider: rider-spring:1.10.0 with underlying rider-core:1.10.0
Steps to reproduce
⚠️ To reproduce this problem you should have Docker installed on your environment.
- Clone the repo https://github.com/iakunin/codexia-bot:
git clone git@github.com:iakunin/codexia-bot.git && cd codexia-bot
- Checkout to specific branch:
git checkout rider-fk-violation
- Run the test with problem
bash bin/gradle_in_docker.sh clean test --info --tests *SendReviewsIntegrationTest.reviewSentWith500
- Grep the postgres container logs
cat var/pg-container.log | grep -v "SELECT NULL AS TABLE_CAT" | grep -v "FROM pg_catalog.pg_type" | grep -v "pg_catalog.pg_namespace" | tail -n16
And you will see the following output:
2020-04-17 11:26:45.671 UTC [69] LOG: execute <unnamed>: DELETE FROM public.codexia_project where 1=1
2020-04-17 11:26:45.671 UTC [69] ERROR: update or delete on table "codexia_project" violates foreign key constraint "codexia_review_codexia_project_id_fkey" on table "codexia_review"
2020-04-17 11:26:45.671 UTC [69] DETAIL: Key (id)=(5) is still referenced from table "codexia_review".
2020-04-17 11:26:45.671 UTC [69] STATEMENT: DELETE FROM public.codexia_project where 1=1
2020-04-17 11:26:45.672 UTC [69] LOG: execute <unnamed>: DELETE FROM public.codexia_review where 1=1
2020-04-17 11:26:45.672 UTC [69] ERROR: update or delete on table "codexia_review" violates foreign key constraint "codexia_review_notification_codexia_review_id_fkey" on table "codexia_review_notification"
2020-04-17 11:26:45.672 UTC [69] DETAIL: Key (id)=(1) is still referenced from table "codexia_review_notification".
2020-04-17 11:26:45.672 UTC [69] STATEMENT: DELETE FROM public.codexia_review where 1=1
2020-04-17 11:26:45.673 UTC [69] LOG: execute <unnamed>: DELETE FROM public.codexia_review_notification where 1=1
2020-04-17 11:26:45.675 UTC [69] LOG: execute <unnamed>: DELETE FROM public.databasechangelog where 1=1
2020-04-17 11:26:45.676 UTC [69] LOG: execute <unnamed>: DELETE FROM public.databasechangeloglock where 1=1
2020-04-17 11:26:45.676 UTC [69] LOG: execute <unnamed>: DELETE FROM public.github_repo where 1=1
2020-04-17 11:26:45.677 UTC [69] LOG: execute <unnamed>: DELETE FROM public.github_repo_source where 1=1
2020-04-17 11:26:45.678 UTC [69] LOG: execute <unnamed>: DELETE FROM public.github_repo_stat where 1=1
2020-04-17 11:26:45.679 UTC [69] LOG: execute <unnamed>: DELETE FROM public.hackernews_item where 1=1
2020-04-17 11:26:45.680 UTC [69] LOG: execute <unnamed>: DELETE FROM public.stars_up_result where 1=1
Pay attention to this: ERROR: update or delete on table "codexia_project" violates foreign key constraint "codexia_review_codexia_project_id_fkey" on table "codexia_review".
I think, that is pretty clear sign of a problem.
As a result, the table codexia_project is not cleared as expected.
It is worth mentioning that corresponding exception is not rethrown, but just logged and swallowed (link to source).
Suggested solution
Decorate the com.github.database.rider.core.dataset.DataSetExecutorImpl#clearDatabase method with disableConstraints() call before and with enableConstraints() call after.
Something like this:
/**
* @throws SQLException if clean up cannot be performed
*/
@Override
public void clearDatabase(DataSetConfig config) throws SQLException {
disableConstraints();
// the original code left intact
enableConstraints();
}
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 1
- Comments: 29 (28 by maintainers)
Commits related to this issue
- #202 - Getting rid of foreign key constraint violation during `clearDatabase` — committed to database-rider/database-rider by iakunin 4 years ago
- #202 - Making disableConstraints() idempotent — committed to database-rider/database-rider by iakunin 4 years ago
- #202 - Enabling constraints only if `disableConstraints == false` — committed to database-rider/database-rider by iakunin 4 years ago
- #202 - Getting rid of foreign key constraint violation during `clearDatabase` — committed to beskow/database-rider by iakunin 4 years ago
- #202 - Making disableConstraints() idempotent — committed to beskow/database-rider by iakunin 4 years ago
- #202 - Enabling constraints only if `disableConstraints == false` — committed to beskow/database-rider by iakunin 4 years ago
- refs #202 Use different approach to disable and enable postgres constraints (per session) — committed to database-rider/database-rider by rmpestano 4 years ago
- refs #202 Use different approach to disable and enable postgres constraints (per session) — committed to database-rider/database-rider by rmpestano 4 years ago
I wasn’t aware that the JUnit 5 module is not hooking up to the Spring transaction context. We are pretty much using
@DataJpaTesteverywhere instead of@SpringBootTest. This definitely happens because of the transactions. I’ll continue the discussion in the other issue.This issue was fixed by #203 and will be available in
v1.13.0Maybe, the problem is that not all DBs are supported in disabled constraints, also there is one DB (don’t remember which one) that you can configure it in a way that you cannot disable constraints at all.
So I think we should keep the table ordering
Hmmm but for the test you’ll want to have it enabled, of course.
Can you send a PR for that?