Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: database API OpenAPI spec and location #10303

Merged
merged 3 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion superset/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def init_views(self) -> None:
Dashboard,
DashboardModelViewAsync,
)
from superset.views.database.api import DatabaseRestApi
from superset.databases.api import DatabaseRestApi
from superset.views.database.views import (
DatabaseView,
CsvToDatabaseView,
Expand Down
16 changes: 16 additions & 0 deletions superset/databases/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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.
112 changes: 18 additions & 94 deletions superset/views/database/api.py → superset/databases/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
from sqlalchemy.exc import NoSuchTableError, SQLAlchemyError

from superset import event_logger
from superset.databases.decorators import check_datasource_access
from superset.databases.schemas import (
SelectStarResponseSchema,
TableMetadataResponseSchema,
)
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
from superset.views.database.decorators import check_datasource_access
from superset.views.database.filters import DatabaseFilter
from superset.views.database.mixins import DatabaseMixin
from superset.views.database.validators import sqlalchemy_uri_validator


Expand Down Expand Up @@ -63,9 +66,8 @@ 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.

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
Expand Down Expand Up @@ -110,7 +112,7 @@ def get_table_metadata(
}


class DatabaseRestApi(DatabaseMixin, BaseSupersetModelRestApi):
class DatabaseRestApi(BaseSupersetModelRestApi):
datamodel = SQLAInterface(Database)

include_route_methods = {"get_list", "table_metadata", "select_star"}
Expand Down Expand Up @@ -141,13 +143,15 @@ class DatabaseRestApi(DatabaseMixin, BaseSupersetModelRestApi):
"backend",
"function_names",
]
show_columns = list_columns

# Removes the local limit for the page size
max_page_size = -1
validators_columns = {"sqlalchemy_uri": sqlalchemy_uri_validator}

openapi_spec_tag = "Database"
openapi_spec_component_schemas = (
TableMetadataResponseSchema,
SelectStarResponseSchema,
)

@expose("/<int:pk>/table/<table_name>/<schema_name>/", methods=["GET"])
@protect()
Expand Down Expand Up @@ -179,87 +183,11 @@ def table_metadata(
description: Table schema
responses:
200:
description: Table schema info
description: Table metadata information
content:
text/plain:
application/json:
schema:
type: object
properties:
columns:
type: array
description: Table columns info
items:
type: object
properties:
keys:
type: array
items:
type: string
longType:
type: string
name:
type: string
type:
type: string
foreignKeys:
type: array
description: Table list of foreign keys
items:
type: object
properties:
column_names:
type: array
items:
type: string
name:
type: string
options:
type: object
referred_columns:
type: array
items:
type: string
referred_schema:
type: string
referred_table:
type: string
type:
type: string
indexes:
type: array
description: Table list of indexes
items:
type: object
properties:
column_names:
type: array
items:
type: string
name:
type: string
options:
type: object
referred_columns:
type: array
items:
type: string
referred_schema:
type: string
referred_table:
type: string
type:
type: string
primaryKey:
type: object
properties:
column_names:
type: array
items:
type: string
name:
type: string
type:
type: string
$ref: "#/components/schemas/TableMetadataResponseSchema"
400:
$ref: '#/components/responses/400'
401:
Expand Down Expand Up @@ -311,15 +239,11 @@ def select_star(
description: Table schema
responses:
200:
description: select star for table
description: Query result
dpgaspar marked this conversation as resolved.
Show resolved Hide resolved
content:
text/plain:
application/json:
schema:
type: object
properties:
result:
type: string
description: SQL select star
$ref: "#/components/schemas/SelectStarResponseSchema"
400:
$ref: '#/components/responses/400'
401:
Expand Down
File renamed without changes.
79 changes: 79 additions & 0 deletions superset/databases/schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# 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 marshmallow import fields, Schema


class TableMetadataOptionsResponseSchema(Schema):
deferrable = fields.Bool()
initially = fields.Bool()
match = fields.Bool()
ondelete = fields.Bool()
onupdate = fields.Bool()


class TableMetadataColumnsResponseSchema(Schema):
keys = fields.List(fields.String(), description="")
longType = fields.String(description="The actual backend long type for the column")
name = fields.String(description="The column name")
type = fields.String(description="The column type")
duplicates_constraint = fields.String(required=False)


class TableMetadataForeignKeysIndexesResponseSchema(Schema):
column_names = fields.List(
fields.String(
description="A list of column names that compose the foreign key or index"
)
)
name = fields.String(description="The name of the foreign key or index")
options = fields.Nested(TableMetadataOptionsResponseSchema)
referred_columns = fields.List(fields.String())
referred_schema = fields.String()
referred_table = fields.String()
type = fields.String()


class TableMetadataPrimaryKeyResponseSchema(Schema):
column_names = fields.List(
fields.String(description="A list of column names that compose the primary key")
)
name = fields.String(description="The primary key index name")
type = fields.String()


class TableMetadataResponseSchema(Schema):
name = fields.String(description="The name of the table")
columns = fields.List(
fields.Nested(TableMetadataColumnsResponseSchema),
description="A list of columns and their metadata",
)
foreignKeys = fields.List(
fields.Nested(TableMetadataForeignKeysIndexesResponseSchema),
description="A list of foreign keys and their metadata",
)
indexes = fields.List(
fields.Nested(TableMetadataForeignKeysIndexesResponseSchema),
description="A list of indexes and their metadata",
)
primaryKey = fields.Nested(
TableMetadataPrimaryKeyResponseSchema, description="Primary keys metadata"
)
selectStar = fields.String(description="SQL select star")


class SelectStarResponseSchema(Schema):
result = fields.String(description="SQL select star")