Skip to content

Commit

Permalink
feat: Adds chart ID option to migrate-viz
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-s-molina committed Jun 25, 2024
1 parent fe3ba12 commit 2fe65a8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
47 changes: 27 additions & 20 deletions superset/cli/viz_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations

Check warning on line 17 in superset/cli/viz_migrations.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/viz_migrations.py#L17

Added line #L17 was not covered by tests

from enum import Enum

import click
from click_option_group import optgroup, RequiredMutuallyExclusiveOptionGroup
from flask.cli import with_appcontext

from superset import db
Expand Down Expand Up @@ -46,37 +47,43 @@ def migrate_viz() -> None:

@migrate_viz.command()
@with_appcontext
@optgroup.group(
"Grouped options",
cls=RequiredMutuallyExclusiveOptionGroup,
)
@optgroup.option(
@click.option(

Check warning on line 50 in superset/cli/viz_migrations.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/viz_migrations.py#L50

Added line #L50 was not covered by tests
"--viz_type",
"-t",
help=f"The viz type to migrate: {', '.join(list(VizType))}",
help=f"The viz type to upgrade: {', '.join(list(VizType))}",
required=True,
)
@click.option(

Check warning on line 56 in superset/cli/viz_migrations.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/viz_migrations.py#L56

Added line #L56 was not covered by tests
"--chart_id",
help="The chart ID to upgrade",
type=int,
)
def upgrade(viz_type: str) -> None:
def upgrade(viz_type: str, chart_id: int | None = None) -> None:

Check warning on line 61 in superset/cli/viz_migrations.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/viz_migrations.py#L61

Added line #L61 was not covered by tests
"""Upgrade a viz to the latest version."""
migrate(VizType(viz_type))
migrate(VizType(viz_type), chart_id)

Check warning on line 63 in superset/cli/viz_migrations.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/viz_migrations.py#L63

Added line #L63 was not covered by tests


@migrate_viz.command()
@with_appcontext
@optgroup.group(
"Grouped options",
cls=RequiredMutuallyExclusiveOptionGroup,
)
@optgroup.option(
@click.option(

Check warning on line 68 in superset/cli/viz_migrations.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/viz_migrations.py#L68

Added line #L68 was not covered by tests
"--viz_type",
"-t",
help=f"The viz type to migrate: {', '.join(list(VizType))}",
help=f"The viz type to downgrade: {', '.join(list(VizType))}",
required=True,
)
@click.option(

Check warning on line 74 in superset/cli/viz_migrations.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/viz_migrations.py#L74

Added line #L74 was not covered by tests
"--chart_id",
help="The chart ID to downgrade",
type=int,
)
def downgrade(viz_type: str) -> None:
def downgrade(viz_type: str, chart_id: int | None = None) -> None:

Check warning on line 79 in superset/cli/viz_migrations.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/viz_migrations.py#L79

Added line #L79 was not covered by tests
"""Downgrade a viz to the previous version."""
migrate(VizType(viz_type), is_downgrade=True)
migrate(VizType(viz_type), chart_id, is_downgrade=True)

Check warning on line 81 in superset/cli/viz_migrations.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/viz_migrations.py#L81

Added line #L81 was not covered by tests


def migrate(viz_type: VizType, is_downgrade: bool = False) -> None:
def migrate(

Check warning on line 84 in superset/cli/viz_migrations.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/viz_migrations.py#L84

Added line #L84 was not covered by tests
viz_type: VizType, chart_id: int | None = None, is_downgrade: bool = False
) -> None:
"""Migrate a viz from one type to another."""
# pylint: disable=import-outside-toplevel
from superset.migrations.shared.migrate_viz.processors import (
Expand Down Expand Up @@ -107,6 +114,6 @@ def migrate(viz_type: VizType, is_downgrade: bool = False) -> None:
VizType.TREEMAP: MigrateTreeMap,
}
if is_downgrade:
migrations[viz_type].downgrade(db.session)
migrations[viz_type].downgrade(db.session, chart_id)

Check warning on line 117 in superset/cli/viz_migrations.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/viz_migrations.py#L117

Added line #L117 was not covered by tests
else:
migrations[viz_type].upgrade(db.session)
migrations[viz_type].upgrade(db.session, chart_id)

Check warning on line 119 in superset/cli/viz_migrations.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/viz_migrations.py#L119

Added line #L119 was not covered by tests
12 changes: 9 additions & 3 deletions superset/migrations/shared/migrate_viz/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,26 @@ def downgrade_slice(cls, slc: Slice) -> None:
slc.query_context = json.dumps(query_context)

@classmethod
def upgrade(cls, session: Session) -> None:
slices = session.query(Slice).filter(Slice.viz_type == cls.source_viz_type)
def upgrade(cls, session: Session, chart_id: int | None = None) -> None:
slices = session.query(Slice).filter(

Check warning on line 157 in superset/migrations/shared/migrate_viz/base.py

View check run for this annotation

Codecov / codecov/patch

superset/migrations/shared/migrate_viz/base.py#L157

Added line #L157 was not covered by tests
and_(
Slice.viz_type == cls.source_viz_type,
Slice.id == chart_id if chart_id is not None else True,
)
)
for slc in paginated_update(
slices,
lambda current, total: print(f"Upgraded {current}/{total} charts"),
):
cls.upgrade_slice(slc)

@classmethod
def downgrade(cls, session: Session) -> None:
def downgrade(cls, session: Session, chart_id: int | None = None) -> None:
slices = session.query(Slice).filter(
and_(
Slice.viz_type == cls.target_viz_type,
Slice.params.like(f"%{FORM_DATA_BAK_FIELD_NAME}%"),
Slice.id == chart_id if chart_id is not None else True,
)
)
for slc in paginated_update(
Expand Down

0 comments on commit 2fe65a8

Please sign in to comment.