Skip to content

Commit

Permalink
Merge pull request #174 from cs50/SQLAlchemy-2.0
Browse files Browse the repository at this point in the history
Support SQLAlchemy 2.0
  • Loading branch information
rongxin-liu authored Oct 24, 2023
2 parents b3f0a0c + 3ddef31 commit fcc8cb9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Setup databases
run: |
pip install .
pip install mysqlclient psycopg2-binary SQLAlchemy==1.4.46
pip install mysqlclient psycopg2-binary SQLAlchemy
- name: Run tests
run: python tests/sql.py
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
"Topic :: Software Development :: Libraries :: Python Modules"
],
description="CS50 library for Python",
install_requires=["Flask>=1.0", "packaging", "SQLAlchemy==1.4.46", "sqlparse", "termcolor", "wheel"],
install_requires=["Flask>=1.0", "packaging", "SQLAlchemy<3", "sqlparse", "termcolor", "wheel"],
keywords="cs50",
license="GPLv3",
long_description_content_type="text/markdown",
name="cs50",
package_dir={"": "src"},
packages=["cs50"],
url="https://github.com/cs50/python-cs50",
version="9.2.7"
version="9.3.0"
)
12 changes: 6 additions & 6 deletions src/cs50/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def connect(dbapi_connection, connection_record):
self._logger.disabled = True
try:
connection = self._engine.connect()
connection.execute("SELECT 1")
connection.execute(sqlalchemy.text("SELECT 1"))
connection.close()
except sqlalchemy.exc.OperationalError as e:
e = RuntimeError(_parse_exception(e))
Expand Down Expand Up @@ -153,10 +153,10 @@ def execute(self, sql, *args, **kwargs):
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])
full_statement = full_statement.upper()

# set of possible commands
# Set of possible commands
commands = {"BEGIN", "CREATE VIEW", "DELETE", "INSERT", "SELECT", "START", "UPDATE"}

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

# Flatten statement
Expand Down Expand Up @@ -344,7 +344,7 @@ def teardown_appcontext(exception):
if command == "SELECT":

# Coerce types
rows = [dict(row) for row in result.fetchall()]
rows = [dict(row) for row in result.mappings().all()]
for row in rows:
for column in row:

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

# If not PostgreSQL
Expand Down

0 comments on commit fcc8cb9

Please sign in to comment.