Skip to content

Commit fcc8cb9

Browse files
authored
Merge pull request #174 from cs50/SQLAlchemy-2.0
Support SQLAlchemy 2.0
2 parents b3f0a0c + 3ddef31 commit fcc8cb9

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
- name: Setup databases
2929
run: |
3030
pip install .
31-
pip install mysqlclient psycopg2-binary SQLAlchemy==1.4.46
31+
pip install mysqlclient psycopg2-binary SQLAlchemy
3232
3333
- name: Run tests
3434
run: python tests/sql.py

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
"Topic :: Software Development :: Libraries :: Python Modules"
1111
],
1212
description="CS50 library for Python",
13-
install_requires=["Flask>=1.0", "packaging", "SQLAlchemy==1.4.46", "sqlparse", "termcolor", "wheel"],
13+
install_requires=["Flask>=1.0", "packaging", "SQLAlchemy<3", "sqlparse", "termcolor", "wheel"],
1414
keywords="cs50",
1515
license="GPLv3",
1616
long_description_content_type="text/markdown",
1717
name="cs50",
1818
package_dir={"": "src"},
1919
packages=["cs50"],
2020
url="https://github.com/cs50/python-cs50",
21-
version="9.2.7"
21+
version="9.3.0"
2222
)

src/cs50/sql.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def connect(dbapi_connection, connection_record):
100100
self._logger.disabled = True
101101
try:
102102
connection = self._engine.connect()
103-
connection.execute("SELECT 1")
103+
connection.execute(sqlalchemy.text("SELECT 1"))
104104
connection.close()
105105
except sqlalchemy.exc.OperationalError as e:
106106
e = RuntimeError(_parse_exception(e))
@@ -153,10 +153,10 @@ def execute(self, sql, *args, **kwargs):
153153
full_statement = ' '.join(str(token) for token in statements[0].tokens if token.ttype in [sqlparse.tokens.Keyword, sqlparse.tokens.Keyword.DDL, sqlparse.tokens.Keyword.DML])
154154
full_statement = full_statement.upper()
155155

156-
# set of possible commands
156+
# Set of possible commands
157157
commands = {"BEGIN", "CREATE VIEW", "DELETE", "INSERT", "SELECT", "START", "UPDATE"}
158158

159-
# check if the full_statement starts with any command
159+
# Check if the full_statement starts with any command
160160
command = next((cmd for cmd in commands if full_statement.startswith(cmd)), None)
161161

162162
# Flatten statement
@@ -344,7 +344,7 @@ def teardown_appcontext(exception):
344344
if command == "SELECT":
345345

346346
# Coerce types
347-
rows = [dict(row) for row in result.fetchall()]
347+
rows = [dict(row) for row in result.mappings().all()]
348348
for row in rows:
349349
for column in row:
350350

@@ -370,7 +370,7 @@ def teardown_appcontext(exception):
370370
# "(psycopg2.errors.ObjectNotInPrerequisiteState) lastval is not yet defined in this session",
371371
# a la https://stackoverflow.com/a/24186770/5156190;
372372
# cf. https://www.psycopg.org/docs/errors.html re 55000
373-
result = connection.execute("""
373+
result = connection.execute(sqlalchemy.text("""
374374
CREATE OR REPLACE FUNCTION _LASTVAL()
375375
RETURNS integer LANGUAGE plpgsql
376376
AS $$
@@ -382,7 +382,7 @@ def teardown_appcontext(exception):
382382
END;
383383
END $$;
384384
SELECT _LASTVAL();
385-
""")
385+
"""))
386386
ret = result.first()[0]
387387

388388
# If not PostgreSQL

0 commit comments

Comments
 (0)