Skip to content

[15.0][FIX]database_cleanup: endless loop - #2682

Closed
Camille0907 wants to merge 1 commit into
OCA:15.0from
Camille0907:15.0-fix_database_cleanup
Closed

[15.0][FIX]database_cleanup: endless loop#2682
Camille0907 wants to merge 1 commit into
OCA:15.0from
Camille0907:15.0-fix_database_cleanup

Conversation

@Camille0907

@Camille0907 Camille0907 commented Jul 20, 2023

Copy link
Copy Markdown

We have some cases where database_cleanup loop endlessly because of referential integrity.

In my case, the issue was caused by tables having foreign keys on tables which are not in the list of tables to delete. Updating the code to drop all constraints, even on tables which are not meant to be dropped, seems to solve the issue


[OUTDATED]
Here for example, DROP TABLE is failing because some tables have foreign keys on a studio table, and the error message suggest using DROP TABLE ... CASCADE, which seems indeed to fix the problem.

        Start purging tables attempt n° 158
        Try to purge table: transport_vehicle
2023-07-20 07:50:32,502 347 ERROR test_error odoo.sql_db: bad query: DROP TABLE "transport_vehicle"
ERROR: cannot drop table transport_vehicle because other objects depend on it
DETAIL:  constraint x_journaux_x_studio_field_PE7Np_fkey on table x_journaux depends on table transport_vehicle
HINT:  Use DROP ... CASCADE to drop the dependent objects too.
 
        Try to purge table: transport_vehicle_type
2023-07-20 07:50:32,513 347 ERROR test_error odoo.sql_db: bad query: DROP TABLE "transport_vehicle_type"
ERROR: cannot drop table transport_vehicle_type because other objects depend on it
DETAIL:  constraint transport_vehicle_vehicle_type_id_fkey on table transport_vehicle depends on table transport_vehicle_type
constraint x_journaux_x_studio_field_mHvR3_fkey on table x_journaux depends on table transport_vehicle_type
HINT:  Use DROP ... CASCADE to drop the dependent objects too.

First introduced by damdam-s#1 in #2219

But lost when this PR has been superseded by #2547

@Camille0907
Camille0907 force-pushed the 15.0-fix_database_cleanup branch from 9ea0b3e to 76b5ecb Compare July 20, 2023 08:06
Comment thread database_cleanup/models/purge_tables.py Outdated
self.logger.info("Dropping table %s", line.name)
self.env.cr.execute("DROP TABLE %s", (IdentifierAdapter(line.name),))
self.env.cr.execute(
"DROP TABLE %s CASCADE", (IdentifierAdapter(line.name),)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks dangerous and I would never knowingly run this on a production database. Do you think it would work to drop all constraints for all tables in the line loop in a first pass and then drop the tables in a second pass?

@Camille0907 Camille0907 Jul 27, 2023

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right :) I tried your proposition and it works, at least in my case. Updating the PR to this less dangerous solution ;) Thank you !

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What used to be the case is that constraints between tables that were going to be dropped were removed explicitely. That can be considered safe, because the related tables were going to be dropped anyway. What happens now in the suggested change is that constraints from a table that is not going to be dropped to a table that is going to be dropped are being dropped explicitely.
I would not consider this safe, because the existence of such a constraint is an indication that it is not safe to drop the table in the first place. It also breaks dropping tables that still (only) depend on each other.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, what would the safe solution be? Provide a bugfix just for the endless loop, but fail to drop the table?

@StefanRijnhart StefanRijnhart Feb 14, 2024

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering that

  • The current code in this PR is unsafe,
  • My original suggestion is very much what is already implemented in the released version of the module,
  • The issue occurs when dealing with a manually created x_ model according to the traceback,

I would make an exception only when dealing with a manually created model (c.q. table) that starts with x_

I had a case where database_cleanup looped endlessly because of
referential integrity. DROP TABLE was failing because some tables had
foreign keys on tables which were not in the list of tables to delete.
Dropping all contraints linked to the table, even if they are on table which won't be dropped seems to solve the issue.
@Camille0907
Camille0907 force-pushed the 15.0-fix_database_cleanup branch from 76b5ecb to c8d1695 Compare July 27, 2023 11:49
@github-actions

github-actions Bot commented Feb 8, 2026

Copy link
Copy Markdown

There hasn't been any activity on this pull request in the past 4 months, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 30 days.
If you want this PR to never become stale, please ask a PSC member to apply the "no stale" label.

@github-actions github-actions Bot added the stale PR/Issue without recent activity, it'll be soon closed automatically. label Feb 8, 2026
@github-actions github-actions Bot closed this Mar 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale PR/Issue without recent activity, it'll be soon closed automatically.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants