2022-02-18 07:48:38 +01:00
|
|
|
# Copyright 2021 Collate
|
|
|
|
# Licensed 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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Build and document all supported Engines
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from sqlalchemy import create_engine
|
|
|
|
from sqlalchemy.engine.base import Engine
|
|
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from sqlalchemy.orm.session import Session
|
|
|
|
|
2022-04-05 21:20:39 +02:00
|
|
|
from metadata.generated.schema.metadataIngestion.workflow import Source as SourceConfig
|
2022-02-18 07:48:38 +01:00
|
|
|
|
2022-03-07 00:43:43 +01:00
|
|
|
logger = logging.getLogger("Utils")
|
2022-02-18 07:48:38 +01:00
|
|
|
|
|
|
|
|
2022-04-05 21:20:39 +02:00
|
|
|
# TODO: fix this and use the singledispatch to build the URL instead of get_connection_url
|
|
|
|
def get_engine(config: SourceConfig, verbose: bool = False) -> Engine:
|
2022-02-18 07:48:38 +01:00
|
|
|
"""
|
|
|
|
Given an SQL configuration, build the SQLAlchemy Engine
|
|
|
|
"""
|
2022-04-05 21:20:39 +02:00
|
|
|
logger.info(f"Building Engine for {config.serviceName}...")
|
2022-02-18 07:48:38 +01:00
|
|
|
|
|
|
|
engine = create_engine(
|
2022-03-07 00:43:43 +01:00
|
|
|
config.get_connection_url(),
|
|
|
|
**config.options,
|
|
|
|
connect_args=config.connect_args,
|
2022-02-18 07:48:38 +01:00
|
|
|
echo=verbose,
|
|
|
|
)
|
|
|
|
|
|
|
|
return engine
|
|
|
|
|
|
|
|
|
|
|
|
def create_and_bind_session(engine: Engine) -> Session:
|
|
|
|
"""
|
|
|
|
Given an engine, create a session bound
|
|
|
|
to it to make our operations.
|
|
|
|
"""
|
|
|
|
session = sessionmaker()
|
|
|
|
session.configure(bind=engine)
|
|
|
|
return session()
|