Skip to content
New issue

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

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

Already on GitHub? # to your account

Upgrade SQLAlchemy to 2.0 #8452

Merged
merged 8 commits into from
Jan 31, 2024
Merged

Upgrade SQLAlchemy to 2.0 #8452

merged 8 commits into from
Jan 31, 2024

Conversation

marcospri
Copy link
Member

@marcospri marcospri commented Jan 23, 2024

For #7973

Base automatically changed from remove-reindex to main January 23, 2024 13:47
@marcospri marcospri changed the title Upgrade sql Upgrade SQLAlchemy to 2.0 Jan 23, 2024
@@ -9,12 +9,12 @@ class Timestamps:
created = sa.Column(
sa.DateTime,
default=datetime.datetime.utcnow,
server_default=sa.func.now(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annoying pylint disables all around for these.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -33,7 +33,7 @@ def group_search(
"""

query = (
sa.select([Group.authority_provided_id])
sa.select(Group.authority_provided_id)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the args, not list style of call

"annotation_id", Annotation.id, "force", bool(force)
),
]
literal_column(f"'{name}'"),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No list here either

UserIdentity(
user=user,
provider=i_args["provider"],
provider_unique_id=str(i_args["provider_unique_id"]),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a bit more context on the commit message for this but we need this type cast for SQLA to produce the right SQL.

transaction.nested and not transaction._parent.nested
):
session.begin_nested()
session = db_sessionfactory(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 2.0 version is much simpler.

@@ -293,6 +293,7 @@ def test_if_the_annotation_isnt_in_the_DB_it_deletes_the_job_from_the_queue(
job = factories.SyncAnnotationJob(annotation=annotation)
queue_service.get.return_value = [job]
db_session.delete(annotation)
db_session.commit()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to commit for the object to be actually deleted and the test to see it gone.

def mock_transaction(db_session):
transaction = mock.Mock(spec=db_session.transaction)
def mock_transaction():
transaction = mock.Mock(spec=SessionTransaction)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the class for the spec info. It's no longer accessible via the session.

@marcospri marcospri requested a review from seanh January 23, 2024 15:10
@marcospri marcospri marked this pull request as draft January 23, 2024 15:32
@marcospri marcospri marked this pull request as ready for review January 24, 2024 10:10
@seanh
Copy link
Contributor

seanh commented Jan 30, 2024

Oh, we should delete this now:

h/tests/conftest.py

Lines 5 to 35 in 534cca8

@pytest.fixture
def db_session(db_engine, db_sessionfactory):
"""
Return the SQLAlchemy database session.
h overrides the db_session fixture from h-testkit because h still uses
SQLAlchemy 1.4 so it has to use the older, 1.4 version of the SQLAlchemy
test suite technique:
https://docs.sqlalchemy.org/en/14/orm/session_transaction.html#joining-a-session-into-an-external-transaction-such-as-for-test-suites
When h is upgraded to SQLAlchemy 2 this fixture can be removed and it can
just use the one from h-testkit.
"""
connection = db_engine.connect()
transaction = connection.begin()
session = db_sessionfactory(bind=connection)
session.begin_nested()
@event.listens_for(session, "after_transaction_end")
def restart_savepoint(session, transaction):
if ( # pylint:disable=protected-access
transaction.nested and not transaction._parent.nested
):
session.begin_nested()
try:
yield session
finally:
session.close()
transaction.rollback()
I think it can just be straight deleted

Not sure if this is a bug in SQLA but while inserting more than one
element in a relationship the generated statement seems to add typecasts based
on the values, not the column definition.

Casting provider_unique_id to a string here to fix the issue.
Without the explicit commit create_all fails because the UUID extension
is not present.
@marcospri marcospri merged commit 9e507d7 into main Jan 31, 2024
9 checks passed
@marcospri marcospri deleted the upgrade-sql branch January 31, 2024 08:51
Copy link

sentry-io bot commented Feb 3, 2024

Suspect Issues

This pull request was deployed and Sentry observed the following issues:

  • ‼️ OperationalError: (psycopg2.OperationalError) SSL connection has been closed unexpectedly status View Issue
  • ‼️ OperationalError: (psycopg2.OperationalError) Connection refused status View Issue

Did you find this useful? React with a 👍 or 👎

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants