Skip to content

Use RESTRICT instead of CASCADE when truncating with postgres#58

Merged
botandrose merged 2 commits into
DatabaseCleaner:mainfrom
bsuchodolski:do-not-use-cascade-when-truncating-tables-with-postgres
May 8, 2021
Merged

Use RESTRICT instead of CASCADE when truncating with postgres#58
botandrose merged 2 commits into
DatabaseCleaner:mainfrom
bsuchodolski:do-not-use-cascade-when-truncating-tables-with-postgres

Conversation

@bsuchodolski

Copy link
Copy Markdown

So the problem was described in this issue #57

In case of two tables, from which one was referencing another, when only referenced table was set to be truncated (e.g. with only option) then referencing table was silently truncated as well because of how postgres's TRUNCATE works with CASCADE option:

Automatically truncate all tables that have foreign-key references to any of the named tables, or to any tables added to the group due to CASCADE.

In case of applications with complex database schema it was easy to stumble upon a problem when such referencing table was truncated despite it was not named to truncate and it was really hard to tell why at the first glance.

With RESTRICT option truncation fails with an exception with clear message about what is the problem and which tables are referencing each another.

@codecov

codecov Bot commented Mar 19, 2021

Copy link
Copy Markdown

Codecov Report

Merging #58 (288ea61) into main (cd18900) will not change coverage.
The diff coverage is 100.00%.

❗ Current head 288ea61 differs from pull request most recent head 4918122. Consider uploading reports for the commit 4918122 to get more accurate results
Impacted file tree graph

@@            Coverage Diff            @@
##              main       #58   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           12        12           
  Lines          535       549   +14     
=========================================
+ Hits           535       549   +14     
Impacted Files Coverage Δ
lib/database_cleaner/active_record/truncation.rb 100.00% <100.00%> (ø)
.../database_cleaner/active_record/truncation_spec.rb 100.00% <100.00%> (ø)
spec/support/database_helper.rb 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cd18900...4918122. Read the comment docs.

@bsuchodolski

Copy link
Copy Markdown
Author

Hey @botandrose the only thing I cannot figure out is how and to what number bump the version of the gem?

@botandrose

Copy link
Copy Markdown
Contributor

@bsuchodolski Thanks for putting this together! Don't worry about bumping the version, I'll do that when I release v2.0.1

@bsuchodolski bsuchodolski deleted the do-not-use-cascade-when-truncating-tables-with-postgres branch May 26, 2021 12:27
@GoktugOzturk

GoktugOzturk commented Jun 7, 2021

Copy link
Copy Markdown

You should use RESTRICT if table names are defined. Otherwise, CASCADE is the only option to truncate multiple tables with default behavior.

Our tests broke because of this change.

  PG::FeatureNotSupported: ERROR:  cannot truncate a table referenced in a foreign key constraint
  DETAIL:  Table "teams" references "events".
  HINT:  Truncate table "teams" at the same time, or use TRUNCATE ... CASCADE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants