You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2020/11/12 21:08:43 UTC

[GitHub] [incubator-superset] betodealmeida commented on a change in pull request #11670: feat: new import commands for dataset and databases

betodealmeida commented on a change in pull request #11670:
URL: https://github.com/apache/incubator-superset/pull/11670#discussion_r522427293



##########
File path: superset/datasets/commands/importers/v1.py
##########
@@ -0,0 +1,136 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from typing import Any, Dict, Set
+
+from marshmallow import fields, Schema
+from sqlalchemy.orm import Session
+
+from superset.commands.importers.v1 import ImportModelsCommand
+from superset.connectors.sqla.models import SqlaTable
+
+
+class ColumnSchema(Schema):
+    column_name = fields.String(required=True)
+    verbose_name = fields.String()
+    is_dttm = fields.Boolean()
+    is_active = fields.Boolean(allow_none=True)
+    type = fields.String(required=True)
+    groupby = fields.Boolean()
+    filterable = fields.Boolean()
+    expression = fields.String()
+    description = fields.String(allow_none=True)
+    python_date_format = fields.String(allow_none=True)
+
+
+class MetricSchema(Schema):
+    metric_name = fields.String(required=True)
+    verbose_name = fields.String()
+    metric_type = fields.String(allow_none=True)
+    expression = fields.String(required=True)
+    description = fields.String(allow_none=True)
+    d3format = fields.String(allow_none=True)
+    extra = fields.String(allow_none=True)
+    warning_text = fields.String(allow_none=True)
+
+
+class DatasetSchema(Schema):
+    table_name = fields.String(required=True)
+    main_dttm_col = fields.String(allow_none=True)
+    description = fields.String()
+    default_endpoint = fields.String()
+    offset = fields.Integer()
+    cache_timeout = fields.Integer()
+    schema = fields.String()
+    sql = fields.String()
+    params = fields.String(allow_none=True)
+    template_params = fields.String(allow_none=True)
+    filter_select_enabled = fields.Boolean()
+    fetch_values_predicate = fields.String(allow_none=True)
+    extra = fields.String(allow_none=True)
+    uuid = fields.UUID(required=True)
+    columns = fields.List(fields.Nested(ColumnSchema))
+    metrics = fields.List(fields.Nested(MetricSchema))
+    version = fields.String(required=True)
+    database_uuid = fields.UUID(required=True)
+
+
+class ImportDatasetsCommand(ImportModelsCommand):
+
+    model = SqlaTable
+    schema = DatasetSchema
+    prefix = "datasets/"
+
+    @staticmethod
+    def import_(
+        session: Session, config: Dict[str, Any], overwrite: bool = False,
+    ) -> SqlaTable:
+        existing = session.query(SqlaTable).filter_by(uuid=config["uuid"]).first()
+        if existing:

Review comment:
       It needs to be treated separately. If the imported table already exists there are 2 scenarios:
   
   - If `overwrite` is false them we shouldn't do anything, so `return`
   - if `overwrite` is true then we can continue, but first we need to set the current ID to replace insert of insert
   
   So if we could write it:
   
   ```python
   if existing and not overwrite:
       return
   
   if existing:
       config["id"] = existing.id
   ```
   
   Which is a bit more verbose.
   ```
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org