Skip to content

Commit efc51a4

Browse files
authored
fix: use pytds driver in SQL Server sample (GoogleCloudPlatform#5292)
* fix: use pytds driver in SQL Server sample * fix tests * add license header to test file * add pyopenssl to requirements.txt * remove query string
1 parent fa3c5ab commit efc51a4

File tree

5 files changed

+23
-13
lines changed

5 files changed

+23
-13
lines changed

cloud-sql/sql-server/sqlalchemy/Dockerfile

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616
# https://hub.docker.com/_/python
1717
FROM python:3.9-buster
1818

19-
#Download the desired package(s) for Microsoft ODBC 17 Driver
20-
RUN (curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -) && \
21-
curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/mssql-release.list && \
22-
apt-get update && ACCEPT_EULA=Y apt-get install -y msodbcsql17 unixodbc-dev
23-
2419
# Copy application dependency manifests to the container image.
2520
# Copying this separately prevents re-running pip install on every code change.
2621
COPY requirements.txt ./

cloud-sql/sql-server/sqlalchemy/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
3. Click **CREATE**.
2727

28-
1. Install the version of [Microsoft ODBC 17 Driver for SQL Server](https://docs.microsoft.com/en-us/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-ver15_) for your operating system.
28+
2929

3030
1. Create a service account with the 'Cloud SQL Client' permissions by following these
3131
[instructions](https://cloud.google.com/sql/docs/postgres/connect-external-app#4_if_required_by_your_authentication_method_create_a_service_account).

cloud-sql/sql-server/sqlalchemy/connection_test.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
114
from contextlib import contextmanager
215
import logging
316
import os
417
from typing import Dict
518

6-
import pyodbc
19+
import pytds
720
import pytest
821

922
import main
@@ -45,7 +58,7 @@ def tcp_db_connection():
4558
def _common_setup():
4659
try:
4760
pool = main.init_connection_engine()
48-
except pyodbc.OperationalError as e:
61+
except pytds.OperationalError as e:
4962
logger.warning(
5063
'Could not connect to the production database. '
5164
'If running tests locally, is the cloud_sql_proxy currently running?'

cloud-sql/sql-server/sqlalchemy/main.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,14 @@ def init_tcp_connection_engine(db_config):
9292
# managing a pool of connections to your database
9393
pool = sqlalchemy.create_engine(
9494
# Equivalent URL:
95-
# mssql+pyodbc://<db_user>:<db_pass>@/<host>:<port>/<db_name>?driver=ODBC+Driver+17+for+SQL+Server
95+
# mssql+pytds://<db_user>:<db_pass>@/<host>:<port>/<db_name>?driver=ODBC+Driver+17+for+SQL+Server
9696
sqlalchemy.engine.url.URL(
97-
"mssql+pyodbc",
97+
"mssql+pytds",
9898
username=db_user,
9999
password=db_pass,
100100
database=db_name,
101101
host=db_hostname,
102102
port=db_port,
103-
query={"driver": "ODBC Driver 17 for SQL Server"},
104103
),
105104
**db_config
106105
)
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
Flask==1.1.2
2-
SQLAlchemy==1.3.23
3-
pyodbc==4.0.30
2+
SQLAlchemy==1.3.22
3+
python-tds==1.10.0
4+
sqlalchemy-pytds==0.3.1
5+
pyopenssl==20.0.1
6+
47

0 commit comments

Comments
 (0)