test encoooore
This commit is contained in:
parent
5d8e3923ce
commit
9d23a03bc2
@ -2,25 +2,63 @@ from django.db import migrations
|
|||||||
|
|
||||||
|
|
||||||
def fix_user_foreign_key(apps, schema_editor):
|
def fix_user_foreign_key(apps, schema_editor):
|
||||||
with schema_editor.connection.cursor() as cursor:
|
connection = schema_editor.connection
|
||||||
# Récupère le vrai nom de la FK actuellement présente
|
|
||||||
|
with connection.cursor() as cursor:
|
||||||
|
# Récupère toutes les contraintes de time_tracking_timeentry
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
SELECT CONSTRAINT_NAME
|
SELECT
|
||||||
|
CONSTRAINT_NAME,
|
||||||
|
COLUMN_NAME,
|
||||||
|
REFERENCED_TABLE_NAME,
|
||||||
|
REFERENCED_COLUMN_NAME
|
||||||
FROM information_schema.KEY_COLUMN_USAGE
|
FROM information_schema.KEY_COLUMN_USAGE
|
||||||
WHERE TABLE_SCHEMA = DATABASE()
|
WHERE TABLE_SCHEMA = DATABASE()
|
||||||
AND TABLE_NAME = 'time_tracking_timeentry'
|
AND TABLE_NAME = 'time_tracking_timeentry'
|
||||||
AND COLUMN_NAME = 'user_id'
|
AND COLUMN_NAME = 'user_id'
|
||||||
AND REFERENCED_TABLE_NAME = 'auth_user'
|
|
||||||
AND REFERENCED_COLUMN_NAME = 'id'
|
|
||||||
""")
|
""")
|
||||||
|
|
||||||
row = cursor.fetchone()
|
constraints = cursor.fetchall()
|
||||||
|
|
||||||
if not row:
|
print("DEBUG FK FOUND:", constraints)
|
||||||
# La FK vers auth_user n'existe déjà plus.
|
|
||||||
# On vérifie simplement si la bonne FK existe.
|
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("""
|
cursor.execute("""
|
||||||
SELECT CONSTRAINT_NAME
|
SELECT
|
||||||
|
CONSTRAINT_NAME
|
||||||
FROM information_schema.KEY_COLUMN_USAGE
|
FROM information_schema.KEY_COLUMN_USAGE
|
||||||
WHERE TABLE_SCHEMA = DATABASE()
|
WHERE TABLE_SCHEMA = DATABASE()
|
||||||
AND TABLE_NAME = 'time_tracking_timeentry'
|
AND TABLE_NAME = 'time_tracking_timeentry'
|
||||||
@ -29,49 +67,26 @@ def fix_user_foreign_key(apps, schema_editor):
|
|||||||
AND REFERENCED_COLUMN_NAME = 'id'
|
AND REFERENCED_COLUMN_NAME = 'id'
|
||||||
""")
|
""")
|
||||||
|
|
||||||
if cursor.fetchone():
|
correct_constraint = cursor.fetchone()
|
||||||
|
|
||||||
|
if correct_constraint:
|
||||||
|
print(
|
||||||
|
"La FK vers users_user existe déjà :",
|
||||||
|
correct_constraint[0],
|
||||||
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
raise RuntimeError(
|
print("Création de la FK vers users_user")
|
||||||
"Impossible de trouver la FK de time_tracking_timeentry.user_id"
|
|
||||||
)
|
|
||||||
|
|
||||||
old_constraint = row[0]
|
|
||||||
|
|
||||||
# Supprime la FK actuelle
|
|
||||||
cursor.execute(
|
|
||||||
f"""
|
|
||||||
ALTER TABLE time_tracking_timeentry
|
|
||||||
DROP FOREIGN KEY `{old_constraint}`
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
|
|
||||||
# Crée la FK correcte vers users_user
|
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
ALTER TABLE time_tracking_timeentry
|
ALTER TABLE `time_tracking_timeentry`
|
||||||
ADD CONSTRAINT time_tracking_timeentry_user_id_users_user_fk
|
ADD CONSTRAINT `time_tracking_timeentry_user_id_users_user_fk`
|
||||||
FOREIGN KEY (user_id)
|
FOREIGN KEY (`user_id`)
|
||||||
REFERENCES users_user (id)
|
REFERENCES `users_user` (`id`)
|
||||||
ON DELETE CASCADE
|
ON DELETE CASCADE
|
||||||
""")
|
""")
|
||||||
|
|
||||||
|
print("FK corrigée avec succès.")
|
||||||
def reverse_user_foreign_key(apps, schema_editor):
|
|
||||||
with schema_editor.connection.cursor() as cursor:
|
|
||||||
# Supprime la FK users_user
|
|
||||||
cursor.execute("""
|
|
||||||
ALTER TABLE time_tracking_timeentry
|
|
||||||
DROP FOREIGN KEY time_tracking_timeentry_user_id_users_user_fk
|
|
||||||
""")
|
|
||||||
|
|
||||||
# Recrée la FK vers auth_user
|
|
||||||
cursor.execute("""
|
|
||||||
ALTER TABLE time_tracking_timeentry
|
|
||||||
ADD CONSTRAINT time_tracking_timeentry_user_id_auth_user_fk
|
|
||||||
FOREIGN KEY (user_id)
|
|
||||||
REFERENCES auth_user (id)
|
|
||||||
ON DELETE CASCADE
|
|
||||||
""")
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
@ -83,6 +98,6 @@ class Migration(migrations.Migration):
|
|||||||
operations = [
|
operations = [
|
||||||
migrations.RunPython(
|
migrations.RunPython(
|
||||||
fix_user_foreign_key,
|
fix_user_foreign_key,
|
||||||
reverse_user_foreign_key,
|
migrations.RunPython.noop,
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
Loading…
x
Reference in New Issue
Block a user