From b6135fd6e3a1e7b4cb4d4ef97f5e1db59f8ebdb3 Mon Sep 17 00:00:00 2001 From: Colin Date: Thu, 28 Mar 2024 13:35:59 -0700 Subject: [PATCH 1/4] revert default psycopg2 back to psycopg2-binary --- .changes/unreleased/Dependencies-20240328-133507.yaml | 6 ++++++ pyproject.toml | 8 ++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 .changes/unreleased/Dependencies-20240328-133507.yaml diff --git a/.changes/unreleased/Dependencies-20240328-133507.yaml b/.changes/unreleased/Dependencies-20240328-133507.yaml new file mode 100644 index 00000000..c7dbd319 --- /dev/null +++ b/.changes/unreleased/Dependencies-20240328-133507.yaml @@ -0,0 +1,6 @@ +kind: Dependencies +body: add "no-binary" install option +time: 2024-03-28T13:35:07.300121-07:00 +custom: + Author: colin-rogers-dbt + Issue: "6" diff --git a/pyproject.toml b/pyproject.toml index 10fd7f7f..94243e92 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,11 +24,13 @@ classifiers = [ ] dependencies = [ "dbt-adapters>=0.1.0a1,<2.0", - "psycopg2>=2.9,<3.0", + "psycopg2-binary>=2.9,<3.0", # installed via dbt-adapters but used directly "dbt-common>=0.1.0a1,<2.0", "agate>=1.0,<2.0", ] +[project.optional-dependencies] +no-binary = ["psycopg2>=2.9,<3.0"] [project.urls] Homepage = "https://github.com/dbt-labs/dbt-postgres" Documentation = "https://docs.getdbt.com" @@ -119,6 +121,8 @@ dependencies = [ "twine", "check-wheel-contents", ] + + [tool.hatch.envs.build.scripts] check-all = [ "- check-wheel", @@ -166,4 +170,4 @@ env_files = ["test.env"] testpaths = [ "tests/functional", "tests/unit", -] +] \ No newline at end of file From 26fed541ca9e1c2baaa72bb03caa53d20cae6d33 Mon Sep 17 00:00:00 2001 From: Colin Date: Mon, 1 Apr 2024 13:38:17 -0700 Subject: [PATCH 2/4] revert default psycopg2 back to psycopg2-binary --- hatch_build.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 15 +++++---------- 2 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 hatch_build.py diff --git a/hatch_build.py b/hatch_build.py new file mode 100644 index 00000000..eecbf407 --- /dev/null +++ b/hatch_build.py @@ -0,0 +1,52 @@ +import logging +import os +import sys +from typing import Any + +from hatchling.builders.config import BuilderConfig +from hatchling.builders.hooks.plugin.interface import BuildHookInterface +from hatchling.plugin import hookimpl + +BASE_DEPS = [ + # psycopg2 dependency installed in custom hatch_build.py + "dbt-adapters>=0.1.0a1,<2.0", + # installed via dbt-adapters but used directly + "dbt-common>=0.1.0a1,<2.0", + "agate>=1.0,<2.0", +] + +PSYCOPG2_MESSAGE = """ +No package name override was set. +Using 'psycopg2-binary' package to satisfy 'psycopg2' + +If you experience segmentation faults, silent crashes, or installation errors, +consider retrying with the 'DBT_PSYCOPG2_NAME' environment variable set to +'psycopg2'. It may require a compiler toolchain and development libraries! +""".strip() + + +def _dbt_psycopg2_name(): + # if the user chose something, use that + package_name = os.getenv("DBT_PSYCOPG2_NAME", "") + if package_name: + return package_name + + # default to psycopg2-binary for all OSes/versions + print(PSYCOPG2_MESSAGE) + return "psycopg2-binary" + + +class CustomBuildHook(BuildHookInterface[BuilderConfig]): + + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + + def initialize(self, version: str, build_data: dict[str, Any]) -> None: + build_data["dependencies"] = BASE_DEPS + psycopg2_pkg_name = _dbt_psycopg2_name() + build_data["dependencies"].append(f"{psycopg2_pkg_name}>=2.9,<3.0") + + +@hookimpl +def hatch_register_build_hook(): + return CustomBuildHook diff --git a/pyproject.toml b/pyproject.toml index 94243e92..e9d880a2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [project] -dynamic = ["version"] +dynamic = ["version", "dependencies"] name = "dbt-postgres" description = "The set of adapter protocols and base functionality that supports integration with dbt-core" readme = "README.md" @@ -22,15 +22,7 @@ classifiers = [ "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", ] -dependencies = [ - "dbt-adapters>=0.1.0a1,<2.0", - "psycopg2-binary>=2.9,<3.0", - # installed via dbt-adapters but used directly - "dbt-common>=0.1.0a1,<2.0", - "agate>=1.0,<2.0", -] -[project.optional-dependencies] -no-binary = ["psycopg2>=2.9,<3.0"] + [project.urls] Homepage = "https://github.com/dbt-labs/dbt-postgres" Documentation = "https://docs.getdbt.com" @@ -51,6 +43,9 @@ packages = ["dbt"] [tool.hatch.version] path = "dbt/adapters/postgres/__version__.py" +[tool.hatch.build.hooks.custom] +path = "./hatch_build.py" + [tool.hatch.envs.default] dependencies = [ "dbt-adapters @ git+https://github.com/dbt-labs/dbt-adapters.git", From 0817ce37b761c3d41e73522d1cbb48c36ae9ea6f Mon Sep 17 00:00:00 2001 From: Colin Date: Mon, 1 Apr 2024 14:54:56 -0700 Subject: [PATCH 3/4] add docstring --- hatch_build.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/hatch_build.py b/hatch_build.py index eecbf407..32a3a4f8 100644 --- a/hatch_build.py +++ b/hatch_build.py @@ -1,6 +1,4 @@ -import logging import os -import sys from typing import Any from hatchling.builders.config import BuilderConfig @@ -37,6 +35,10 @@ def _dbt_psycopg2_name(): class CustomBuildHook(BuildHookInterface[BuilderConfig]): + """ + Custom build hook to install psycopg2 instead of psycopg2-binary based on the presence of `DBT_PSYCOPG2_NAME` env + var. This is necessary as psycopg2-binary is better for local development, but psycopg2 is better for production. + """ def __init__(self, *args: Any, **kwargs: Any) -> None: super().__init__(*args, **kwargs) From 9dc6923a947705f3ca8802318f4acffd2e2c689b Mon Sep 17 00:00:00 2001 From: Colin Date: Mon, 1 Apr 2024 15:50:14 -0700 Subject: [PATCH 4/4] fix type in buildhook initialize --- hatch_build.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hatch_build.py b/hatch_build.py index 32a3a4f8..02693cf0 100644 --- a/hatch_build.py +++ b/hatch_build.py @@ -1,5 +1,5 @@ import os -from typing import Any +from typing import Any, Dict from hatchling.builders.config import BuilderConfig from hatchling.builders.hooks.plugin.interface import BuildHookInterface @@ -43,7 +43,7 @@ class CustomBuildHook(BuildHookInterface[BuilderConfig]): def __init__(self, *args: Any, **kwargs: Any) -> None: super().__init__(*args, **kwargs) - def initialize(self, version: str, build_data: dict[str, Any]) -> None: + def initialize(self, version: str, build_data: Dict) -> None: build_data["dependencies"] = BASE_DEPS psycopg2_pkg_name = _dbt_psycopg2_name() build_data["dependencies"].append(f"{psycopg2_pkg_name}>=2.9,<3.0")