Skip to content

Commit

Permalink
[ADD] validate_fk_constraints function
Browse files Browse the repository at this point in the history
This is useful in cases where you need to delete a lot of records,
and you speed the deletion up by disabling FK triggers,
but you want to validate integrity after deletion.
  • Loading branch information
thomaspaulb committed Sep 25, 2024
1 parent 6300043 commit eee2e40
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions openupgradelib/openupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ def do_raise(error):
"cow_templates_mark_if_equal_to_upstream",
"cow_templates_replicate_upstream",
"clean_transient_models",
"validate_fk_constraints",
]


Expand Down Expand Up @@ -3547,3 +3548,32 @@ def clean_transient_models(cr):
cr.execute(query)
except Exception as e:
logger.warning("Failed to clean transient table %s\n%s", table_name, str(e))


def validate_fk_constraints(cr, table, column):
"""
Validate all FK constraints to a column in table.
This is useful in cases where you need to delete a lot of records,
and you speed the deletion up by disabling FK triggers,
but you want to validate integrity after deletion.
Can only be run as superuser.
:param cr: Database cursor.
"""
cr.execute(
query,
{
"table": table,
"column": column,
},
)
for schema, table, constraint in cr.fetchall():
cr.execute(
"update pg_constraint set convalidated = false where conname = %s",
(constraint,),
)
cr.execute(
"alter table %s validate constraint %s", (AsIs(table), AsIs(constraint))
)

0 comments on commit eee2e40

Please sign in to comment.