Skip to content

Commit

Permalink
fix: check for column before adding in migrations (#31185)
Browse files Browse the repository at this point in the history
  • Loading branch information
sadpandajoe authored Dec 2, 2024
1 parent deec63b commit 8020729
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
15 changes: 15 additions & 0 deletions superset/migrations/shared/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from typing import Any, Callable, Optional, Union
from uuid import uuid4

import sqlalchemy as sa
from alembic import op
from sqlalchemy import inspect
from sqlalchemy.dialects.mysql.base import MySQLDialect
Expand Down Expand Up @@ -182,3 +183,17 @@ def has_table(table_name: str) -> bool:
table_exists = insp.has_table(table_name)

return table_exists


def add_column_if_not_exists(table_name: str, column: sa.Column) -> None:
"""
Adds a column to a table if it does not already exist.
:param table_name: Name of the table.
:param column: SQLAlchemy Column object.
"""
if not table_has_column(table_name, column.name):
print(f"Adding column '{column.name}' to table '{table_name}'.\n")
op.add_column(table_name, column)
else:
print(f"Column '{column.name}' already exists in table '{table_name}'.\n")
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@
import sqlalchemy as sa
from alembic import op

from superset.migrations.shared.utils import add_column_if_not_exists

# revision identifiers, used by Alembic.
revision = "c22cb5c2e546"
down_revision = "678eefb4ab44"


def upgrade():
op.add_column(
"user_attribute", sa.Column("avatar_url", sa.String(length=100), nullable=True)
add_column_if_not_exists(
"user_attribute",
sa.Column("avatar_url", sa.String(length=100), nullable=True),
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import sqlalchemy as sa
from alembic import op

from superset.migrations.shared.utils import add_column_if_not_exists

# revision identifiers, used by Alembic.
revision = "5f57af97bc3f"
down_revision = "d60591c5515f"
Expand All @@ -34,7 +36,7 @@

def upgrade():
for table in tables:
op.add_column(
add_column_if_not_exists(
table,
sa.Column("catalog", sa.String(length=256), nullable=True),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,19 @@
downgrade_catalog_perms,
upgrade_catalog_perms,
)
from superset.migrations.shared.utils import add_column_if_not_exists

# revision identifiers, used by Alembic.
revision = "58d051681a3b"
down_revision = "4a33124c18ad"


def upgrade():
op.add_column(
add_column_if_not_exists(
"tables",
sa.Column("catalog_perm", sa.String(length=1000), nullable=True),
)
op.add_column(
add_column_if_not_exists(
"slices",
sa.Column("catalog_perm", sa.String(length=1000), nullable=True),
)
Expand Down

0 comments on commit 8020729

Please sign in to comment.