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

Pre-commit setup #228

Merged
merged 6 commits into from
May 22, 2022
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: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
pip-dependency-file: dev_requirements.txt
- run: tox -- -v test/unit
integration-sqlserver: &sqlserver
docker: &msodbc_py_&_sqlserver
docker:
- image: *docker_image
- image: mcr.microsoft.com/mssql/server:2019-latest
environment:
Expand Down Expand Up @@ -47,7 +47,7 @@ jobs:
- run:
name: wait for SQL Server container to set up
command: sleep 30
- run: &prep=connect
- run: &prep-connect
name: prep for connecting
command: |
mkdir -p ~/.dbt
Expand Down Expand Up @@ -79,7 +79,7 @@ jobs:
- python/install-packages: *install_dev
- run: *install-dbt-sqlserver
- azure-cli/install
- run: *prep=connect
- run: *prep-connect
- azure-cli/login-with-service-principal:
azure-sp: DBT_AZURE_SP_NAME
azure-sp-password: DBT_AZURE_SP_SECRET
Expand Down
16 changes: 11 additions & 5 deletions .circleci/wakeup_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@
async def resume_azsql() -> bool:
try:
client = SqlManagementClient(credential=credential, subscription_id=subscription_id)
db = await client.databases.get(resource_group_name=resource_group_name, server_name=sql_server_name,
database_name=database_name)
db = await client.databases.get(
resource_group_name=resource_group_name,
server_name=sql_server_name,
database_name=database_name,
)
if db.status == "Paused":
res = await client.databases.begin_resume(resource_group_name=resource_group_name,
server_name=sql_server_name, database_name=database_name)
res = await client.databases.begin_resume(
resource_group_name=resource_group_name,
server_name=sql_server_name,
database_name=database_name,
)
print("Resuming SQL Database")
await res.wait()
elif db.status in ("Pausing", "Resuming"):
Expand All @@ -40,7 +46,7 @@ async def main():
await main()


if __name__ == '__main__':
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
2 changes: 1 addition & 1 deletion .github/workflows/publish-docker.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: Publish Docker image for CI/CD
on:
on: # yamllint disable-line rule:truthy
push:
tags:
- 'docker-*'
Expand Down
96 changes: 96 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
default_language_version:
python: python3.9
repos:
- repo: 'https://github.com/pre-commit/pre-commit-hooks'
rev: v4.2.0
hooks:
- id: check-yaml
args:
- '--unsafe'
- id: check-json
- id: end-of-file-fixer
- id: trailing-whitespace
exclude_types:
- markdown
- id: check-case-conflict
- id: check-ast
- id: check-builtin-literals
- id: check-merge-conflict
- id: no-commit-to-branch
- id: fix-byte-order-marker
- id: mixed-line-ending
- id: check-docstring-first
- repo: 'https://github.com/adrienverge/yamllint'
rev: v1.26.3
hooks:
- id: yamllint
args:
- '-d {extends: default, rules: {line-length: disable, document-start: disable}}'
- '-s'
- repo: 'https://github.com/MarcoGorelli/absolufy-imports'
rev: v0.3.1
hooks:
- id: absolufy-imports
- repo: 'https://github.com/hadialqattan/pycln'
rev: v1.2.5
hooks:
- id: pycln
args:
- '--all'
- repo: 'https://github.com/pycqa/isort'
rev: 5.10.1
hooks:
- id: isort
args:
- '--profile'
- black
- '--atomic'
- '--line-length'
- '99'
- '--python-version'
- '39'
- repo: 'https://github.com/psf/black'
rev: 22.3.0
hooks:
- id: black
args:
- '--line-length=99'
- '--target-version=py39'
- id: black
alias: black-check
stages:
- manual
args:
- '--line-length=99'
- '--target-version=py39'
- '--check'
- '--diff'
- repo: 'https://gitlab.com/pycqa/flake8'
rev: 3.9.2
hooks:
- id: flake8
args:
- '--max-line-length=99'
- id: flake8
args:
- '--max-line-length=99'
alias: flake8-check
stages:
- manual
- repo: 'https://github.com/pre-commit/mirrors-mypy'
rev: v0.950
hooks:
- id: mypy
args:
- '--show-error-codes'
- '--ignore-missing-imports'
files: '^dbt/adapters'
- id: mypy
alias: mypy-check
stages:
- manual
args:
- '--show-error-codes'
- '--pretty'
- '--ignore-missing-imports'
files: '^dbt/adapters'
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ pyenv virtualenv 3.9.12 dbt-sqlserver
pyenv activate dbt-sqlserver
```

Install the development dependencies:
Install the development dependencies and pre-commit and get information about possible make commands:

```shell
pip install -r devrequirements.txt
pip install -e .
make dev
make help
```

## Testing
Expand Down
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1 @@
recursive-include dbt/include *.sql *.yml *.md
recursive-include dbt/include *.sql *.yml *.md
56 changes: 56 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
.DEFAULT_GOAL:=help

.PHONY: dev
dev: ## Installs adapter in develop mode along with development depedencies
@\
pip install -r dev_requirements.txt && pre-commit install

.PHONY: mypy
mypy: ## Runs mypy against staged changes for static type checking.
@\
pre-commit run --hook-stage manual mypy-check | grep -v "INFO"

.PHONY: flake8
flake8: ## Runs flake8 against staged changes to enforce style guide.
@\
pre-commit run --hook-stage manual flake8-check | grep -v "INFO"

.PHONY: black
black: ## Runs black against staged changes to enforce style guide.
@\
pre-commit run --hook-stage manual black-check -v | grep -v "INFO"

.PHONY: lint
lint: ## Runs flake8 and mypy code checks against staged changes.
@\
pre-commit run flake8-check --hook-stage manual | grep -v "INFO"; \
pre-commit run mypy-check --hook-stage manual | grep -v "INFO"

.PHONY: linecheck
linecheck: ## Checks for all Python lines 100 characters or more
@\
find dbt -type f -name "*.py" -exec grep -I -r -n '.\{100\}' {} \;

.PHONY: unit
unit: ## Runs unit tests.
@\
tox -- -v test/unit

.PHONY: test
test: ## Runs unit tests and code checks against staged changes.
@\
tox -- -v test/unit; \
pre-commit run black-check --hook-stage manual | grep -v "INFO"; \
pre-commit run flake8-check --hook-stage manual | grep -v "INFO"; \
pre-commit run mypy-check --hook-stage manual | grep -v "INFO"

.PHONY: clean
@echo "cleaning repo"
@git clean -f -X

.PHONY: help
help: ## Show this help message.
@echo 'usage: make [target]'
@echo
@echo 'targets:'
@grep -E '^[7+a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
Empty file removed __init__.py
Empty file.
2 changes: 1 addition & 1 deletion dbt/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
2 changes: 1 addition & 1 deletion dbt/adapters/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
10 changes: 5 additions & 5 deletions dbt/adapters/sqlserver/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from dbt.adapters.sqlserver.connections import SQLServerConnectionManager
from dbt.adapters.sqlserver.connections import SQLServerCredentials
from dbt.adapters.sqlserver.impl import SQLServerAdapter

from dbt.adapters.base import AdapterPlugin
from dbt.include import sqlserver

from dbt.adapters.sqlserver.connections import SQLServerConnectionManager, SQLServerCredentials
from dbt.adapters.sqlserver.impl import SQLServerAdapter
from dbt.include import sqlserver

Plugin = AdapterPlugin(
adapter=SQLServerAdapter,
credentials=SQLServerCredentials,
include_path=sqlserver.PACKAGE_PATH,
)

__all__ = ["Plugin", "SQLServerConnectionManager", "SQLServerAdapter", "SQLServerCredentials"]
2 changes: 1 addition & 1 deletion dbt/adapters/sqlserver/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = '1.0.0.dev0'
version = "1.0.0.dev0"
24 changes: 11 additions & 13 deletions dbt/adapters/sqlserver/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
from contextlib import contextmanager
from dataclasses import dataclass
from itertools import chain, repeat
from typing import Callable, Dict, Mapping
from typing import Optional
from typing import Callable, Dict, Mapping, Optional

import dbt.exceptions
import pyodbc
from azure.core.credentials import AccessToken
from azure.identity import (
AzureCliCredential,
ManagedIdentityCredential,
ClientSecretCredential,
DefaultAzureCredential,
EnvironmentCredential,
ManagedIdentityCredential,
)
from dbt.adapters.base import Credentials
from dbt.adapters.sql import SQLConnectionManager
Expand Down Expand Up @@ -216,7 +215,7 @@ def get_sp_access_token(credentials: SQLServerCredentials) -> AccessToken:
The access token.
"""
token = ClientSecretCredential(
credentials.tenant_id, credentials.client_id, credentials.client_secret
str(credentials.tenant_id), str(credentials.client_id), str(credentials.client_secret)
).get_token(AZURE_CREDENTIAL_SCOPE)
return token

Expand Down Expand Up @@ -253,11 +252,9 @@ def get_pyodbc_attrs_before(credentials: SQLServerCredentials) -> Dict:
"environment": get_environment_access_token,
}

authentication = credentials.authentication.lower()
authentication = str(credentials.authentication).lower()
if authentication in azure_auth_functions:
time_remaining = (
(_TOKEN.expires_on - time.time()) if _TOKEN else MAX_REMAINING_TIME
)
time_remaining = (_TOKEN.expires_on - time.time()) if _TOKEN else MAX_REMAINING_TIME

if _TOKEN is None or (time_remaining < MAX_REMAINING_TIME):
azure_auth_function = azure_auth_functions[authentication]
Expand Down Expand Up @@ -318,8 +315,9 @@ def open(cls, connection):
con_str.append(f"DRIVER={{{credentials.driver}}}")

if "\\" in credentials.host:
# if there is a backslash \ in the host name the host is a sql-server named instance
# in this case then port number has to be omitted

# If there is a backslash \ in the host name, the host is a
# SQL Server named instance. In this case then port number has to be omitted.
con_str.append(f"SERVER={credentials.host}")
else:
con_str.append(f"SERVER={credentials.host},{credentials.port}")
Expand All @@ -338,7 +336,7 @@ def open(cls, connection):
con_str.append(f"UID={{{credentials.UID}}}")

elif getattr(credentials, "windows_login", False):
con_str.append(f"trusted_connection=yes")
con_str.append("trusted_connection=yes")
elif type_auth == "sql":
con_str.append(f"UID={{{credentials.UID}}}")
con_str.append(f"PWD={{{credentials.PWD}}}")
Expand All @@ -347,9 +345,9 @@ def open(cls, connection):
# to learn more visit
# https://docs.microsoft.com/en-us/sql/relational-databases/native-client/features/using-encryption-without-validation?view=sql-server-ver15
if getattr(credentials, "encrypt", False) is True:
con_str.append(f"Encrypt=Yes")
con_str.append("Encrypt=Yes")
if getattr(credentials, "trust_cert", False) is True:
con_str.append(f"TrustServerCertificate=Yes")
con_str.append("TrustServerCertificate=Yes")

plugin_version = __version__.version
application_name = f"dbt-{credentials.type}/{plugin_version}"
Expand Down
Loading