mirror of
https://github.com/complexcaresolutions/dak.c2s.git
synced 2026-03-17 17:13:42 +00:00
feat: add migration to anonymize existing fall_ids
Replaces Nachname-based fall_ids with KVNR or random 6-char suffix for all existing cases in the database. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
04a2e8fe93
commit
7bbe501bfa
1 changed files with 42 additions and 0 deletions
42
backend/alembic/versions/006_anonymize_fall_ids.py
Normal file
42
backend/alembic/versions/006_anonymize_fall_ids.py
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
"""Anonymize fall_ids: replace Nachname with KVNR or random suffix.
|
||||||
|
|
||||||
|
Revision ID: 006_anonymize_fall_ids
|
||||||
|
Revises: 005_add_disclosure_requests
|
||||||
|
"""
|
||||||
|
|
||||||
|
import random
|
||||||
|
import string
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
revision = "006_anonymize_fall_ids"
|
||||||
|
down_revision = "005_add_disclosure_requests"
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def _random_suffix(length=6):
|
||||||
|
charset = string.ascii_uppercase + string.digits
|
||||||
|
return "".join(random.choices(charset, k=length))
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
conn = op.get_bind()
|
||||||
|
cases = conn.execute(
|
||||||
|
sa.text("SELECT id, fall_id, kvnr, jahr, kw, fallgruppe FROM cases")
|
||||||
|
).fetchall()
|
||||||
|
|
||||||
|
for case in cases:
|
||||||
|
case_id, old_fall_id, kvnr, jahr, kw, fallgruppe = case
|
||||||
|
suffix = kvnr if kvnr else _random_suffix()
|
||||||
|
new_fall_id = f"{jahr}-{kw:02d}-{fallgruppe}-{suffix}"
|
||||||
|
conn.execute(
|
||||||
|
sa.text("UPDATE cases SET fall_id = :new_id WHERE id = :case_id"),
|
||||||
|
{"new_id": new_fall_id, "case_id": case_id},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# Cannot restore original Nachname-based fall_ids — data is lost
|
||||||
|
pass
|
||||||
Loading…
Reference in a new issue