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(migration): add log for values unseen in Slice.datasource_type #23925

Merged
merged 14 commits into from
May 4, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
down_revision = "07f9a902af1b"

import json
import logging

import sqlalchemy as sa
from alembic import op
Expand All @@ -36,6 +37,8 @@

Base = declarative_base()

logger = logging.getLogger(__name__)


class Slice(Base): # type: ignore
__tablename__ = "slices"
Expand All @@ -48,13 +51,16 @@ class Slice(Base): # type: ignore
def upgrade_slc(slc: Slice) -> None:
# clean up all charts with datasource_type not != table
slc.datasource_type = "table"
ds_id = None
ds_type = None
try:
params_dict = json.loads(slc.params)
ds_id, ds_type = params_dict["datasource"].split("__")
hughhhh marked this conversation as resolved.
Show resolved Hide resolved
params_dict["datasource"] = f"{ds_id}__table"
slc.params = json.dumps(params_dict)
eschutho marked this conversation as resolved.
Show resolved Hide resolved
except Exception:
# skip any malformatted params
logger.warning("failed to update slice: %s__%s", ds_id, ds_type)
pass


Expand All @@ -63,9 +69,16 @@ def upgrade():
session = db.Session(bind=bind)

with op.batch_alter_table("slices") as batch_op:
for slc in session.query(Slice).filter(Slice.datasource_type == "query").all():
upgrade_slc(slc)
session.add(slc)
for slc in session.query(Slice).filter(Slice.datasource_type != "table").all():
if slc.datasource_type == "query":
upgrade_slc(slc)
session.add(slc)

else:
logger.warning(
"unknown value detected for slc.datasource_type: %s",
slc.datasource_type,
)

batch_op.create_check_constraint(
"ck_chart_datasource", "datasource_type in ('table')"
Expand Down