fix: text => JSON migration util (#33516)

This commit is contained in:
Beto Dealmeida
2025-05-22 08:41:38 -04:00
committed by GitHub
parent b7d3ff1e85
commit 875f538d54

View File

@@ -506,9 +506,21 @@ def cast_text_column_to_json(
conn.execute(
text(
f"""
ALTER TABLE {table}
ALTER COLUMN {column} TYPE jsonb
USING {column}::jsonb
CREATE OR REPLACE FUNCTION safe_to_jsonb(input text)
RETURNS jsonb
LANGUAGE plpgsql
IMMUTABLE
AS $$
BEGIN
RETURN input::jsonb;
EXCEPTION WHEN invalid_text_representation THEN
RETURN NULL;
END;
$$;
ALTER TABLE {table}
ALTER COLUMN {column} TYPE jsonb
USING safe_to_jsonb({column});
"""
)
)
@@ -525,6 +537,13 @@ def cast_text_column_to_json(
stmt_select = select(t.c[pk], t.c[column]).where(t.c[column].is_not(None))
for row_pk, value in conn.execute(stmt_select):
try:
json.loads(value)
except json.JSONDecodeError:
logger.warning(
f"Invalid JSON value in column {column} for {pk}={row_pk}: {value}"
)
continue
stmt_update = update(t).where(t.c[pk] == row_pk).values({tmp_column: value})
conn.execute(stmt_update)