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,33 @@ 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
# the assumption here is that the query was saved as a dataset
# but the type wasn't written properly to the slice
# by updating the type here we expect it will either work
# or it will 404 when the dataset is looked up.
params_dict["datasource"] = f"{ds_id}__table"
slc.params = json.dumps(params_dict)
eschutho marked this conversation as resolved.
Show resolved Hide resolved
logger.warning(
"updated slice datasource from %s__%s to %s__table for slice: %s",
ds_id,
ds_type,
ds_id,
slc.id,
)
except Exception:
# skip any malformatted params
logger.warning(
"failed to update slice.id = %s w/ datasource = %s__%s to %s__table",
slc.id,
ds_id,
ds_type,
ds_id,
)
pass


Expand All @@ -63,9 +86,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