From f807f1ef8b50b72d9307c0130828af41b99f517d Mon Sep 17 00:00:00 2001 From: Rongxin Liu Date: Wed, 13 Sep 2023 11:10:54 -0400 Subject: [PATCH 1/6] added support for SQLAlchemy 2.0 --- setup.py | 4 ++-- src/cs50/sql.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/setup.py b/setup.py index 1a8ef3a..2c25e53 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ "Topic :: Software Development :: Libraries :: Python Modules" ], description="CS50 library for Python", - install_requires=["Flask>=1.0", "SQLAlchemy==1.4.46", "sqlparse", "termcolor", "wheel"], + install_requires=["Flask>=1.0", "SQLAlchemy<3", "sqlparse", "termcolor", "wheel"], keywords="cs50", license="GPLv3", long_description_content_type="text/markdown", @@ -18,5 +18,5 @@ package_dir={"": "src"}, packages=["cs50"], url="https://github.com/cs50/python-cs50", - version="9.2.5" + version="9.2.6" ) diff --git a/src/cs50/sql.py b/src/cs50/sql.py index 24690e3..8110cba 100644 --- a/src/cs50/sql.py +++ b/src/cs50/sql.py @@ -53,7 +53,7 @@ def __init__(self, url, **kwargs): import sqlalchemy import sqlalchemy.orm import threading - + # Temporary fix for missing sqlite3 module on the buildpack stack try: import sqlite3 @@ -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)) @@ -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: From ca09441d54ab52805baa38d50fe47c33bf4dcc65 Mon Sep 17 00:00:00 2001 From: Rongxin Liu Date: Wed, 13 Sep 2023 11:17:39 -0400 Subject: [PATCH 2/6] convert string to sqlalchemy text --- src/cs50/sql.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cs50/sql.py b/src/cs50/sql.py index 8110cba..527a98d 100644 --- a/src/cs50/sql.py +++ b/src/cs50/sql.py @@ -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 From 2ed4803dbe6f29f0b10ddb1ef5b173d991d43805 Mon Sep 17 00:00:00 2001 From: Rongxin Liu Date: Thu, 14 Sep 2023 23:28:20 -0400 Subject: [PATCH 3/6] bump SQLAlchemy version to 1.4.49 in workflow --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 964db68..6547cf2 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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==1.4.49 - name: Run tests run: python tests/sql.py From 0608340a51d5fc60742b4a94ab3f9b45e7d45f2a Mon Sep 17 00:00:00 2001 From: Rongxin Liu Date: Wed, 4 Oct 2023 11:29:20 -0700 Subject: [PATCH 4/6] unpin SQLAlchemy version in workflow --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6547cf2..b8165f7 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -28,7 +28,7 @@ jobs: - name: Setup databases run: | pip install . - pip install mysqlclient psycopg2-binary SQLAlchemy==1.4.49 + pip install mysqlclient psycopg2-binary SQLAlchemy - name: Run tests run: python tests/sql.py From bb7263454280a73077ee1657956d030f6fd60f31 Mon Sep 17 00:00:00 2001 From: Rongxin Liu Date: Tue, 24 Oct 2023 09:36:58 -0400 Subject: [PATCH 5/6] bumped version number, fixed comment styles --- setup.py | 2 +- src/cs50/sql.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index d9a2ee4..8c1abfb 100644 --- a/setup.py +++ b/setup.py @@ -18,5 +18,5 @@ package_dir={"": "src"}, packages=["cs50"], url="https://github.com/cs50/python-cs50", - version="9.2.7" + version="9.3.0" ) diff --git a/src/cs50/sql.py b/src/cs50/sql.py index fee0d68..de3ad56 100644 --- a/src/cs50/sql.py +++ b/src/cs50/sql.py @@ -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 From 3ddef3132a2485d48ea487bb9401317921ebfc82 Mon Sep 17 00:00:00 2001 From: Rongxin Liu Date: Tue, 24 Oct 2023 09:39:36 -0400 Subject: [PATCH 6/6] updated SQLAlchemy version constraint --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 8c1abfb..23f6b01 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ "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",