Skip to content

Commit

Permalink
fix(migration): add log for values unseen in Slice.datasource_type (#…
Browse files Browse the repository at this point in the history
…23925)

Co-authored-by: Elizabeth Thompson <[email protected]>
  • Loading branch information
hughhhh and eschutho committed May 4, 2023
1 parent f1fa1a7 commit 3dc4de4
Showing 1 changed file with 33 additions and 3 deletions.
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("__")
# 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)
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

0 comments on commit 3dc4de4

Please sign in to comment.