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

revert default psycopg2 back to psycopg2-binary #41

Merged
merged 4 commits into from
Apr 2, 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
6 changes: 6 additions & 0 deletions .changes/unreleased/Dependencies-20240328-133507.yaml
Original file line number Diff line number Diff line change
@@ -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"
54 changes: 54 additions & 0 deletions hatch_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import os
from typing import Any, Dict

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]):
"""
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)

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")


@hookimpl
def hatch_register_build_hook():
return CustomBuildHook
17 changes: 8 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -22,13 +22,7 @@ classifiers = [
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
]
dependencies = [
"dbt-adapters>=0.1.0a1,<2.0",
"psycopg2>=2.9,<3.0",
# installed via dbt-adapters but used directly
"dbt-common>=0.1.0a1,<2.0",
"agate>=1.0,<2.0",
]

[project.urls]
Homepage = "https://github.com/dbt-labs/dbt-postgres"
Documentation = "https://docs.getdbt.com"
Expand All @@ -49,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",
Expand Down Expand Up @@ -119,6 +116,8 @@ dependencies = [
"twine",
"check-wheel-contents",
]


[tool.hatch.envs.build.scripts]
check-all = [
"- check-wheel",
Expand Down Expand Up @@ -166,4 +165,4 @@ env_files = ["test.env"]
testpaths = [
"tests/functional",
"tests/unit",
]
]
Loading