|
| 1 | +""" |
| 2 | +Rename Collection.key -> Collection.collection_code and change from key_field to code_field. |
| 3 | +
|
| 4 | +Squashed from 0007–0010. |
| 5 | +""" |
| 6 | +import re |
| 7 | + |
| 8 | +import django.core.validators |
| 9 | +import django.db.models.lookups |
| 10 | +from django.conf import settings |
| 11 | +from django.db import migrations, models |
| 12 | + |
| 13 | +import openedx_django_lib.fields |
| 14 | + |
| 15 | + |
| 16 | +class Migration(migrations.Migration): |
| 17 | + |
| 18 | + dependencies = [ |
| 19 | + ('openedx_content', '0006_typed_ids'), |
| 20 | + migrations.swappable_dependency(settings.AUTH_USER_MODEL), |
| 21 | + ] |
| 22 | + |
| 23 | + operations = [ |
| 24 | + # Drop old constraint (references the old field name). |
| 25 | + migrations.RemoveConstraint( |
| 26 | + model_name='collection', |
| 27 | + name='oel_coll_uniq_lp_key', |
| 28 | + ), |
| 29 | + # Rename the column. |
| 30 | + migrations.RenameField( |
| 31 | + model_name='collection', |
| 32 | + old_name='key', |
| 33 | + new_name='collection_code', |
| 34 | + ), |
| 35 | + # Change from key_field (max_length=500, no validator) to code_field |
| 36 | + # (max_length=255, with regex validator). |
| 37 | + migrations.AlterField( |
| 38 | + model_name='collection', |
| 39 | + name='collection_code', |
| 40 | + field=openedx_django_lib.fields.MultiCollationCharField( |
| 41 | + db_collations={'mysql': 'utf8mb4_bin', 'sqlite': 'BINARY'}, |
| 42 | + max_length=255, |
| 43 | + validators=[ |
| 44 | + django.core.validators.RegexValidator( |
| 45 | + re.compile('^[a-zA-Z0-9\\-\\_\\.]+\\Z'), |
| 46 | + 'Enter a valid "code name" consisting of letters, numbers, underscores, hyphens, or periods.', |
| 47 | + 'invalid', |
| 48 | + ), |
| 49 | + ], |
| 50 | + ), |
| 51 | + ), |
| 52 | + # Re-add uniqueness constraint with the new field name. |
| 53 | + migrations.AddConstraint( |
| 54 | + model_name='collection', |
| 55 | + constraint=models.UniqueConstraint( |
| 56 | + fields=('learning_package', 'collection_code'), |
| 57 | + name='oel_coll_uniq_lp_key', |
| 58 | + ), |
| 59 | + ), |
| 60 | + # DB-level regex check constraint. |
| 61 | + migrations.AddConstraint( |
| 62 | + model_name='collection', |
| 63 | + constraint=models.CheckConstraint( |
| 64 | + condition=django.db.models.lookups.Regex( |
| 65 | + models.F('collection_code'), |
| 66 | + '^[a-zA-Z0-9\\-\\_\\.]+\\Z', |
| 67 | + ), |
| 68 | + name='oel_coll_collection_code_regex', |
| 69 | + violation_error_message=( |
| 70 | + 'Enter a valid "code name" consisting of letters, numbers,' |
| 71 | + ' underscores, hyphens, or periods.' |
| 72 | + ), |
| 73 | + ), |
| 74 | + ), |
| 75 | + ] |
0 commit comments