Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
"""Change enum columns to string type
Revision ID: cb7368a4bfae
Revises: 51547dcccb10
Create Date: 2025-05-14 15:45:43.734154+00:00
"""
# pylint: disable=no-member, invalid-name, missing-function-docstring, unused-import, no-name-in-module

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "cb7368a4bfae"
down_revision = "51547dcccb10"
branch_labels = None
depends_on = None


def upgrade():
with op.batch_alter_table("history", schema=None) as batch_op:
batch_op.alter_column(
"entity_type",
existing_type=postgresql.ENUM(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also drop the enum types from Postgres? Maybe with something like:

batch_op.execute("DROP TYPE IF EXISTS entitytype")
batch_op.execute("DROP TYPE IF EXISTS activitytype")

"ATTRIBUTE",
"AVAILABILITY",
"BACKFILL",
"CATALOG",
"COLUMN_ATTRIBUTE",
"DEPENDENCY",
"ENGINE",
"LINK",
"MATERIALIZATION",
"NAMESPACE",
"NODE",
"PARTITION",
"QUERY",
"TAG",
name="entitytype",
),
type_=sa.String(length=20),
existing_nullable=True,
)
batch_op.alter_column(
"activity_type",
existing_type=postgresql.ENUM(
"CREATE",
"DELETE",
"RESTORE",
"UPDATE",
"REFRESH",
"TAG",
"SET_ATTRIBUTE",
"STATUS_CHANGE",
name="activitytype",
),
type_=sa.String(length=20),
existing_nullable=True,
)


def downgrade():
with op.batch_alter_table("history", schema=None) as batch_op:
batch_op.alter_column(
"activity_type",
existing_type=sa.String(length=20),
type_=postgresql.ENUM(
"CREATE",
"DELETE",
"RESTORE",
"UPDATE",
"REFRESH",
"TAG",
"SET_ATTRIBUTE",
"STATUS_CHANGE",
name="activitytype",
),
existing_nullable=True,
)
batch_op.alter_column(
"entity_type",
existing_type=sa.String(length=20),
type_=postgresql.ENUM(
"ATTRIBUTE",
"AVAILABILITY",
"BACKFILL",
"CATALOG",
"COLUMN_ATTRIBUTE",
"DEPENDENCY",
"ENGINE",
"LINK",
"MATERIALIZATION",
"NAMESPACE",
"NODE",
"PARTITION",
"QUERY",
"TAG",
name="entitytype",
),
existing_nullable=True,
)
10 changes: 4 additions & 6 deletions datajunction-server/datajunction_server/database/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
JSON,
BigInteger,
DateTime,
Enum,
Index,
Integer,
String,
)
from sqlalchemy.orm import Mapped, mapped_column

from datajunction_server.database.base import Base
from datajunction_server.internal.history import ActivityType, EntityType
from datajunction_server.typing import UTCDatetime


Expand All @@ -35,14 +33,14 @@ class History(Base):
BigInteger().with_variant(Integer, "sqlite"),
primary_key=True,
)
entity_type: Mapped[Optional[EntityType]] = mapped_column(
Enum(EntityType),
entity_type: Mapped[Optional[str]] = mapped_column(
String(20),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why only 20?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason, I just thought that the postgres default of infinite length seemed unnecessary for what will just be a string representation of an enum value. Should I make this larger? Or maybe don't specify the length at all?

default=None,
)
entity_name: Mapped[Optional[str]] = mapped_column(String, default=None)
node: Mapped[Optional[str]] = mapped_column(String, default=None)
activity_type: Mapped[Optional[ActivityType]] = mapped_column(
Enum(ActivityType),
activity_type: Mapped[Optional[str]] = mapped_column(
String(20),
default=None,
)
user: Mapped[Optional[str]] = mapped_column(String, default=None)
Expand Down