Skip to content

Commit 51acb26

Browse files
committed
Merge branch 'patched' into fix/test-fixes-remove-dbt-patched
# Conflicts: # .github/workflows/patched-integration-tests-sqlserver.yml # .github/workflows/patched-publish-docker.yml # .github/workflows/patched-unit-tests-patched.yml # Makefile # dbt/adapters/sqlserver/sqlserver_adapter.py # dbt/adapters/sqlserver/sqlserver_column.py # dbt/include/sqlserver/macros/adapters/columns.sql # dbt/include/sqlserver/macros/materializations/unit_test/unit_test_table.sql
2 parents 7b2aac6 + c73497c commit 51acb26

23 files changed

Lines changed: 613 additions & 139 deletions

File tree

.devcontainer/Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM mcr.microsoft.com/devcontainers/python:3.11-trixie
2+
3+
RUN rm -f /etc/apt/sources.list.d/yarn.list \
4+
/etc/apt/sources.list.d/yarn.sources \
5+
/usr/share/keyrings/yarnkey.gpg \
6+
/etc/apt/keyrings/yarn-archive-keyring.gpg
7+
8+
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/

.devcontainer/devcontainer.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
{
2-
"name": "Python 3",
3-
"image": "mcr.microsoft.com/devcontainers/python:1-3.10-bookworm",
2+
"name": "DBT - SQL Server",
3+
"build": {
4+
"dockerfile": "Dockerfile"
5+
},
46
"features": {
5-
"ghcr.io/devcontainers/features/docker-in-docker:2.12.0": {}
7+
"ghcr.io/devcontainers/features/docker-in-docker:2.16.1": {
8+
"moby": false
9+
}
610
},
711
"forwardPorts": [1433],
8-
"postStartCommand": "/bin/bash ./.devcontainer/setup_odbc.sh & /bin/bash ./.devcontainer/setup_env.sh",
12+
"postCreateCommand": "/bin/bash ./.devcontainer/setup_odbc.sh && /bin/bash ./.devcontainer/setup_env.sh",
13+
"postStartCommand": "docker compose up -d",
914
"containerEnv": {
1015
"SQLSERVER_TEST_DRIVER": "ODBC Driver 18 for SQL Server",
1116
"SQLSERVER_TEST_HOST": "127.0.0.1",

.devcontainer/setup_env.sh

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
cp test.env.sample test.env
1+
#!/bin/bash
2+
set -euo pipefail
23

3-
docker compose build
4-
docker compose up -d
5-
6-
pip install -r dev_requirements.txt
4+
if [ ! -f test.env ]; then
5+
cp test.env.sample test.env
6+
fi
7+
export UV_VENV_CLEAR=1
8+
uv venv .venv
9+
uv pip install --python .venv/bin/python -r dev_requirements.txt
10+
.venv/bin/pre-commit install

.devcontainer/setup_odbc.sh

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
curl https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc
1+
sudo install -d -m 0755 /usr/share/keyrings
2+
curl -fsSL https://packages.microsoft.com/keys/microsoft-rolling.asc | gpg --dearmor | sudo tee /usr/share/keyrings/microsoft-prod.gpg >/dev/null
23

34
#Download appropriate package for the OS version
45
#Choose only ONE of the following, corresponding to your OS version
56

6-
#Debian 12
7-
curl https://packages.microsoft.com/config/debian/12/prod.list | sudo tee /etc/apt/sources.list.d/mssql-release.list
7+
#Debian 13 (trixie)
8+
curl -fsSL https://packages.microsoft.com/config/debian/13/prod.list | sudo tee /etc/apt/sources.list.d/mssql-release.list >/dev/null
89

9-
sudo apt-get update
10-
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql18
10+
sudo apt update
11+
sudo ACCEPT_EULA=Y apt install -y msodbcsql18
1112
# optional: for bcp and sqlcmd
12-
sudo ACCEPT_EULA=Y apt-get install -y mssql-tools18
13+
sudo ACCEPT_EULA=Y apt install -y mssql-tools18
1314
echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bashrc
1415
source ~/.bashrc
1516
# optional: for unixODBC development headers
16-
sudo apt-get install -y unixodbc-dev
17+
sudo apt install -y unixodbc-dev
1718
# optional: kerberos library for debian-slim distributions
18-
sudo apt-get install -y libgssapi-krb5-2
19+
sudo apt install -y libgssapi-krb5-2

.github/copilot-instructions.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Copilot instructions — dbt-sqlserver
2+
3+
Purpose
4+
- Quickly orient AI coding agents to the structure, conventions, and workflows required to make safe, useful changes to the dbt-sqlserver adapter.
5+
- Focuses on concrete, discoverable facts: major entry points, test/run commands, files to edit for common changes, and CI/development quirks.
6+
7+
Branch and patching purpose
8+
- This repository comes from a fork of upstream dbt-sqlserver
9+
- This file lives in the `patched` branch and documents the current state of the codebase
10+
- All new feature and bugfix branches should be created off `master` and merged into `patched` for internal production ready code.
11+
12+
Big picture (keep in mind)
13+
- This repository implements a dbt adapter for Microsoft SQL Server / Azure SQL. Core adapter code is under `dbt/adapters/sqlserver/` and is built on top of `dbt.adapters.fabric` (`FabricAdapter`).
14+
- Key responsibilities:
15+
- Connection & auth: `sqlserver_connections.py` (pyodbc + AAD support; extends FabricConnectionManager)
16+
- Credentials: `sqlserver_credentials.py` (dataclass, inherits FabricCredentials)
17+
- Adapter surface: `sqlserver_adapter.py` (exports `ConnectionManager`, `Column`, `Relation`, constraint rendering, date function, and incremental strategies)
18+
- Relation policies and limits: `relation_configs/policies.py` and `sqlserver_relation.py` (identifier length enforcement — max 127 chars)
19+
- SQL helper macros & materializations: `dbt/include/sqlserver/macros/…` and `dbt/include/sqlserver/materializations/…` (use these as canonical SQL patterns)
20+
21+
Concrete code patterns & examples
22+
- Adapter subclass pattern: `SQLServerAdapter(FabricAdapter)` sets class attributes to concrete implementations. To add new surface behavior, add/override methods in `sqlserver_adapter.py` and supply new classes for `ConnectionManager`, `Column`, or `Relation`.
23+
- Connection/auth extension: `sqlserver_connections.py` merges Fabric's `AZURE_AUTH_FUNCTIONS` and adds `"serviceprincipal"` and `"msi"` keys. To add new auth types, provide a function that returns an `azure.core.credentials.AccessToken` and register it in `AZURE_AUTH_FUNCTIONS`.
24+
- Constraints: `render_model_constraint` produces SQL like `add constraint {name} unique nonclustered(col1,col2)` — note the adapter intentionally uses nonclustered constraints.
25+
- Limits and pagination: `SQLServerRelation.render_limited()` uses SQL Server `TOP` and special aliasing for `limit == 0`.
26+
- Versioning: adapter version is declared in `dbt/adapters/sqlserver/__version__.py``sqlserver_connections` uses it to set `APP` in the connection string.
27+
28+
Tests & CI (how to run locally and what CI does)
29+
- Install dev/test deps locally with `make dev` or `uv venv .venv && uv pip install --python .venv/bin/python -r dev_requirements.txt`.
30+
- Unit tests: `make unit` or `pytest -n auto -ra -v tests/unit`.
31+
- Functional tests: `make functional` or:
32+
- Example: `pytest -ra -v tests/functional --profile "ci_sql_server"`
33+
- Functional tests select dbt profile via `--profile` (see `tests/conftest.py` for profiles: `ci_sql_server`, `ci_azure_*`, `user`, `user_azure`)
34+
- CI sets environment variables such as `DBT_TEST_USER_1`, `SQLSERVER_TEST_DRIVER` (see `.github/workflows/integration-tests-sqlserver.yml`).
35+
- Local SQL Server for functional testing: `make server` (uses `docker-compose.yml` and `devops/server.Dockerfile`) or `docker compose up -d`.
36+
- CI images and behavior: workflows build and use GHCR images named like `ghcr.io/${{ github.repository }}:CI-<py>-msodbc18` and `server-<version>`; updating CI container logic requires editing `devops/*` and `.github/workflows/*`.
37+
38+
Development environment & onboarding
39+
- Recommended Python version for development: Python 3.10.
40+
- Preferred bootstrapping flow uses `uv` and a local `.venv`:
41+
- `uv venv .venv`
42+
- `uv pip install --python .venv/bin/python -r dev_requirements.txt`
43+
- Install dev dependencies and enable pre-commit with `make dev`; run `make help` to list Makefile targets (e.g. `make unit`, `make functional`, `make server`).
44+
- A devcontainer is provided (since v1.7.2) for a consistent development environment — look for `.devcontainer` in the repo when onboarding.
45+
46+
Test environment file
47+
- Functional tests expect a `test.env` file in the repo root. Create it from the sample: `cp test.env.sample test.env`.
48+
- Functional tests require three test users for grants-related tests. Ensure these environment variables are set in `test.env` or your environment:
49+
- `DBT_TEST_USER_1`
50+
- `DBT_TEST_USER_2`
51+
- `DBT_TEST_USER_3`
52+
53+
Releasing a new version
54+
- To cut a release: bump the version in `dbt/adapters/sqlserver/__version__.py`, create a git tag named `v<version>`, and push it to GitHub. The `release-version` workflow will publish the package to PyPI.
55+
56+
Project conventions and gotchas
57+
- Many classes use Python dataclasses (credentials, relation policies) — follow that style for new small value types.
58+
- String length limit: identifiers are limited to 127 chars (`MAX_CHARACTERS_IN_IDENTIFIER` in `relation_configs/policies.py`). Tests and runtime enforce this in `sqlserver_relation.py`.
59+
- SQL is generated with explicit SQL Server semantics (e.g., uses `TOP`, nonclustered constraints, `CAST(... as datetimeoffset)` for time filters). Prefer existing macros in `dbt/include/sqlserver/macros` when creating SQL.
60+
- Authentication modes: `authentication` in credentials can be `sql`, `serviceprincipal`, `msi` or Fabric-provided options (see `sqlserver_connections.py` and tests). When adding auth modes, add tests under `tests/functional` and `tests/unit/adapters/mssql`.
61+
- Logging/debug: connection-level logs use logger name `sqlserver` (see `sqlserver_connections.py`). Use these logs when debugging connection/auth issues.
62+
63+
Where to change common features
64+
- To change connection behavior or add a new driver tweak: edit `dbt/adapters/sqlserver/sqlserver_connections.py` and corresponding unit tests in `tests/unit/adapters/mssql`.
65+
- To add adapter-level behavior: edit `dbt/adapters/sqlserver/sqlserver_adapter.py` and add tests under `tests/functional/adapter/dbt`.
66+
- To expose or change macros/materializations: update files under `dbt/include/sqlserver/macro*` and `materializations/*`.
67+
68+
Quick checklist for a typical change
69+
- Add/modify implementation in `dbt/adapters/sqlserver/*`.
70+
- Add unit tests in `tests/unit` and functional tests in `tests/functional` (pick proper profile).
71+
- Run `make dev` to install test deps and enable pre-commit, run `make unit` and `make functional` locally where possible.
72+
- If the change impacts connection images or CI, update `devops/Dockerfile*`, `docker-compose.yml`, and `.github/workflows/*` accordingly.
73+
74+
If anything here is unclear or you need more detail on specific areas (auth flows, CI images, macros), find files or behavior you want expanded and iterate.

.github/workflows/integration-tests-sqlserver.yml renamed to .github/workflows/patched-integration-tests-sqlserver.yml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
---
2-
name: Integration tests on SQL Server
2+
name: Patched Integration tests on SQL Server
33
on: # yamllint disable-line rule:truthy
44
workflow_dispatch:
55
push:
66
branches:
7-
- master
7+
- patched
88
- v*
99
pull_request:
1010
branches:
11-
- master
11+
- patched
1212
- v*
13-
schedule:
14-
- cron: '0 22 * * 0'
1513

1614
jobs:
17-
integration-tests-sql-server:
15+
patched-integration-tests-sql-server:
1816
name: Regular
1917
strategy:
2018
matrix:
21-
python_version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
22-
msodbc_version: ["17", "18"]
23-
sqlserver_version: ["2017", "2019", "2022"]
24-
collation: ["SQL_Latin1_General_CP1_CS_AS", "SQL_Latin1_General_CP1_CI_AS"]
19+
python_version: ["3.11", "3.12", "3.13"]
20+
msodbc_version: ["18"]
21+
sqlserver_version: ["2017", "2022"]
22+
collation: ["SQL_Latin1_General_CP1_CS_AS"]
2523
runs-on: ubuntu-latest
2624
container:
2725
image: ghcr.io/${{ github.repository }}:CI-${{ matrix.python_version }}-msodbc${{ matrix.msodbc_version }}

.github/workflows/publish-docker.yml renamed to .github/workflows/patched-publish-docker.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
---
2-
name: Publish Docker images for CI/CD
2+
name: Patched Publish Docker images for CI/CD
33
on: # yamllint disable-line rule:truthy
44
push:
55
paths:
66
- 'devops/**'
77
- '.github/workflows/publish-docker.yml'
88
branches:
9-
- 'master'
9+
- 'patched'
1010

1111
jobs:
12-
publish-docker-client:
12+
patched-publish-docker-client:
1313
strategy:
1414
matrix:
15-
python_version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
16-
docker_target: ["msodbc17", "msodbc18"]
15+
python_version: ["3.11", "3.12", "3.13"]
16+
docker_target: ["msodbc18"]
1717
runs-on: ubuntu-latest
1818
permissions:
1919
contents: read
@@ -40,10 +40,10 @@ jobs:
4040
target: ${{ matrix.docker_target }}
4141
tags: ghcr.io/${{ github.repository }}:CI-${{ matrix.python_version }}-${{ matrix.docker_target }}
4242

43-
publish-docker-server:
43+
patched-publish-docker-server:
4444
strategy:
4545
matrix:
46-
mssql_version: ["2017", "2019", "2022"]
46+
mssql_version: ["2017", "2022"]
4747
runs-on: ubuntu-latest
4848
permissions:
4949
contents: read

.github/workflows/unit-tests.yml renamed to .github/workflows/patched-unit-tests-patched.yml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
---
2-
name: Unit tests
2+
name: Patched Unit tests
33
on: # yamllint disable-line rule:truthy
44
workflow_dispatch:
55
push:
66
branches:
7-
- master
7+
- patched
88
- v*
99
pull_request:
1010
branches:
11-
- master
11+
- patched
1212
- v*
13-
schedule:
14-
- cron: '0 22 * * 0'
1513

1614
jobs:
17-
unit-tests:
15+
patched-unit-tests:
1816
name: Unit tests
1917
strategy:
2018
matrix:
21-
python_version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
19+
python_version: ["3.11", "3.12", "3.13"]
2220
runs-on: ubuntu-latest
2321
permissions:
2422
contents: read
@@ -27,7 +25,7 @@ jobs:
2725
image: ghcr.io/${{ github.repository }}:CI-${{ matrix.python_version }}-msodbc18
2826
credentials:
2927
username: ${{ github.actor }}
30-
password: ${{ secrets.github_token }}
28+
password: ${{ secrets.GITHUB_TOKEN }}
3129
steps:
3230

3331
- uses: actions/checkout@v4

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
default_language_version:
2-
python: python3.10
2+
python: python3
33
repos:
44
- repo: 'https://github.com/pre-commit/pre-commit-hooks'
55
rev: v4.6.0

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"tests"
44
],
55
"python.testing.unittestEnabled": false,
6-
"python.testing.pytestEnabled": true
6+
"python.testing.pytestEnabled": true,
7+
"python-envs.defaultEnvManager": "ms-python.python:venv"
78
}

0 commit comments

Comments
 (0)