Skip to content
Snippets Groups Projects
Commit 9930c23a authored by Radko Krkoš's avatar Radko Krkoš
Browse files

Fix: Add migration to use correct encoding from environment

* The issue was fixed in code in revisions 4c89be6b and a82dc686. This
  commit uses the same approach, but fixes the existing malformed records
  in the users table in an alembic migration during upgrade.
* First, an approach based on plpython3u extension and a user-defined
  function was considered, which had the downside of added dependency.
  This one is implemented in Python - data is read from the database,
  the demangling is performed and the correct new data is UPDATEd into
  the database.

(Redmine issue: #7553)
parent 9505cc2a
No related branches found
No related tags found
No related merge requests found
"""Demangle national characters in auth attributes
Revision ID: 1c380ebe027e
Revises: 432c8bc8e49b
Create Date: 2022-07-25 12:38:15.843315
"""
from alembic import op
# revision identifiers, used by Alembic.
revision = '1c380ebe027e'
down_revision = '432c8bc8e49b'
branch_labels = None
depends_on = None
def demangle(value):
"""National character demangling helper"""
try:
return value.encode("iso-8859-1").decode()
except (UnicodeDecodeError, UnicodeEncodeError):
return value
def upgrade():
conn = op.get_bind()
rows = conn.execute('SELECT id, fullname, organization FROM users').fetchall()
for rid, fnm_old, org_old in rows:
fnm = demangle(fnm_old)
org = demangle(org_old)
if fnm != fnm_old or org != org_old:
conn.execute("UPDATE users SET fullname = %s, organization = %s WHERE id = %s", (fnm, org, rid))
def downgrade():
pass
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment