diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 964db68..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.46 + pip install mysqlclient psycopg2-binary SQLAlchemy - name: Run tests run: python tests/sql.py diff --git a/setup.py b/setup.py index d9a2ee4..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", @@ -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 8d07327..de3ad56 100644 --- a/src/cs50/sql.py +++ b/src/cs50/sql.py @@ -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