22
33from __future__ import annotations
44
5+ import re
56import typing as t
6- from builtins import issubclass
7+ from builtins import issubclass # noqa: A004
8+ from contextlib import contextmanager
79from datetime import datetime
810
911import sqlalchemy as sa
1315from sqlalchemy_cratedb .type .array import _ObjectArray
1416from sqlalchemy_cratedb .type .object import ObjectTypeImpl
1517from target_postgres .connector import NOTYPE , PostgresConnector
18+ from verlib2 import Version
1619
1720from 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