|
| 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