Skip to content

Commit

Permalink
trying to fix integrity errors before migrating tables
Browse files Browse the repository at this point in the history
  • Loading branch information
José Redrejo committed Jun 13, 2024
1 parent 2c5f722 commit c4c94d7
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion kolibri/utils/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.core.exceptions import ValidationError
from django.core.management import call_command
from django.core.management.base import handle_default_options
from django.db import connections
from django.db.utils import DatabaseError

import kolibri
Expand Down Expand Up @@ -185,6 +186,27 @@ def _copy_preseeded_db(db_name, target=None):
)


def sqlite_check_foreign_keys():
db_connection = connections["default"]
with db_connection.cursor() as cursor:
cursor.execute("PRAGMA foreign_key_check;")
result = cursor.fetchall()
if len(result) > 0:
logger.warning(
"Foreign key constraint failed. Trying to fix integrity errors..."
)
for row in result:
bad_table = row[0]
rowid = row[1]
# for security, only fix automatically error integrities in loggers
if bad_table[:6] == "logger":
cursor.execute(f"DELETE FROM {bad_table} WHERE rowid = {rowid};")
else:
logger.error(
f"Foreign key constraint failed in {bad_table} table, rowid {rowid}. Please fix it manually."
)


def _upgrades_before_django_setup(updated, version):
if version and updated:
check_plugin_config_file_location(version)
Expand All @@ -205,6 +227,7 @@ def _upgrades_before_django_setup(updated, version):
check_default_options_exist()

if OPTIONS["Database"]["DATABASE_ENGINE"] == "sqlite":
sqlite_check_foreign_keys()
# If we are using sqlite,
# we can shortcut migrations by using the preseeded databases
# that we bundle in the Kolibri whl file.
Expand Down Expand Up @@ -236,7 +259,7 @@ def _post_django_initialization():
process_cache.directory,
settings.CACHES["process_cache"]["SHARDS"],
settings.CACHES["process_cache"]["TIMEOUT"],
**settings.CACHES["process_cache"]["OPTIONS"]
**settings.CACHES["process_cache"]["OPTIONS"],
)


Expand Down

0 comments on commit c4c94d7

Please sign in to comment.