Skip to content

Commit c2d12ee

Browse files
author
Kareem Zidane
committed
Support postgres:// scheme
1 parent a88fa09 commit c2d12ee

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,14 @@ jobs:
2626
python-version: '3.6'
2727
- name: Setup databases
2828
run: |
29-
python setup.py install
30-
pip install mysqlclient
31-
pip install psycopg2-binary
32-
touch test.db test1.db
29+
pip install .
30+
pip install mysqlclient psycopg2-binary
3331
- name: Run tests
3432
run: python tests/sql.py
3533
- name: Install pypa/build
36-
run: |
37-
python -m pip install build --user
34+
run: python -m pip install build --user
3835
- name: Build a binary wheel and a source tarball
39-
run: |
40-
python -m build --sdist --wheel --outdir dist/ .
36+
run: python -m build --sdist --wheel --outdir dist/ .
4137
- name: Deploy to PyPI
4238
if: ${{ github.ref == 'refs/heads/main' }}
4339
uses: pypa/gh-action-pypi-publish@release/v1

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
*.db
66
*.egg-info/
77
*.pyc
8+
build/
89
dist/
910
test.db

src/cs50/_engine.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import threading
2+
import warnings
23

34
from ._engine_util import create_engine
45

@@ -11,6 +12,7 @@ class Engine:
1112
"""
1213

1314
def __init__(self, url):
15+
url = _replace_scheme_if_postgres(url)
1416
self._engine = create_engine(url)
1517

1618
def get_transaction_connection(self):
@@ -64,3 +66,23 @@ def _thread_local_connections():
6466
connections = thread_local_data.connections = {}
6567

6668
return connections
69+
70+
def _replace_scheme_if_postgres(url):
71+
"""
72+
Replaces the postgres scheme with the postgresql scheme if possible since the postgres scheme
73+
is deprecated.
74+
75+
:returns: url with postgresql scheme if the scheme was postgres; otherwise returns url as is
76+
"""
77+
78+
if url.startswith("postgres://"):
79+
with warnings.catch_warnings():
80+
warnings.simplefilter("always")
81+
warnings.warn(
82+
"The postgres:// scheme is deprecated and will not be supported in the next major"
83+
+ " release of the library. Please use the postgresql:// scheme instead.",
84+
DeprecationWarning
85+
)
86+
url = f"postgresql{url[len('postgres'):]}"
87+
88+
return url

tests/sql.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ def setUp(self):
169169
def test_cte(self):
170170
self.assertEqual(self.db.execute("WITH foo AS ( SELECT 1 AS bar ) SELECT bar FROM foo"), [{"bar": 1}])
171171

172+
def test_postgres_scheme(self):
173+
db = SQL("postgres://postgres:[email protected]/test")
174+
db.execute("SELECT 1")
172175

173176
class SQLiteTests(SQLTests):
174177

0 commit comments

Comments
 (0)