|
| 1 | +# Reference migration: matches the pattern that `mrt fix` generates for RemoveField. |
| 2 | +# Used by test_django_fixer_e2e.py to verify that the generated code pattern |
| 3 | +# actually executes correctly against a real database. |
| 4 | +from django.db import migrations |
| 5 | + |
| 6 | +_MRT_TABLE = "_mrt_backups" |
| 7 | +_MRT_CHUNK = 500 |
| 8 | + |
| 9 | + |
| 10 | +def __mrt_enc(v): |
| 11 | + import base64 |
| 12 | + from datetime import date, datetime, time |
| 13 | + from decimal import Decimal |
| 14 | + from uuid import UUID |
| 15 | + |
| 16 | + if v is None: |
| 17 | + return None |
| 18 | + if isinstance(v, bool): |
| 19 | + return v |
| 20 | + if isinstance(v, int): |
| 21 | + return v |
| 22 | + if isinstance(v, float): |
| 23 | + return v |
| 24 | + if isinstance(v, Decimal): |
| 25 | + return "D:" + str(v) |
| 26 | + if isinstance(v, datetime): |
| 27 | + if v.tzinfo is not None: |
| 28 | + return "DTs:" + str(v.timestamp()) |
| 29 | + return "DT:" + v.isoformat() |
| 30 | + if isinstance(v, date): |
| 31 | + return "d:" + v.isoformat() |
| 32 | + if isinstance(v, time): |
| 33 | + return "t:" + v.isoformat() |
| 34 | + if isinstance(v, UUID): |
| 35 | + return "U:" + str(v) |
| 36 | + if isinstance(v, (bytes, bytearray, memoryview)): |
| 37 | + return "B:" + base64.b64encode(bytes(v)).decode() |
| 38 | + s = str(v) |
| 39 | + for prefix in ("D:", "DT:", "DTs:", "d:", "t:", "U:", "B:", "S:"): |
| 40 | + if s.startswith(prefix): |
| 41 | + return "S:" + s |
| 42 | + return s |
| 43 | + |
| 44 | + |
| 45 | +def __mrt_dec(v): |
| 46 | + import base64 |
| 47 | + from datetime import date, datetime, time |
| 48 | + from decimal import Decimal |
| 49 | + from uuid import UUID |
| 50 | + |
| 51 | + if not isinstance(v, str): |
| 52 | + return v |
| 53 | + if v.startswith("D:"): |
| 54 | + return Decimal(v[2:]) |
| 55 | + if v.startswith("DTs:"): |
| 56 | + return datetime.fromtimestamp(float(v[4:])) |
| 57 | + if v.startswith("DT:"): |
| 58 | + return datetime.fromisoformat(v[3:]) |
| 59 | + if v.startswith("d:"): |
| 60 | + return date.fromisoformat(v[2:]) |
| 61 | + if v.startswith("t:"): |
| 62 | + return time.fromisoformat(v[2:]) |
| 63 | + if v.startswith("U:"): |
| 64 | + return UUID(v[2:]) |
| 65 | + if v.startswith("B:"): |
| 66 | + return base64.b64decode(v[2:]) |
| 67 | + if v.startswith("S:"): |
| 68 | + return v[2:] |
| 69 | + return v |
| 70 | + |
| 71 | + |
| 72 | +_MRT_LABEL_contact_phone = "0002_remove_phone__contact_phone" |
| 73 | + |
| 74 | + |
| 75 | +def _backup_contact_phone(apps, schema_editor): |
| 76 | + import json |
| 77 | + |
| 78 | + from django.db import connection |
| 79 | + |
| 80 | + Contact = apps.get_model("django_fixer_app", "Contact") |
| 81 | + with connection.cursor() as cur: |
| 82 | + cur.execute( |
| 83 | + "CREATE TABLE IF NOT EXISTS " + _MRT_TABLE + " " |
| 84 | + "(migration_label TEXT NOT NULL, object_id TEXT NOT NULL, payload TEXT)" |
| 85 | + ) |
| 86 | + cur.execute( |
| 87 | + "DELETE FROM " + _MRT_TABLE + " WHERE migration_label = %s", |
| 88 | + [_MRT_LABEL_contact_phone], |
| 89 | + ) |
| 90 | + last_pk = None |
| 91 | + while True: |
| 92 | + qs = Contact.objects.order_by("pk") |
| 93 | + if last_pk is not None: |
| 94 | + qs = qs.filter(pk__gt=last_pk) |
| 95 | + batch = list(qs.values_list("pk", "phone")[:_MRT_CHUNK]) |
| 96 | + if not batch: |
| 97 | + break |
| 98 | + with connection.cursor() as cur: |
| 99 | + for pk, val in batch: |
| 100 | + cur.execute( |
| 101 | + "INSERT INTO " + _MRT_TABLE + " VALUES (%s, %s, %s)", |
| 102 | + [ |
| 103 | + _MRT_LABEL_contact_phone, |
| 104 | + json.dumps(__mrt_enc(pk)), |
| 105 | + json.dumps(__mrt_enc(val)), |
| 106 | + ], |
| 107 | + ) |
| 108 | + last_pk = batch[-1][0] |
| 109 | + |
| 110 | + |
| 111 | +def _restore_contact_phone(apps, schema_editor): |
| 112 | + import json |
| 113 | + |
| 114 | + from django.db import connection |
| 115 | + |
| 116 | + Contact = apps.get_model("django_fixer_app", "Contact") |
| 117 | + with connection.cursor() as cur: |
| 118 | + cur.execute( |
| 119 | + "SELECT object_id, payload FROM " + _MRT_TABLE + " WHERE migration_label = %s", |
| 120 | + [_MRT_LABEL_contact_phone], |
| 121 | + ) |
| 122 | + rows = cur.fetchall() |
| 123 | + for pk_raw, val_raw in rows: |
| 124 | + pk = __mrt_dec(json.loads(pk_raw)) |
| 125 | + val = __mrt_dec(json.loads(val_raw)) |
| 126 | + Contact.objects.filter(pk=pk).update(phone=val) |
| 127 | + with connection.cursor() as cur: |
| 128 | + cur.execute( |
| 129 | + "DELETE FROM " + _MRT_TABLE + " WHERE migration_label = %s", |
| 130 | + [_MRT_LABEL_contact_phone], |
| 131 | + ) |
| 132 | + |
| 133 | + |
| 134 | +class Migration(migrations.Migration): |
| 135 | + dependencies = [("django_fixer_app", "0001_initial")] |
| 136 | + |
| 137 | + operations = [ |
| 138 | + migrations.RunPython(_backup_contact_phone, _restore_contact_phone), |
| 139 | + migrations.RemoveField(model_name="Contact", name="phone"), |
| 140 | + ] |
0 commit comments