Skip to content

Commit 19a8492

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: - user_tasks_usertaskstatus.uuid - user_tasks_usertaskartifact.uuid See: https://docs.djangoproject.com/en/5.2/releases/5.0/#migrating-uuidfield
1 parent 67ba572 commit 19a8492

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

user_tasks/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from django.dispatch import Signal
66

7-
__version__ = '3.4.3'
7+
__version__ = '3.4.4'
88

99

1010
# This signal is emitted when a user task reaches any final state:
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
cursor.execute(
35+
"ALTER TABLE user_tasks_usertaskstatus "
36+
"MODIFY uuid uuid NOT NULL"
37+
)
38+
cursor.execute(
39+
"ALTER TABLE user_tasks_usertaskartifact "
40+
"MODIFY uuid uuid NOT NULL"
41+
)
42+
43+
44+
def reverse_mariadb_migration(apps, schema_editor):
45+
"""Reverse the migration only for MariaDB databases."""
46+
connection = schema_editor.connection
47+
48+
# Check if this is a MariaDB database
49+
if connection.vendor != 'mysql':
50+
return
51+
52+
# Additional check for MariaDB specifically (vs MySQL)
53+
with connection.cursor() as cursor:
54+
cursor.execute("SELECT VERSION()")
55+
version = cursor.fetchone()[0]
56+
if 'mariadb' not in version.lower():
57+
return
58+
59+
# Reverse the field changes for MariaDB
60+
with connection.cursor() as cursor:
61+
cursor.execute(
62+
"ALTER TABLE user_tasks_usertaskstatus "
63+
"MODIFY uuid char(32) NOT NULL"
64+
)
65+
cursor.execute(
66+
"ALTER TABLE user_tasks_usertaskartifact "
67+
"MODIFY uuid char(32) NOT NULL"
68+
)
69+
70+
71+
class Migration(migrations.Migration):
72+
73+
dependencies = [
74+
('user_tasks', '0004_url_textfield'),
75+
]
76+
77+
operations = [
78+
migrations.RunPython(
79+
code=apply_mariadb_migration,
80+
reverse_code=reverse_mariadb_migration,
81+
),
82+
]

0 commit comments

Comments
 (0)