| 125 | return Context(create_db_engine(db_url), connection, scope, scope_config, options) |
| 126 | |
| 127 | def create_db_engine(db_url) -> Engine: |
| 128 | # SQLAlchemy doesn't understand postgres:// scheme |
| 129 | db_url = db_url.replace("postgres://", "postgresql://") |
| 130 | # Remove query args |
| 131 | base_url = db_url.split('?')[0] |
| 132 | # `parseTime` parameter is not understood by MySQL driver, |
| 133 | # so we have to parse query args to remove it |
| 134 | connect_args = dict(parse_qsl(urlparse(db_url).query)) |
| 135 | if 'parseTime' in connect_args: |
| 136 | del connect_args['parseTime'] |
| 137 | if 'loc' in connect_args: |
| 138 | del connect_args['loc'] |
| 139 | if 'tls' in connect_args: |
| 140 | del connect_args['tls'] |
| 141 | connect_args['ssl'] = {'verify_cert': 'False'} |
| 142 | try: |
| 143 | engine = create_engine(base_url, connect_args=connect_args) |
| 144 | tables = SubtaskRun.metadata.tables |
| 145 | tables[SubtaskRun.__tablename__].create(engine, checkfirst=True) |
| 146 | return engine |
| 147 | except Exception as e: |
| 148 | raise Exception(f"Unable to make a database connection") from e |