Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support SQLAlchemy 2.0 #174

Merged
merged 7 commits into from
Oct 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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
@@ -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))
@@ -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
@@ -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:

@@ -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 $$
@@ -382,7 +382,7 @@ def teardown_appcontext(exception):
END;
END $$;
SELECT _LASTVAL();
""")
"""))
ret = result.first()[0]

# If not PostgreSQL