Skip to content

Convert metricunit DB enum to varchar to eliminate migration overhead #1876

@shangyian

Description

@shangyian

Problem

The unit column on metricmetadata is stored as a PostgreSQL native ENUM type (metricunit). This means that every time a new unit is added to MetricUnit in Python, it requires a database migration just to register the new value with the DB, even though the application logic doesn't otherwise change.

This has already happened multiple times (eg most recently in #1869). The downgrade path is also problematic: Postgres doesn't support removing enum values directly, so downgrade has to drop and recreate the entire enum type.

Proposed Solution

Change the unit column from Enum(MetricUnit) to VARCHAR. The python-side MetricUnit enum stays as-is for validation, but we stop using Postgres's native enum type to store it:

  unit: Optional[MetricUnit] = Field(
      sa_column=SqlaColumn(String),
      default=MetricUnit.UNKNOWN,
  )

We can do a one-time db migration to convert the column type:

ALTER TABLE metricmetadata ALTER COLUMN unit TYPE VARCHAR USING unit::text

After this change, adding new metric units requires zero DB migrations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions