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

chore: switching out ConnectorRegistry references for DatasourceDAO #20380

Merged
merged 32 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0fbde4a
rename and move dao file
pkdotson May 18, 2022
148f400
Update dao.py
hughhhh May 18, 2022
c63d575
add cachekey
pkdotson May 18, 2022
139e8f4
Merge branch 'connreg-to-datadao_2' of https://github.com/preset-io/s…
pkdotson May 18, 2022
bb79bc6
Update __init__.py
hughhhh May 18, 2022
8119f7a
change reference in query context test
pkdotson May 18, 2022
7804433
add utils ref
pkdotson May 18, 2022
7ea4d1e
more ref changes
pkdotson May 18, 2022
e691320
add helpers
pkdotson May 18, 2022
d0a694a
add todo in dashboard.py
pkdotson May 19, 2022
c92443a
add cachekey
pkdotson May 19, 2022
606a285
circular import error in dar.py
pkdotson May 19, 2022
f0d308f
push rest of refs
pkdotson May 19, 2022
0e44849
fix linting
hughhhh May 20, 2022
ea7ee06
Merge branch 'master' of https://github.com/apache/superset into conn…
hughhhh May 20, 2022
6439dfa
fix more linting
hughhhh May 20, 2022
36bf229
update enum
hughhhh May 20, 2022
ba919fc
remove references for connector registry
hughhhh May 23, 2022
5dff58f
fixed merge conflicts
hughhhh Jun 5, 2022
95c5eb2
big reafctor
hughhhh Jun 5, 2022
e08c4b8
take value
hughhhh Jun 5, 2022
ec17f51
fix
hughhhh Jun 5, 2022
19de44a
test to see if removing value works
hughhhh Jun 6, 2022
b678a8b
delete connectregistry
hughhhh Jun 6, 2022
748805e
address concerns
hughhhh Jun 7, 2022
eb0da19
address comments
hughhhh Jun 7, 2022
b9b77b6
fix merge conflicts
hughhhh Jun 14, 2022
a7555ee
fix merge conflicts
hughhhh Jun 14, 2022
64ecbfe
address concern II
hughhhh Jun 15, 2022
f282ad2
address concern II
hughhhh Jun 15, 2022
0653a58
Merge branch 'connreg-to-datadao_2' of https://github.com/preset-io/s…
hughhhh Jun 15, 2022
ed31bf1
fix test
hughhhh Jun 16, 2022
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
1 change: 0 additions & 1 deletion superset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from werkzeug.local import LocalProxy

from superset.app import create_app
from superset.connectors.connector_registry import ConnectorRegistry
from superset.extensions import (
appbuilder,
cache_manager,
Expand Down
6 changes: 3 additions & 3 deletions superset/cachekeys/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from sqlalchemy.exc import SQLAlchemyError

from superset.cachekeys.schemas import CacheInvalidationRequestSchema
from superset.connectors.connector_registry import ConnectorRegistry
from superset.connectors.sqla.models import SqlaTable
from superset.extensions import cache_manager, db, event_logger
from superset.models.cache import CacheKey
from superset.views.base_api import BaseSupersetModelRestApi, statsd_metrics
Expand Down Expand Up @@ -83,13 +83,13 @@ def invalidate(self) -> Response:
return self.response_400(message=str(error))
datasource_uids = set(datasources.get("datasource_uids", []))
for ds in datasources.get("datasources", []):
ds_obj = ConnectorRegistry.get_datasource_by_name(
ds_obj = SqlaTable.get_datasource_by_name(
session=db.session,
datasource_type=ds.get("datasource_type"),
datasource_name=ds.get("datasource_name"),
schema=ds.get("schema"),
database_name=ds.get("database_name"),
)

if ds_obj:
datasource_uids.add(ds_obj.uid)

Expand Down
11 changes: 6 additions & 5 deletions superset/commands/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
OwnersNotFoundValidationError,
RolesNotFoundValidationError,
)
from superset.connectors.connector_registry import ConnectorRegistry
from superset.datasets.commands.exceptions import DatasetNotFoundError
from superset.dao.exceptions import DatasourceNotFound
from superset.datasource.dao import DatasourceDAO
from superset.extensions import db, security_manager
from superset.utils.core import DatasourceType

if TYPE_CHECKING:
from superset.connectors.base.models import BaseDatasource
Expand Down Expand Up @@ -79,8 +80,8 @@ def populate_roles(role_ids: Optional[List[int]] = None) -> List[Role]:

def get_datasource_by_id(datasource_id: int, datasource_type: str) -> BaseDatasource:
try:
return ConnectorRegistry.get_datasource(
datasource_type, datasource_id, db.session
return DatasourceDAO.get_datasource(
db.session, DatasourceType(datasource_type), datasource_id
)
except DatasetNotFoundError as ex:
except DatasourceNotFound as ex:
raise DatasourceNotFoundValidationError() from ex
10 changes: 5 additions & 5 deletions superset/common/query_context_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from superset.common.chart_data import ChartDataResultFormat, ChartDataResultType
from superset.common.query_context import QueryContext
from superset.common.query_object_factory import QueryObjectFactory
from superset.connectors.connector_registry import ConnectorRegistry
from superset.utils.core import DatasourceDict
from superset.datasource.dao import DatasourceDAO
from superset.utils.core import DatasourceDict, DatasourceType

if TYPE_CHECKING:
from superset.connectors.base.models import BaseDatasource
Expand All @@ -32,7 +32,7 @@


def create_query_object_factory() -> QueryObjectFactory:
return QueryObjectFactory(config, ConnectorRegistry(), db.session)
return QueryObjectFactory(config, DatasourceDAO(), db.session)


class QueryContextFactory: # pylint: disable=too-few-public-methods
Expand Down Expand Up @@ -80,6 +80,6 @@ def create(

# pylint: disable=no-self-use
def _convert_to_model(self, datasource: DatasourceDict) -> BaseDatasource:
return ConnectorRegistry.get_datasource(
str(datasource["type"]), int(datasource["id"]), db.session
return DatasourceDAO.get_datasource(
db.session, DatasourceType(datasource["type"]), int(datasource["id"])
)
16 changes: 9 additions & 7 deletions superset/common/query_object_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,29 @@

from superset.common.chart_data import ChartDataResultType
from superset.common.query_object import QueryObject
from superset.utils.core import apply_max_row_limit, DatasourceDict
from superset.utils.core import apply_max_row_limit, DatasourceDict, DatasourceType
from superset.utils.date_parser import get_since_until

if TYPE_CHECKING:
from sqlalchemy.orm import sessionmaker

from superset import ConnectorRegistry
from superset.connectors.base.models import BaseDatasource
from superset.datasource.dao import DatasourceDAO


class QueryObjectFactory: # pylint: disable=too-few-public-methods
_config: Dict[str, Any]
_connector_registry: ConnectorRegistry
_datasource_dao: DatasourceDAO
_session_maker: sessionmaker

def __init__(
self,
app_configurations: Dict[str, Any],
connector_registry: ConnectorRegistry,
_datasource_dao: DatasourceDAO,
session_maker: sessionmaker,
):
self._config = app_configurations
self._connector_registry = connector_registry
self._datasource_dao = _datasource_dao
self._session_maker = session_maker

def create( # pylint: disable=too-many-arguments
Expand Down Expand Up @@ -75,8 +75,10 @@ def create( # pylint: disable=too-many-arguments
)

def _convert_to_model(self, datasource: DatasourceDict) -> BaseDatasource:
return self._connector_registry.get_datasource(
str(datasource["type"]), int(datasource["id"]), self._session_maker()
return self._datasource_dao.get_datasource(
datasource_type=DatasourceType(datasource["type"]),
datasource_id=int(datasource["id"]),
session=self._session_maker(),
)

def _process_extras( # pylint: disable=no-self-use
Expand Down
164 changes: 0 additions & 164 deletions superset/connectors/connector_registry.py

This file was deleted.

43 changes: 43 additions & 0 deletions superset/connectors/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
List,
NamedTuple,
Optional,
Set,
Tuple,
Type,
Union,
Expand Down Expand Up @@ -1988,6 +1989,48 @@ def query_datasources_by_name(
query = query.filter_by(schema=schema)
return query.all()

@classmethod
def query_datasources_by_permissions( # pylint: disable=invalid-name
cls,
session: Session,
database: Database,
permissions: Set[str],
schema_perms: Set[str],
) -> List["SqlaTable"]:
# TODO(hughhhh): add unit test
return (
session.query(cls)
.filter_by(database_id=database.id)
.filter(
or_(
SqlaTable.perm.in_(permissions),
SqlaTable.schema_perm.in_(schema_perms),
)
)
.all()
)

@classmethod
def get_eager_sqlatable_datasource(
cls, session: Session, datasource_id: int
) -> "SqlaTable":
"""Returns SqlaTable with columns and metrics."""
return (
session.query(cls)
.options(
sa.orm.subqueryload(cls.columns),
sa.orm.subqueryload(cls.metrics),
)
.filter_by(id=datasource_id)
.one()
)

@classmethod
def get_all_datasources(cls, session: Session) -> List["SqlaTable"]:
qry = session.query(cls)
qry = cls.default_query(qry)
return qry.all()

@staticmethod
def default_query(qry: Query) -> Query:
return qry.filter_by(is_sqllab_view=False)
Expand Down
Loading