Skip to content

Commit

Permalink
pre-commit setup
Browse files Browse the repository at this point in the history
  • Loading branch information
sdebruyn committed May 21, 2022
1 parent 3389cab commit 23d70bd
Show file tree
Hide file tree
Showing 32 changed files with 184 additions and 95 deletions.
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
92 changes: 92 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
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'
- id: mypy
alias: mypy-check
stages:
- manual
args:
- '--show-error-codes'
- '--pretty'
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ pyenv virtualenv 3.9.12 dbt-sqlserver
pyenv activate dbt-sqlserver
```

Install the development dependencies:
Install the development dependencies and pre-commit:

```shell
pip install -r devrequirements.txt
pip install -e .
pre-commit install
```

## 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
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__)
6 changes: 2 additions & 4 deletions dbt/adapters/sqlserver/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from dbt.adapters.sqlserver.connections import SQLServerConnectionManager
from dbt.adapters.base import AdapterPlugin

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


Plugin = AdapterPlugin(
adapter=SQLServerAdapter,
credentials=SQLServerCredentials,
Expand Down
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"
20 changes: 9 additions & 11 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 @@ -255,9 +254,7 @@ def get_pyodbc_attrs_before(credentials: SQLServerCredentials) -> Dict:

authentication = 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
28 changes: 13 additions & 15 deletions dbt/adapters/sqlserver/impl.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from typing import List, Optional

import agate
from dbt.adapters.base.relation import BaseRelation
from dbt.adapters.sql import SQLAdapter

from dbt.adapters.sqlserver import SQLServerConnectionManager
from dbt.adapters.base.relation import BaseRelation
import agate
from typing import (
Optional, Tuple, Callable, Iterable, Type, Dict, Any, List, Mapping,
Iterator, Union, Set
)


class SQLServerAdapter(SQLAdapter):
Expand Down Expand Up @@ -42,9 +41,7 @@ def convert_time_type(cls, agate_table, col_idx):
return "datetime"

# Methods used in adapter tests
def timestamp_add_sql(
self, add_to: str, number: int = 1, interval: str = "hour"
) -> str:
def timestamp_add_sql(self, add_to: str, number: int = 1, interval: str = "hour") -> str:
# note: 'interval' is not supported for T-SQL
# for backwards compatibility, we're compelled to set some sort of
# default. A lot of searching has lead me to believe that the
Expand All @@ -53,19 +50,20 @@ def timestamp_add_sql(
return f"DATEADD({interval},{number},{add_to})"

def string_add_sql(
self, add_to: str, value: str, location='append',
self,
add_to: str,
value: str,
location="append",
) -> str:
"""
`+` is T-SQL's string concatenation operator
"""
if location == 'append':
if location == "append":
return f"{add_to} + '{value}'"
elif location == 'prepend':
elif location == "prepend":
return f"'{value}' + {add_to}"
else:
raise RuntimeException(
f'Got an unexpected location value of "{location}"'
)
raise ValueError(f'Got an unexpected location value of "{location}"')

def get_rows_different_sql(
self,
Expand Down
2 changes: 1 addition & 1 deletion dbt/include/__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__)
1 change: 0 additions & 1 deletion dbt/include/sqlserver/dbt_project.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

name: dbt_sqlserver
version: 1.0

Expand Down
2 changes: 1 addition & 1 deletion dbt/include/sqlserver/macros/adapters/columns.sql
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@
{%- endcall -%}
{% endmacro %}
{% endmacro %}
2 changes: 1 addition & 1 deletion dbt/include/sqlserver/macros/adapters/freshness.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{% macro sqlserver__current_timestamp() -%}
SYSDATETIME()
{%- endmacro %}
{%- endmacro %}
12 changes: 6 additions & 6 deletions dbt/include/sqlserver/macros/adapters/indexes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ select @drop_remaining_indexes_last = (

{% set idx_name = this.table + '__clustered_index_on_' + columns|join('_') %}

if not exists(select * from sys.indexes
where
name = '{{ idx_name }}' and
if not exists(select * from sys.indexes
where
name = '{{ idx_name }}' and
object_id = OBJECT_ID('{{ this }}')
)
begin
Expand All @@ -156,9 +156,9 @@ end

{% set idx_name = this.table + '__index_on_' + columns|join('_')|replace(" ", "_") %}

if not exists(select * from sys.indexes
where
name = '{{ idx_name }}' and
if not exists(select * from sys.indexes
where
name = '{{ idx_name }}' and
object_id = OBJECT_ID('{{ this }}')
)
begin
Expand Down
2 changes: 1 addition & 1 deletion dbt/include/sqlserver/macros/adapters/metadata.sql
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@
where table_schema like '{{ schema_relation.schema }}'
{% endcall %}
{{ return(load_result('list_relations_without_caching').table) }}
{% endmacro %}
{% endmacro %}
2 changes: 1 addition & 1 deletion dbt/include/sqlserver/macros/adapters/persist_docs.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{# we don't support "persist docs" today, but we'd like to!
https://github.com/dbt-msft/dbt-sqlserver/issues/134

#}
#}
2 changes: 1 addition & 1 deletion dbt/include/sqlserver/macros/adapters/relation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@
WHERE name='{{ from_relation.schema }}_{{ from_relation.identifier }}_cci' and object_id = OBJECT_ID('{{ from_relation.schema }}.{{ to_relation.identifier }}'))
EXEC sp_rename N'{{ from_relation.schema }}.{{ to_relation.identifier }}.{{ from_relation.schema }}_{{ from_relation.identifier }}_cci', N'{{ from_relation.schema }}_{{ to_relation.identifier }}_cci', N'INDEX'
{%- endcall %}
{% endmacro %}
{% endmacro %}
2 changes: 1 addition & 1 deletion dbt/include/sqlserver/macros/adapters/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
{% endmacro %}


{# there is no drop_schema... why? #}
{# there is no drop_schema... why? #}
Loading

0 comments on commit 23d70bd

Please sign in to comment.