Skip to content

Commit

Permalink
[Regression] [backport 1.8.latest] Fix psycopg2 version install (#113) (
Browse files Browse the repository at this point in the history
  • Loading branch information
mikealfare authored Jun 20, 2024
1 parent 5a5a753 commit 39c1a9f
Show file tree
Hide file tree
Showing 39 changed files with 80 additions and 5,609 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/Fixes-20240605-202614.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Fixes
body: Default to psycopg2-binary and allow overriding to psycopg2 via DBT_PSYCOPG2_NAME
(restores previous behavior)
time: 2024-06-05T20:26:14.801254-04:00
custom:
Author: mikealfare
Issue: "96"
20 changes: 20 additions & 0 deletions .github/scripts/psycopg2-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
python -m venv venv
source venv/bin/activate
python -m pip install .

if [[ "$PSYCOPG2_WORKAROUND" == true ]]; then
if [[ $(pip show psycopg2-binary) ]]; then
PSYCOPG2_VERSION=$(pip show psycopg2-binary | grep Version | cut -d " " -f 2)
pip uninstall -y psycopg2-binary
pip install psycopg2==$PSYCOPG2_VERSION
fi
fi

PSYCOPG2_NAME=$((pip show psycopg2 || pip show psycopg2-binary) | grep Name | cut -d " " -f 2)
if [[ "$PSYCOPG2_NAME" != "$PSYCOPG2_EXPECTED_NAME" ]]; then
echo -e 'Expected: "$PSYCOPG2_EXPECTED_NAME" but found: "$PSYCOPG2_NAME"'
exit 1
fi
deactivate
rm -r ./venv
47 changes: 32 additions & 15 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ defaults:
jobs:
integration:
name: Integration Tests
runs-on: ubuntu-latest
runs-on: ubuntu-22.04

strategy:
fail-fast: false
Expand Down Expand Up @@ -102,24 +102,41 @@ jobs:

psycopg2-check:
name: "Test psycopg2 build version"
runs-on: ${{ matrix.scenario.platform }}
runs-on: ${{ matrix.platform }}
strategy:
fail-fast: false
matrix:
scenario:
- {platform: ubuntu-latest, psycopg2-name: psycopg2}
- {platform: macos-12, psycopg2-name: psycopg2-binary}
platform: [ubuntu-22.04, macos-12]
python-version: ["3.8", "3.11"]
steps:
- name: "Check out repository"
uses: actions/checkout@v4

- name: "Test psycopg2 name"
run: |
python -m pip install .
PSYCOPG2_PIP_ENTRY=$(pip list | grep "psycopg2 " || pip list | grep psycopg2-binary)
echo $PSYCOPG2_PIP_ENTRY
PSYCOPG2_NAME="${PSYCOPG2_PIP_ENTRY%% *}"
echo $PSYCOPG2_NAME
if [[ "${PSYCOPG2_NAME}" != "${{ matrix.scenario.psycopg2-name }}" ]]; then
exit 1
fi
- name: "Set up Python ${{ matrix.python-version }}"
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: "Test psycopg2 name - default"
run: .github/scripts/psycopg2-check.sh
env:
PSYCOPG2_EXPECTED_NAME: psycopg2-binary

- name: "Test psycopg2 name - invalid override"
run: .github/scripts/psycopg2-check.sh
env:
DBT_PSYCOPG2_NAME: rubber-baby-buggy-bumpers
PSYCOPG2_EXPECTED_NAME: psycopg2-binary

- name: "Test psycopg2 name - override"
run: .github/scripts/psycopg2-check.sh
env:
DBT_PSYCOPG2_NAME: psycopg2
PSYCOPG2_EXPECTED_NAME: psycopg2-binary # we have not implemented the hook yet, so this doesn't work

- name: "Test psycopg2 name - manual override"
# verify that the workaround documented in the `README.md` continues to work
run: .github/scripts/psycopg2-check.sh
env:
PSYCOPG2_WORKAROUND: true
PSYCOPG2_EXPECTED_NAME: psycopg2
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,9 @@ cython_debug/

# testing artifacts
/logs

# MacOS
.DS_Store

# vscode
.vscode/
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ more information on using dbt with Postgres, consult [the docs](https://docs.get
- [Install dbt](https://docs.getdbt.com/docs/installation)
- Read the [introduction](https://docs.getdbt.com/docs/introduction/) and [viewpoint](https://docs.getdbt.com/docs/about/viewpoint/)

### `psycopg2-binary` vs. `psycopg2`

By default, `dbt-postgres` installs `psycopg2-binary`. This is great for development, and even testing, as it does not require any OS dependencies; it's a pre-built wheel. However, building `psycopg2` from source will grant performance improvements that are desired in a production environment. In order to install `psycopg2`, use the following steps:

```bash
if [[ $(pip show psycopg2-binary) ]]; then
PSYCOPG2_VERSION=$(pip show psycopg2-binary | grep Version | cut -d " " -f 2)
pip uninstall -y psycopg2-binary
pip install psycopg2==$PSYCOPG2_VERSION
fi
```

This ensures the version of `psycopg2` will match that of `psycopg2-binary`.

## Join the dbt Community

- Be part of the conversation in the [dbt Community Slack](http://community.getdbt.com/)
Expand Down
5 changes: 1 addition & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ classifiers = [
"Programming Language :: Python :: 3.11",
]
dependencies = [
# install `psycopg2` on linux (assumed production)
'psycopg2>=2.9,<3.0; platform_system == "Linux"',
# install `psycopg2-binary` on macos/windows (assumed development)
'psycopg2-binary>=2.9,<3.0; platform_system != "Linux"',
"psycopg2-binary>=2.9,<3.0",
"dbt-adapters>=0.1.0a1,<2.0",
# add dbt-core to ensure backwards compatibility of installation, this is not a functional dependency
"dbt-core>=1.8.0a1",
Expand Down
201 changes: 0 additions & 201 deletions tests/functional/configs/fixtures.py

This file was deleted.

Loading

0 comments on commit 39c1a9f

Please sign in to comment.