Skip to content

Commit 77d279a

Browse files
committed
Add pre-flight check for CrateDB >= 6.2
1 parent 90a88a6 commit 77d279a

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ dependencies = [
9393
"importlib-resources; python_version<'3.9'", # "meltanolabs-target-postgres==0.0.9",
9494
"meltanolabs-target-postgres @ git+https://github.com/singer-contrib/meltanolabs-target-postgres.git@pgvector",
9595
"sqlalchemy-cratedb[vector]",
96+
"verlib2<0.4",
9697
]
9798
optional-dependencies.all = [
9899
"meltano-target-cratedb[vector]",

target_cratedb/connector.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
from __future__ import annotations
44

5+
import re
56
import typing as t
6-
from builtins import issubclass
7+
from builtins import issubclass # noqa: A004
8+
from contextlib import contextmanager
79
from datetime import datetime
810

911
import sqlalchemy as sa
@@ -13,6 +15,7 @@
1315
from sqlalchemy_cratedb.type.array import _ObjectArray
1416
from sqlalchemy_cratedb.type.object import ObjectTypeImpl
1517
from target_postgres.connector import NOTYPE, PostgresConnector
18+
from verlib2 import Version
1619

1720
from target_cratedb.sqlalchemy.patch import polyfill_refresh_after_dml_engine
1821

@@ -36,6 +39,34 @@ def create_engine(self) -> sa.Engine:
3639
polyfill_refresh_after_dml_engine(engine)
3740
return engine
3841

42+
@contextmanager
43+
def _connect(self) -> t.Iterator[sa.engine.Connection]:
44+
"""
45+
Connect to the database.
46+
47+
Note: Needs to be overwritten to perform a CrateDB version check.
48+
"""
49+
engine = self._engine
50+
with engine.connect().execution_options() as conn:
51+
self._check_cratedb_620(conn)
52+
yield conn
53+
engine.dispose()
54+
55+
def _check_cratedb_620(self, conn: sa.Connection):
56+
"""
57+
Fail if the CrateDB version is lower than 6.2.
58+
59+
-- https://github.com/crate/crate/issues/15161
60+
"""
61+
with conn.begin():
62+
version_raw = conn.exec_driver_sql("SELECT version()").scalar_one()
63+
matches = re.match(r"^CrateDB (\d+\.\d+\.\d+).*", version_raw)
64+
if not matches:
65+
raise ConnectionError("Unable to inquire CrateDB version")
66+
cratedb_version = matches.group(1)
67+
if Version(cratedb_version) < Version("6.2"):
68+
raise ConnectionError("The connector requires CrateDB 6.2 or higher")
69+
3970
@staticmethod
4071
def to_sql_type(jsonschema_type: dict) -> sa.types.TypeEngine:
4172
"""Return a JSON Schema representation of the provided type.

0 commit comments

Comments
 (0)