test
This commit is contained in:
parent
9d23a03bc2
commit
78ec41ccb0
@ -1,11 +1,25 @@
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def fix_user_foreign_key(apps, schema_editor):
|
||||
def debug_tables(apps, schema_editor):
|
||||
connection = schema_editor.connection
|
||||
|
||||
with connection.cursor() as cursor:
|
||||
# Récupère toutes les contraintes de time_tracking_timeentry
|
||||
|
||||
cursor.execute("SHOW CREATE TABLE `users_user`")
|
||||
users_table = cursor.fetchone()
|
||||
|
||||
cursor.execute("SHOW CREATE TABLE `time_tracking_timeentry`")
|
||||
timeentry_table = cursor.fetchone()
|
||||
|
||||
print("\n\n========== USERS_USER ==========")
|
||||
print(users_table[1])
|
||||
|
||||
print("\n\n========== TIME_TRACKING_TIMEENTRY ==========")
|
||||
print(timeentry_table[1])
|
||||
|
||||
print("\n\n========== FK INFO ==========")
|
||||
|
||||
cursor.execute("""
|
||||
SELECT
|
||||
CONSTRAINT_NAME,
|
||||
@ -18,75 +32,30 @@ def fix_user_foreign_key(apps, schema_editor):
|
||||
AND COLUMN_NAME = 'user_id'
|
||||
""")
|
||||
|
||||
constraints = cursor.fetchall()
|
||||
for row in cursor.fetchall():
|
||||
print(row)
|
||||
|
||||
print("DEBUG FK FOUND:", constraints)
|
||||
print("\n\n========== COLUMNS ==========")
|
||||
|
||||
old_constraint = None
|
||||
|
||||
for (
|
||||
constraint_name,
|
||||
column_name,
|
||||
referenced_table,
|
||||
referenced_column,
|
||||
) in constraints:
|
||||
if referenced_table:
|
||||
old_constraint = constraint_name
|
||||
|
||||
print(
|
||||
"DEBUG USER FK:",
|
||||
constraint_name,
|
||||
"->",
|
||||
referenced_table,
|
||||
referenced_column,
|
||||
)
|
||||
|
||||
break
|
||||
|
||||
if old_constraint:
|
||||
print(
|
||||
f"Suppression de l'ancienne FK : {old_constraint}"
|
||||
)
|
||||
|
||||
cursor.execute(
|
||||
f"""
|
||||
ALTER TABLE `time_tracking_timeentry`
|
||||
DROP FOREIGN KEY `{old_constraint}`
|
||||
"""
|
||||
)
|
||||
|
||||
# Vérifie si la bonne FK existe déjà
|
||||
cursor.execute("""
|
||||
SELECT
|
||||
CONSTRAINT_NAME
|
||||
FROM information_schema.KEY_COLUMN_USAGE
|
||||
TABLE_NAME,
|
||||
COLUMN_NAME,
|
||||
COLUMN_TYPE,
|
||||
IS_NULLABLE,
|
||||
COLUMN_KEY,
|
||||
COLLATION_NAME
|
||||
FROM information_schema.COLUMNS
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = 'time_tracking_timeentry'
|
||||
AND COLUMN_NAME = 'user_id'
|
||||
AND REFERENCED_TABLE_NAME = 'users_user'
|
||||
AND REFERENCED_COLUMN_NAME = 'id'
|
||||
AND (
|
||||
(TABLE_NAME = 'users_user' AND COLUMN_NAME = 'id')
|
||||
OR
|
||||
(TABLE_NAME = 'time_tracking_timeentry' AND COLUMN_NAME = 'user_id')
|
||||
)
|
||||
""")
|
||||
|
||||
correct_constraint = cursor.fetchone()
|
||||
|
||||
if correct_constraint:
|
||||
print(
|
||||
"La FK vers users_user existe déjà :",
|
||||
correct_constraint[0],
|
||||
)
|
||||
return
|
||||
|
||||
print("Création de la FK vers users_user")
|
||||
|
||||
cursor.execute("""
|
||||
ALTER TABLE `time_tracking_timeentry`
|
||||
ADD CONSTRAINT `time_tracking_timeentry_user_id_users_user_fk`
|
||||
FOREIGN KEY (`user_id`)
|
||||
REFERENCES `users_user` (`id`)
|
||||
ON DELETE CASCADE
|
||||
""")
|
||||
|
||||
print("FK corrigée avec succès.")
|
||||
for row in cursor.fetchall():
|
||||
print(row)
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
@ -97,7 +66,7 @@ class Migration(migrations.Migration):
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(
|
||||
fix_user_foreign_key,
|
||||
debug_tables,
|
||||
migrations.RunPython.noop,
|
||||
),
|
||||
]
|
||||
Loading…
x
Reference in New Issue
Block a user