codeant-ai-for-open-source[bot] commented on code in PR #45790: URL: https://github.com/apache/superset/pull/45790#discussion_r4396508506
########## superset/migrations/versions/2026-10-27_00-00_9670ab7f6bc6_normalize_metadata_cache_timeouts.py: ########## @@ -0,0 +1,89 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""Normalize legacy database metadata cache timeouts. + +Older database forms stored metadata cache timeouts as strings. Convert only +recognized non-negative integer strings, preserving malformed configurations +and unrelated fields instead of guessing at their intended values. + +Revision ID: 9670ab7f6bc6 +Revises: d623a0cb6bb0 +Create Date: 2026-10-27 00:00:00.000000 +""" + +# The shared JSON wrapper cannot decode decimals without losing stored precision. +import simplejson # noqa: TID251 +from alembic import op +from sqlalchemy import column, Integer, select, table, Text + +from superset.utils import json + +revision = "9670ab7f6bc6" +down_revision = "d623a0cb6bb0" + +_DATABASES = table("dbs", column("id", Integer), column("extra", Text)) +_TIMEOUT_KEYS = ( + "catalog_cache_timeout", + "schema_cache_timeout", + "table_cache_timeout", +) + + +def upgrade() -> None: + """Convert legacy non-negative integer strings to integer timeouts.""" + connection = op.get_bind() + for database_id, raw_extra in connection.execute( + select(_DATABASES.c.id, _DATABASES.c.extra) + ): + try: + extra = simplejson.loads(raw_extra, use_decimal=True) + except (TypeError, ValueError): + continue + if not isinstance(extra, dict): + continue + timeouts = extra.get("metadata_cache_timeout") + if not isinstance(timeouts, dict): + continue + + changed = False + for key in _TIMEOUT_KEYS: + value = timeouts.get(key) + if not isinstance(value, str): + continue + try: + timeout = int(value) + except ValueError: + continue + if timeout < 0: + continue + timeouts[key] = timeout + changed = True + + if changed: + connection.execute( + _DATABASES.update() + .where(_DATABASES.c.id == database_id) + .values(extra=json.dumps(extra)) Review Comment: ✅ **CodeAnt verified this suggestion was addressed in subsequent commits and marked this thread resolved** as of `ebeb64e`. The update now matches both the database ID and the originally selected `extra` value. If a concurrent edit changed `extra`, the update affects no row and the migration raises an error instead of overwriting the newer configuration. <sub>If that's not right, unresolve this thread and CodeAnt will leave it open.</sub> <!-- codeant-auto-resolve-reply --> -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
