Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update migration logic in #27119 #28422

Merged
merged 1 commit into from
May 13, 2024
Merged
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
33 changes: 26 additions & 7 deletions superset/migrations/shared/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,40 @@
DEFAULT_BATCH_SIZE = int(os.environ.get("BATCH_SIZE", 1000))


def table_has_column(table: str, column: str) -> bool:
def get_table_column(
table_name: str,
column_name: str,
) -> Optional[list[dict[str, Any]]]:
"""
Checks if a column exists in a given table.
Get the specified column.
:param table: A table name
:param column: A column name
:returns: True iff the column exists in the table
:param table_name: The Table name
:param column_name: The column name
:returns: The column
"""

insp = inspect(op.get_context().bind)

try:
return any(col["name"] == column for col in insp.get_columns(table))
for column in insp.get_columns(table_name):
if column["name"] == column_name:
return column
except NoSuchTableError:
return False
pass

return None


def table_has_column(table_name: str, column_name: str) -> bool:
"""
Checks if a column exists in a given table.
:param table_name: A table name
:param column_name: A column name
:returns: True iff the column exists in the table
"""

return bool(get_table_column(table_name, column_name))


def table_has_index(table: str, index: str) -> bool:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@

import sqlalchemy as sa # noqa: E402
from alembic import op # noqa: E402
from sqlalchemy.dialects.mysql import MEDIUMTEXT, TEXT # noqa: E402
from sqlalchemy.dialects.mysql.base import MySQLDialect # noqa: E402

from superset.migrations.shared.utils import get_table_column # noqa: E402
from superset.utils.core import MediumText # noqa: E402

TABLE_COLUMNS = [
Expand All @@ -38,8 +40,6 @@
"dashboards.css",
"keyvalue.value",
"query.extra_json",
"query.executed_sql",
"query.select_sql",
"report_execution_log.value_row_json",
"report_recipient.recipient_config_json",
"report_schedule.sql",
Expand All @@ -65,23 +65,35 @@

def upgrade():
if isinstance(op.get_bind().dialect, MySQLDialect):
for column in TABLE_COLUMNS:
with op.batch_alter_table(column.split(".")[0]) as batch_op:
batch_op.alter_column(
column.split(".")[1],
existing_type=sa.Text(),
type_=MediumText(),
existing_nullable=column not in NOT_NULL_COLUMNS,
)
for item in TABLE_COLUMNS:
table_name, column_name = item.split(".")

if (column := get_table_column(table_name, column_name)) and isinstance(
column["type"],
TEXT,
):
with op.batch_alter_table(table_name) as batch_op:
batch_op.alter_column(
column_name,
existing_type=sa.Text(),
type_=MediumText(),
existing_nullable=item not in NOT_NULL_COLUMNS,
)


def downgrade():
if isinstance(op.get_bind().dialect, MySQLDialect):
for column in TABLE_COLUMNS:
with op.batch_alter_table(column.split(".")[0]) as batch_op:
batch_op.alter_column(
column.split(".")[1],
existing_type=MediumText(),
type_=sa.Text(),
existing_nullable=column not in NOT_NULL_COLUMNS,
)
for item in TABLE_COLUMNS:
table_name, column_name = item.split(".")

if (column := get_table_column(table_name, column_name)) and isinstance(
column["type"],
MEDIUMTEXT,
):
with op.batch_alter_table(table_name) as batch_op:
batch_op.alter_column(
column_name,
existing_type=MediumText(),
type_=sa.Text(),
existing_nullable=item not in NOT_NULL_COLUMNS,
)
14 changes: 10 additions & 4 deletions superset/models/sql_lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@
)
from superset.sql_parse import CtasMethod, extract_tables_from_jinja_sql, Table
from superset.sqllab.limiting_factor import LimitingFactor
from superset.utils.core import get_column_name, MediumText, QueryStatus, user_label
from superset.utils.core import (
get_column_name,
LongText,
MediumText,
QueryStatus,
user_label,
)

if TYPE_CHECKING:
from superset.connectors.sqla.models import TableColumn
Expand Down Expand Up @@ -110,11 +116,11 @@ class Query(
sql_editor_id = Column(String(256), index=True)
schema = Column(String(256))
catalog = Column(String(256), nullable=True, default=None)
sql = Column(MediumText())
sql = Column(LongText())
Copy link
Member

Choose a reason for hiding this comment

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

@john-bodley Just to confirm.. is sql LongText or MediumText? The reason I'm asking is because it's not being affected by your script.

Copy link
Member Author

Choose a reason for hiding this comment

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

Per here sql is LongText and isn't impacted by any of the aforementioned migrations.

# Query to retrieve the results,
# used only in case of select_as_cta_used is true.
select_sql = Column(MediumText())
executed_sql = Column(MediumText())
select_sql = Column(LongText())
executed_sql = Column(LongText())
# Could be configured in the superset config.
limit = Column(Integer)
limiting_factor = Column(
Expand Down
6 changes: 5 additions & 1 deletion superset/utils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
from pandas.api.types import infer_dtype
from pandas.core.dtypes.common import is_numeric_dtype
from sqlalchemy import event, exc, inspect, select, Text
from sqlalchemy.dialects.mysql import MEDIUMTEXT
from sqlalchemy.dialects.mysql import LONGTEXT, MEDIUMTEXT
from sqlalchemy.engine import Connection, Engine
from sqlalchemy.engine.reflection import Inspector
from sqlalchemy.sql.type_api import Variant
Expand Down Expand Up @@ -1497,6 +1497,10 @@ def MediumText() -> Variant: # pylint:disable=invalid-name
return Text().with_variant(MEDIUMTEXT(), "mysql")


def LongText() -> Variant: # pylint:disable=invalid-name
return Text().with_variant(LONGTEXT(), "mysql")


def shortid() -> str:
return f"{uuid.uuid4()}"[-12:]

Expand Down
Loading