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

SQLAlchemy 2.0 upgrades (part 4) #16852

Merged
merged 32 commits into from
Nov 10, 2023
Merged

Conversation

jdavcs
Copy link
Member

@jdavcs jdavcs commented Oct 13, 2023

SQLAlchemy 2.0 compatibility upgrades. Ref #12541.

This covers the managers/ directory, with just a few cases left out (in base, history_contents and sharable - those involve query-building abstractions which may require more refactoring; I'll have a separate PR for those). All commits are atomic; most are straightforward syntactical edits. If there is a notable refactoring present, I noted that in the commit description.

How to test the changes?

(Select all options that apply)

  • I've included appropriate automated tests.
  • This is a refactoring of components with existing test coverage.
  • Instructions for manual testing are as follows:
    1. [add testing steps and prerequisites here if you didn't write automated tests covering all your changes]

License

  • I agree to license these and all my past contributions to the core galaxy codebase under the MIT license.

@jdavcs jdavcs added kind/refactoring cleanup or refactoring of existing code, no functional changes area/database Galaxy's database or data access layer labels Oct 13, 2023
@jdavcs jdavcs added this to the 23.2 milestone Oct 13, 2023
@jdavcs jdavcs force-pushed the dev_sa20_fix20 branch 10 times, most recently from 9340b20 to 1824d65 Compare October 19, 2023 22:06
@jdavcs jdavcs force-pushed the dev_sa20_fix20 branch 8 times, most recently from 1956de1 to 3954230 Compare October 25, 2023 19:48
@jdavcs jdavcs marked this pull request as ready for review October 26, 2023 14:40
@jdavcs jdavcs mentioned this pull request Oct 26, 2023
4 tasks
@jdavcs jdavcs requested a review from a team October 31, 2023 13:19
@jdavcs jdavcs marked this pull request as draft November 3, 2023 14:55
@jdavcs
Copy link
Member Author

jdavcs commented Nov 3, 2023

Converting to draft. While working on a related session issue caught a failing API test that only happens locally and is SA-related. I don't understand (yet) why it has always passed remotely, so marking this as draft until l figure this out and can verify it works correctly.

@jdavcs
Copy link
Member Author

jdavcs commented Nov 3, 2023

Yes, so it happens on sqlite only:
test_workflows.py::TestWorkflowsApi::test_invocation_map_over
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) 1st ORDER BY term does not match any column in the result set.
I'll fix this one and will rerun the entire suite locally - I wonder if there'll be many more failures that have went unnoticed.

jdavcs added 23 commits November 9, 2023 15:33
Can't call union() more than once because on second call we have a
CompoundSelect. Reversing stmt with all-stmts will result in improper
nesting. Proposed fix solves this: we collect Select statements into a
list, then union the first item with the rest. The list is guaranteed to
have at least one item.
Refactor: use native SA one_or_none method instead of calling
self._one_or_none >> self._one_with_recast_errors.
Reason: old code duplicated SA's error catching logic while not
providing a useful error message. Using SA's native method achieves the
same result with a more useful error message and without adding custom code.
The _one_or_none method is dropped as it is not used anywhere else.
We don't need eagerloads(False): it's an `exists` statement; there
were no joins in the first place.
Add forms manager module for FormDefinition/FormDefintionCurrent data access functions.
@jdavcs jdavcs marked this pull request as ready for review November 9, 2023 20:34
@jdavcs
Copy link
Member Author

jdavcs commented Nov 9, 2023

I've removed 2 commits which handled the index_query method in the Job manager. I'll submit a separate PR for that because that requires special handling. Here's the gist of it: if workflow_id or invocation_id are supplied, the select statement becomes a union. That leads to the following problem: the combination of UNION and ORDER BY don't behave as expected under SQLite, which does not recognize the term create_time as referencing a column in the result set: it requires a table prefix, so job.create_time would work. The legacy Query object handled this by wrapping the union in an additional SELECT, in which case Job.create_time was automatically prefixed with anon_1 - the table name generated by SQLAlchemy. The Select object does not handle this case implicitly. As a result, (1) we get much cleaner SQL, and (2) an error under SQLite.

The solution is relatively simple, but requires some refactoring that, I feel, belongs in a separate PR (it's a different type of fix-up compared to what's done here (which is to only upgrade to SA2.0).

SQL generated by the Select object:

SELECT job.id, job.create_time, ...
FROM job ...
UNION
    SELECT job.id, job.create_time, ...
    FROM job ...
ORDER BY create_time DESC  -- this breaks

SQL generated by the legacy Query object:

SELECT
	anon_1.job_id AS anon_1_job_id,
	anon_1.job_create_time AS anon_1_job_create_time, ...
FROM
	(
		SELECT
			job.id AS job_id,
			job.create_time AS job_create_time, ...
		FROM job ...
		UNION
			SELECT
				job.id AS job_id,
				job.create_time AS job_create_time, ...
			FROM job ...
ORDER BY
	anon_1.job_create_time DESC  -- this works fine

)
return bool(self.session().execute(stmt).first())
Copy link
Member

Choose a reason for hiding this comment

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

Seems like an exists would be marginally better ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, and easier to read too! I'll add this to the next batch (#16932).
Thanks for reviewing!

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed in 7769ddc.

Copy link
Member

@mvdbeek mvdbeek left a comment

Choose a reason for hiding this comment

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

Looks great, thank you!

@mvdbeek mvdbeek merged commit 1956639 into galaxyproject:dev Nov 10, 2023
49 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/database Galaxy's database or data access layer kind/refactoring cleanup or refactoring of existing code, no functional changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants