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

[DOP-13849] - upgrade Postgres packages #251

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion docs/changelog/next_release/249.feature.rst
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Allow passing custom JDBC driver version to ``Clickhouse.get_packages(package_version=...)``.
:class:`Clickhouse` connection supports passing custom versions: ``Clickhouse.get_packages(package_version=...)``.
1 change: 1 addition & 0 deletions docs/changelog/next_release/251.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:class:`Postgres` connection now uses PostgreSQL JDBC driver ``42.7.3``, upgraded from ``42.6.0``, and supports passing custom versions: ``Postgres.get_packages(package_version=...)``.
22 changes: 11 additions & 11 deletions onetl/connection/db_connection/clickhouse/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ def get_packages(
apache_http_client_version: str | None = None,
) -> list[str]:
"""
Get package names to be downloaded by Spark. |support_hooks|
Get package names to be downloaded by Spark. Allows specifying custom JDBC and Apache HTTP Client versions. |support_hooks|

Parameters
----------
package_version : str , optional
package_version : str, optional
ClickHouse JDBC version client packages. Defaults to ``0.6.0``.

apache_http_client_version : str, optional
Expand All @@ -139,19 +139,19 @@ def get_packages(
``com.clickhouse:clickhouse-jdbc:0.6.0:all`` to install all required packages.

"""
package_version_obj = Version(package_version).min_digits(3) if package_version else Version("0.6.0")
apache_http_client_version_obj = (
Version(apache_http_client_version).min_digits(3) if apache_http_client_version else Version("5.3.1")
)
default_jdbc_version = "0.6.0"
default_http_version = "5.3.1"

jdbc_version = Version(package_version or default_jdbc_version).min_digits(3)
http_version = Version(apache_http_client_version or default_http_version).min_digits(3)

result = [
f"com.clickhouse:clickhouse-jdbc:{package_version_obj}",
f"com.clickhouse:clickhouse-http-client:{package_version_obj}",
f"com.clickhouse:clickhouse-jdbc:{jdbc_version}",
f"com.clickhouse:clickhouse-http-client:{jdbc_version}",
]

if package_version_obj >= Version("0.5.0"):
# before 0.5.0 builtin Java HTTP Client was used
result.append(f"org.apache.httpcomponents.client5:httpclient5:{apache_http_client_version_obj}")
if jdbc_version >= Version("0.5.0"):
result.append(f"org.apache.httpcomponents.client5:httpclient5:{http_version}")

return result

Expand Down
22 changes: 17 additions & 5 deletions onetl/connection/db_connection/postgres/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import ClassVar

from onetl._util.classproperty import classproperty
from onetl._util.version import Version
from onetl.connection.db_connection.jdbc_connection import JDBCConnection
from onetl.connection.db_connection.jdbc_mixin.options import JDBCOptions
from onetl.connection.db_connection.postgres.dialect import PostgresDialect
Expand All @@ -27,7 +28,7 @@ class Config:
class Postgres(JDBCConnection):
"""PostgreSQL JDBC connection. |support_hooks|

Based on Maven package ``org.postgresql:postgresql:42.6.0``
Based on Maven package ``org.postgresql:postgresql:42.7.3``
(`official Postgres JDBC driver <https://jdbc.postgresql.org/>`_).

.. warning::
Expand Down Expand Up @@ -105,9 +106,14 @@ class Postgres(JDBCConnection):

@slot
@classmethod
def get_packages(cls) -> list[str]:
def get_packages(cls, package_version: str | None = None) -> list[str]:
"""
Get package names to be downloaded by Spark. |support_hooks|
Get package names to be downloaded by Spark. Allows specifying a custom JDBC driver version. |support_hooks|

Parameters
----------
package_version : str, optional
Specifies the version of the PostgreSQL JDBC driver to use. Defaults to ``42.7.3``.

Examples
--------
Expand All @@ -118,15 +124,21 @@ def get_packages(cls) -> list[str]:

Postgres.get_packages()

# custom package version
Postgres.get_packages(package_version="42.6.0")

"""
return ["org.postgresql:postgresql:42.6.0"]
default_version = "42.7.3"
version = Version(package_version or default_version).min_digits(3)

return [f"org.postgresql:postgresql:{version}"]

@classproperty
def package(cls) -> str:
"""Get package name to be downloaded by Spark."""
msg = "`Postgres.package` will be removed in 1.0.0, use `Postgres.get_packages()` instead"
warnings.warn(msg, UserWarning, stacklevel=3)
return "org.postgresql:postgresql:42.6.0"
return "org.postgresql:postgresql:42.7.3"

@property
def jdbc_url(self) -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_clickhouse_get_packages(package_version, apache_http_client_version, ex
("a.b.c", "5.3.1"),
],
)
def test_invalid_versions_raise_error(package_version, apache_http_client_version):
def test_clickhouse_get_packages_invalid_version(package_version, apache_http_client_version):
with pytest.raises(
ValueError,
match=rf"Version '{package_version}' does not have enough numeric components for requested format \(expected at least 3\).",
Expand Down
40 changes: 32 additions & 8 deletions tests/tests_unit/tests_db_connection_unit/test_postgres_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,38 @@ def test_postgres_class_attributes():
def test_postgres_package():
warning_msg = re.escape("will be removed in 1.0.0, use `Postgres.get_packages()` instead")
with pytest.warns(UserWarning, match=warning_msg):
assert Postgres.package == "org.postgresql:postgresql:42.6.0"


def test_postgres_get_packages():
assert Postgres.get_packages() == ["org.postgresql:postgresql:42.6.0"]


def test_oracle_missing_package(spark_no_packages):
assert Postgres.package == "org.postgresql:postgresql:42.7.3"


@pytest.mark.parametrize(
"package_version, expected_packages",
[
(None, ["org.postgresql:postgresql:42.7.3"]),
("42.7.3", ["org.postgresql:postgresql:42.7.3"]),
("42.7.3-patch", ["org.postgresql:postgresql:42.7.3-patch"]),
("42.6.0", ["org.postgresql:postgresql:42.6.0"]),
],
)
def test_postgres_get_packages(package_version, expected_packages):
assert Postgres.get_packages(package_version=package_version) == expected_packages


@pytest.mark.parametrize(
"package_version",
[
"42.2",
"abc",
],
)
def test_postgres_get_packages_invalid_version(package_version):
with pytest.raises(
ValueError,
match=rf"Version '{package_version}' does not have enough numeric components for requested format \(expected at least 3\).",
):
Postgres.get_packages(package_version=package_version)


def test_postgres_missing_package(spark_no_packages):
msg = "Cannot import Java class 'org.postgresql.Driver'"
with pytest.raises(ValueError, match=msg):
Postgres(
Expand Down