Files
FabledSteward/plugins/prometheus/migrations/versions/prometheus_005_own_tokens.py
T
bvandeusenandClaude Opus 5.5 ee9913b6d0
CI / lint (push) Successful in 2s
CI / unit (push) Successful in 1m38s
CI / integration (push) Successful in 1m57s
CI / publish (push) Successful in 1m15s
fix(prometheus): the migration removing sign-in targets runs on a new database, where the metric tables may not exist yet (#6747)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 14:24:00 -05:00

33 lines
1.3 KiB
Python

"""Every target holds its own token: one that took it from a sign-in goes
Revision ID: prometheus_005_own_tokens
Revises: prometheus_004_token_from
Create Date: 2026-11-01
"""
from typing import Sequence, Union
import sqlalchemy as sa
from alembic import op
revision: str = "prometheus_005_own_tokens"
down_revision: Union[str, None] = "prometheus_004_token_from"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# Left in place, such a target would ask with no token, and some services ban
# an address for that. Its newest values go with it; history ages out. On a new
# database the metric tables may not exist yet, and there is nothing to remove.
if sa.inspect(op.get_bind()).has_table("plugin_metric_latest"):
op.execute("""
DELETE FROM plugin_metric_latest l USING prometheus_targets t
WHERE t.token_from IS NOT NULL AND l.source_module = 'prometheus'
AND (l.resource_name = t.name OR left(l.resource_name, length(t.name) + 1) = t.name || ':')""")
op.execute("DELETE FROM prometheus_targets WHERE token_from IS NOT NULL")
op.drop_column("prometheus_targets", "token_from")
def downgrade() -> None:
op.add_column("prometheus_targets", sa.Column("token_from", sa.String(50), nullable=True))