superset-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [incubator-superset] riahk commented on a change in pull request #10741: feat(database): POST, PUT, DELETE API endpoints
Date Tue, 01 Sep 2020 21:25:07 GMT

riahk commented on a change in pull request #10741:
URL: https://github.com/apache/incubator-superset/pull/10741#discussion_r481440513



##########
File path: superset/databases/api.py
##########
@@ -14,126 +14,78 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-from typing import Any, Dict, List, Optional
+import logging
+from typing import Any, Optional
 
+from flask import g, request, Response
 from flask_appbuilder.api import expose, protect, rison, safe
 from flask_appbuilder.models.sqla.interface import SQLAInterface
+from marshmallow import ValidationError
 from sqlalchemy.exc import NoSuchTableError, OperationalError, SQLAlchemyError
 
 from superset import event_logger
+from superset.constants import RouteMethod
+from superset.databases.commands.create import CreateDatabaseCommand
+from superset.databases.commands.delete import DeleteDatabaseCommand
+from superset.databases.commands.exceptions import (
+    DatabaseCreateFailedError,
+    DatabaseDeleteDatasetsExistFailedError,
+    DatabaseDeleteFailedError,
+    DatabaseInvalidError,
+    DatabaseNotFoundError,
+    DatabaseUpdateFailedError,
+)
+from superset.databases.commands.update import UpdateDatabaseCommand
 from superset.databases.decorators import check_datasource_access
+from superset.databases.filters import DatabaseFilter
 from superset.databases.schemas import (
     database_schemas_query_schema,
+    DatabasePostSchema,
+    DatabasePutSchema,
     SchemasResponseSchema,
     SelectStarResponseSchema,
     TableMetadataResponseSchema,
 )
+from superset.databases.utils import get_table_metadata
 from superset.extensions import security_manager
 from superset.models.core import Database
 from superset.typing import FlaskResponse
 from superset.utils.core import error_msg_from_exception
 from superset.views.base_api import BaseSupersetModelRestApi, statsd_metrics
-from superset.views.database.filters import DatabaseFilter
-from superset.views.database.validators import sqlalchemy_uri_validator
-
-
-def get_foreign_keys_metadata(
-    database: Database, table_name: str, schema_name: Optional[str]
-) -> List[Dict[str, Any]]:
-    foreign_keys = database.get_foreign_keys(table_name, schema_name)
-    for fk in foreign_keys:
-        fk["column_names"] = fk.pop("constrained_columns")
-        fk["type"] = "fk"
-    return foreign_keys
-
 
-def get_indexes_metadata(
-    database: Database, table_name: str, schema_name: Optional[str]
-) -> List[Dict[str, Any]]:
-    indexes = database.get_indexes(table_name, schema_name)
-    for idx in indexes:
-        idx["type"] = "index"
-    return indexes
-
-
-def get_col_type(col: Dict[Any, Any]) -> str:
-    try:
-        dtype = f"{col['type']}"
-    except Exception:  # pylint: disable=broad-except
-        # sqla.types.JSON __str__ has a bug, so using __class__.
-        dtype = col["type"].__class__.__name__
-    return dtype
-
-
-def get_table_metadata(
-    database: Database, table_name: str, schema_name: Optional[str]
-) -> Dict[str, Any]:
-    """
-    Get table metadata information, including type, pk, fks.
-    This function raises SQLAlchemyError when a schema is not found.
-
-    :param database: The database model
-    :param table_name: Table name
-    :param schema_name: schema name
-    :return: Dict table metadata ready for API response
-    """
-    keys = []
-    columns = database.get_columns(table_name, schema_name)
-    primary_key = database.get_pk_constraint(table_name, schema_name)
-    if primary_key and primary_key.get("constrained_columns"):
-        primary_key["column_names"] = primary_key.pop("constrained_columns")
-        primary_key["type"] = "pk"
-        keys += [primary_key]
-    foreign_keys = get_foreign_keys_metadata(database, table_name, schema_name)
-    indexes = get_indexes_metadata(database, table_name, schema_name)
-    keys += foreign_keys + indexes
-    payload_columns: List[Dict[str, Any]] = []
-    for col in columns:
-        dtype = get_col_type(col)
-        payload_columns.append(
-            {
-                "name": col["name"],
-                "type": dtype.split("(")[0] if "(" in dtype else dtype,
-                "longType": dtype,
-                "keys": [k for k in keys if col["name"] in k["column_names"]],
-            }
-        )
-    return {
-        "name": table_name,
-        "columns": payload_columns,
-        "selectStar": database.select_star(
-            table_name,
-            schema=schema_name,
-            show_cols=True,
-            indent=True,
-            cols=columns,
-            latest_partition=True,
-        ),
-        "primaryKey": primary_key,
-        "foreignKeys": foreign_keys,
-        "indexes": keys,
-    }
+logger = logging.getLogger(__name__)
 
 
 class DatabaseRestApi(BaseSupersetModelRestApi):
     datamodel = SQLAInterface(Database)
 
-    include_route_methods = {
-        "get_list",
+    include_route_methods = RouteMethod.REST_MODEL_VIEW_CRUD_SET | {
         "table_metadata",
         "select_star",
         "schemas",
     }
     class_permission_name = "DatabaseView"
-    method_permission_name = {
-        "get_list": "list",
-        "table_metadata": "list",
-        "select_star": "list",
-        "schemas": "list",
-    }
     resource_name = "database"
     allow_browser_login = True
     base_filters = [["id", DatabaseFilter, lambda: []]]
+    show_columns = [
+        "id",
+        "database_name",
+        "cache_timeout",
+        "expose_in_sqllab",
+        "allow_run_async",
+        "allow_csv_upload",
+        "allow_ctas",
+        "allow_cvas",
+        "allow_dml",
+        "force_ctas_schema",
+        "allow_multi_schema_metadata_fetch",
+        "impersonate_user",
+        "encrypted_extra",
+        "extra",
+        "server_cert",
+        "sqlalchemy_uri",
+    ]

Review comment:
       Should we add all these fields to `list_columns` as well? Most of them are already
included anyway, so it would save a GET request when opening the edit modal.




----------------------------------------------------------------
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


Mime
View raw message