Skip to content

Default CASCADE option when truncating with postgres is not intuitive #57

Description

@bsuchodolski

I've just spent an afternoon debugging why some of my tables are truncated despite being in except option and finally found out that in case of postgres they are just silently truncated because they reference other tables that should be legitimately truncated.

https://github.com/DatabaseCleaner/database_cleaner-active_record/blob/main/lib/database_cleaner/active_record/truncation.rb#L192

def truncate_tables(table_names)
  return if table_names.nil? || table_names.empty?
  execute("TRUNCATE TABLE #{table_names.map{|name| quote_table_name(name)}.join(', ')} RESTART IDENTITY CASCADE;")
end

I personally feel that this is not very intuitive because tables are truncated silently and it takes a while to get to the root cause of the problem if you have one, especially when you are working on complex application with several dozens of tables with complex structure and references.

I think that user should be able to decide how he wants to truncate the tables especially that there is a RESTRICT option when truncating with postgres and normally this is the default option. I would be super happy if I would be able to explicitly set that I want to use this option maybe with another option restrict: true so I know excatly when I come across this reference problem. If it's not a complex change I'll do it happily myself :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions