fuck
This commit is contained in:
parent
3f8ead410d
commit
5d8e3923ce
@ -1,6 +1,79 @@
|
|||||||
from django.db import migrations
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
def fix_user_foreign_key(apps, schema_editor):
|
||||||
|
with schema_editor.connection.cursor() as cursor:
|
||||||
|
# Récupère le vrai nom de la FK actuellement présente
|
||||||
|
cursor.execute("""
|
||||||
|
SELECT CONSTRAINT_NAME
|
||||||
|
FROM information_schema.KEY_COLUMN_USAGE
|
||||||
|
WHERE TABLE_SCHEMA = DATABASE()
|
||||||
|
AND TABLE_NAME = 'time_tracking_timeentry'
|
||||||
|
AND COLUMN_NAME = 'user_id'
|
||||||
|
AND REFERENCED_TABLE_NAME = 'auth_user'
|
||||||
|
AND REFERENCED_COLUMN_NAME = 'id'
|
||||||
|
""")
|
||||||
|
|
||||||
|
row = cursor.fetchone()
|
||||||
|
|
||||||
|
if not row:
|
||||||
|
# La FK vers auth_user n'existe déjà plus.
|
||||||
|
# On vérifie simplement si la bonne FK existe.
|
||||||
|
cursor.execute("""
|
||||||
|
SELECT CONSTRAINT_NAME
|
||||||
|
FROM information_schema.KEY_COLUMN_USAGE
|
||||||
|
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'
|
||||||
|
""")
|
||||||
|
|
||||||
|
if cursor.fetchone():
|
||||||
|
return
|
||||||
|
|
||||||
|
raise RuntimeError(
|
||||||
|
"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("""
|
||||||
|
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
|
||||||
|
""")
|
||||||
|
|
||||||
|
|
||||||
|
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):
|
||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
@ -8,32 +81,8 @@ class Migration(migrations.Migration):
|
|||||||
]
|
]
|
||||||
|
|
||||||
operations = [
|
operations = [
|
||||||
migrations.RunSQL(
|
migrations.RunPython(
|
||||||
sql=[
|
fix_user_foreign_key,
|
||||||
"""
|
reverse_user_foreign_key,
|
||||||
ALTER TABLE time_tracking_timeentry
|
|
||||||
DROP FOREIGN KEY time_tracking_timeentry_user_id_59aa2b1f_fk_auth_user_id;
|
|
||||||
""",
|
|
||||||
"""
|
|
||||||
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;
|
|
||||||
""",
|
|
||||||
],
|
|
||||||
reverse_sql=[
|
|
||||||
"""
|
|
||||||
ALTER TABLE time_tracking_timeentry
|
|
||||||
DROP FOREIGN KEY time_tracking_timeentry_user_id_users_user_fk;
|
|
||||||
""",
|
|
||||||
"""
|
|
||||||
ALTER TABLE time_tracking_timeentry
|
|
||||||
ADD CONSTRAINT time_tracking_timeentry_user_id_59aa2b1f_fk_auth_user_id
|
|
||||||
FOREIGN KEY (user_id)
|
|
||||||
REFERENCES auth_user (id)
|
|
||||||
ON DELETE CASCADE;
|
|
||||||
""",
|
|
||||||
],
|
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
Loading…
x
Reference in New Issue
Block a user