Skip to content

Commit d9cc3b6

Browse files
blarghmateyormsbee
authored andcommitted
fix: Convert UUIDField columns to uuid type for MariaDB
Converts UUIDField columns from char(32) to uuid type for MariaDB Django 5 compatibility. This migration converts the following columns: - submissions_submission.uuid - submissions_studentitem.uuid - submissions_score.uuid See: https://docs.djangoproject.com/en/5.2/releases/5.0/#migrating-uuidfield
1 parent 81cc930 commit d9cc3b6

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed

submissions/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
""" API for creating submissions and scores. """
2-
__version__ = '3.12.0'
2+
__version__ = '3.12.1'
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Generated migration for MariaDB UUID field conversion (Django 5.2)
2+
"""
3+
Migration to convert UUIDField from char(32) to uuid type for MariaDB compatibility.
4+
5+
This migration is necessary because Django 5 changed the behavior of UUIDField for MariaDB
6+
databases from using CharField(32) to using a proper UUID type. This change isn't managed
7+
automatically, so we need to generate migrations to safely convert the columns.
8+
9+
This migration only executes for MariaDB databases and is a no-op for other backends.
10+
11+
See: https://www.albertyw.com/note/django-5-mariadb-uuidfield
12+
"""
13+
14+
from django.db import migrations
15+
16+
17+
def apply_mariadb_migration(apps, schema_editor):
18+
"""Apply the migration only for MariaDB databases."""
19+
connection = schema_editor.connection
20+
21+
# Check if this is a MariaDB database
22+
if connection.vendor != 'mysql':
23+
return
24+
25+
# Additional check for MariaDB specifically (vs MySQL)
26+
with connection.cursor() as cursor:
27+
cursor.execute("SELECT VERSION()")
28+
version = cursor.fetchone()[0]
29+
if 'mariadb' not in version.lower():
30+
return
31+
32+
# Apply the field changes for MariaDB
33+
with connection.cursor() as cursor:
34+
# Submission model
35+
cursor.execute(
36+
"ALTER TABLE submissions_submission "
37+
"MODIFY uuid uuid NOT NULL"
38+
)
39+
# StudentItem model
40+
cursor.execute(
41+
"ALTER TABLE submissions_studentitem "
42+
"MODIFY uuid uuid NOT NULL"
43+
)
44+
# Score model
45+
cursor.execute(
46+
"ALTER TABLE submissions_score "
47+
"MODIFY uuid uuid NOT NULL"
48+
)
49+
50+
51+
def reverse_mariadb_migration(apps, schema_editor):
52+
"""Reverse the migration only for MariaDB databases."""
53+
connection = schema_editor.connection
54+
55+
# Check if this is a MariaDB database
56+
if connection.vendor != 'mysql':
57+
return
58+
59+
# Additional check for MariaDB specifically (vs MySQL)
60+
with connection.cursor() as cursor:
61+
cursor.execute("SELECT VERSION()")
62+
version = cursor.fetchone()[0]
63+
if 'mariadb' not in version.lower():
64+
return
65+
66+
# Reverse the field changes for MariaDB
67+
with connection.cursor() as cursor:
68+
cursor.execute(
69+
"ALTER TABLE submissions_submission "
70+
"MODIFY uuid char(32) NOT NULL"
71+
)
72+
cursor.execute(
73+
"ALTER TABLE submissions_studentitem "
74+
"MODIFY uuid char(32) NOT NULL"
75+
)
76+
cursor.execute(
77+
"ALTER TABLE submissions_score "
78+
"MODIFY uuid char(32) NOT NULL"
79+
)
80+
81+
82+
class Migration(migrations.Migration):
83+
84+
dependencies = [
85+
('submissions', '0005_CreateTeamModel'),
86+
]
87+
88+
operations = [
89+
migrations.RunPython(
90+
code=apply_mariadb_migration,
91+
reverse_code=reverse_mariadb_migration,
92+
),
93+
]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Generated by Django (merge migration)
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('submissions', '0005_submissionfile'),
10+
('submissions', '0006_mariadb_uuid_conversion'),
11+
]
12+
13+
operations = [
14+
]

0 commit comments

Comments
 (0)