feat: add database dropdown to dashboard import (#10118)

* feat: add database dropdown to dashboard import

Currently, when importing a database from a JSON file, the process
looks at the database name from the source (the info is in the file)
and matches the datasources to that name. If no database by that name
exists, it simply fails.

With this PR, we add a database dropdown that allows the user to specify
which databases the datasources should target as the get upserted.

I want to stress that the code in this area is not in a great shape,
and that the challenge of serializing/deser the nested objects is
challenging, but that there should be a much better way to do this.
One of the improvement (out of scope for this PR) that would allow to
simplify those import/export would be to use UUIDs for
importable/exportable objects.

Another identified issue is the indirections between
`utils/import_expor_{model}.py` on top of `{Model}.import_object`. Not
addressing that here.

Next topic is the MVC stuff. Decided to stick with it for now as this is
more of a [obious missing feat:] than a rewrite.

* isort \!? 0%^$%Y$&?%$^?%0^?

* fix tests

* pre-committing to py3.6

* address dpgaspar's comments

* revert isort
This commit is contained in:
Maxime Beauchemin
2020-07-05 15:08:37 -07:00
committed by GitHub
parent 33584a8095
commit 2314aad450
10 changed files with 130 additions and 52 deletions

View File

@@ -21,9 +21,11 @@ from datetime import datetime
from io import BytesIO
from typing import Any, Dict, Optional
from flask_babel import lazy_gettext as _
from sqlalchemy.orm import Session
from superset.connectors.sqla.models import SqlaTable, SqlMetric, TableColumn
from superset.exceptions import DashboardImportException
from superset.models.dashboard import Dashboard
from superset.models.slice import Slice
@@ -69,14 +71,19 @@ def decode_dashboards( # pylint: disable=too-many-return-statements
def import_dashboards(
session: Session, data_stream: BytesIO, import_time: Optional[int] = None
session: Session,
data_stream: BytesIO,
database_id: Optional[int] = None,
import_time: Optional[int] = None,
) -> None:
"""Imports dashboards from a stream to databases"""
current_tt = int(time.time())
import_time = current_tt if import_time is None else import_time
data = json.loads(data_stream.read(), object_hook=decode_dashboards)
if not data:
raise DashboardImportException(_("No data in file"))
for table in data["datasources"]:
type(table).import_obj(table, import_time=import_time)
type(table).import_obj(table, database_id, import_time=import_time)
session.commit()
for dashboard in data["dashboards"]:
Dashboard.import_obj(dashboard, import_time=import_time)

View File

@@ -24,12 +24,13 @@ from sqlalchemy.orm.session import make_transient
logger = logging.getLogger(__name__)
def import_datasource(
def import_datasource( # pylint: disable=too-many-arguments
session: Session,
i_datasource: Model,
lookup_database: Callable[[Model], Model],
lookup_datasource: Callable[[Model], Model],
import_time: Optional[int] = None,
database_id: Optional[int] = None,
) -> int:
"""Imports the datasource from the object to the database.
@@ -41,7 +42,9 @@ def import_datasource(
logger.info("Started import of the datasource: %s", i_datasource.to_json())
i_datasource.id = None
i_datasource.database_id = lookup_database(i_datasource).id
i_datasource.database_id = (
database_id if database_id else lookup_database(i_datasource).id
)
i_datasource.alter_params(import_time=import_time)
# override the datasource

View File

@@ -24,6 +24,7 @@ from datetime import datetime
from typing import Any, Callable, cast, Optional, Type
from flask import current_app, g, request
from sqlalchemy.exc import SQLAlchemyError
from superset.stats_logger import BaseStatsLogger
@@ -169,6 +170,10 @@ class DBEventLogger(AbstractEventLogger):
)
logs.append(log)
sesh = current_app.appbuilder.get_session
sesh.bulk_save_objects(logs)
sesh.commit()
try:
sesh = current_app.appbuilder.get_session
sesh.bulk_save_objects(logs)
sesh.commit()
except SQLAlchemyError as ex:
logging.error("DBEventLogger failed to log event(s)")
logging.exception(ex)