Skip to content

upgrading for SQLAlchemy 2.0+#75

Open
crooksey wants to merge 3 commits into
ergo:masterfrom
crooksey:master
Open

upgrading for SQLAlchemy 2.0+#75
crooksey wants to merge 3 commits into
ergo:masterfrom
crooksey:master

Conversation

@crooksey

@crooksey crooksey commented Jan 7, 2025

Copy link
Copy Markdown
Contributor

Open for discussion, but think this is all that's needed, also backwards compatible.

@ergo any thoughts?

@ergo

ergo commented Jan 10, 2025

Copy link
Copy Markdown
Owner

Did you try this? The tests are not passing for me. I will take a look when I'll have the chance.

@crooksey

Copy link
Copy Markdown
Contributor Author

I tested this code, you are right that some tests now fail. I have updated the conftest.py to use the 2.0 method of "execute" for SQLAlchemy, there are a few more tests to now individually work on, but I have pushed the changes to the "conftest" file for now.

@ergo

ergo commented Jan 11, 2025

Copy link
Copy Markdown
Owner

Sounds great - now the main question is - will the changes still work on 1.x? If not maybe it makes more sense to release 1.x as it is and then release 2.x with alchemy 2 compatibility.

@crooksey

crooksey commented Jan 11, 2025 via email

Copy link
Copy Markdown
Contributor Author

@crooksey

Copy link
Copy Markdown
Contributor Author

So my new changes ARE backwards compatible, so thats all good.

The test error I also get on SQLAlchemy 1.4x, its on the test:

def test_resources_with_group_permission(

All other tests pass fine, the error code for the failed test is:

================================================================================================== FAILURES ==================================================================================================
__________________________________________________________________________ TestUserPermissions.test_resources_with_group_permission __________________________________________________________________________

self = <ziggurat_foundations.tests.test_permissions.TestUserPermissions object at 0x7fd0c51f8910>, db_session = <sqlalchemy.orm.session.Session object at 0x7fd0c4698050>

    def test_resources_with_group_permission(self, db_session):
        created_user = add_user(db_session)
        resource = add_resource(db_session, 1, "test_resource")
        resource2 = add_resource(db_session, 2, "test_resource2")
        # add_resource(db_session, 3, "test_resource3")
        group = add_group(db_session)
        group.users.append(created_user)
        group_permission = GroupResourcePermission(
            perm_name="test_perm", group_id=1, resource_id=resource.resource_id
        )
        group_permission2 = GroupResourcePermission(
            perm_name="foo_perm", group_id=1, resource_id=resource2.resource_id
        )
        resource.group_permissions.append(group_permission)
        resource2.group_permissions.append(group_permission2)
        db_session.flush()
>       resources = UserService.resources_with_perms(
            instance=created_user, perms=["foo_perm"], db_session=db_session
        )

ziggurat_foundations/tests/test_permissions.py:268: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
ziggurat_foundations/models/services/user.py:111: in resources_with_perms
    query = query.outerjoin(
.tox/python/lib/python3.11/site-packages/sqlalchemy/orm/query.py:2488: in outerjoin
    return self.join(target, onclause=onclause, isouter=True, full=full)
<string>:2: in join
    ???
.tox/python/lib/python3.11/site-packages/sqlalchemy/sql/base.py:279: in _generative
    x = fn(self, *args, **kw)
<string>:2: in join
    ???
.tox/python/lib/python3.11/site-packages/sqlalchemy/orm/base.py:306: in generate
    fn(self, *args, **kw)
.tox/python/lib/python3.11/site-packages/sqlalchemy/orm/query.py:2443: in join
    join_target = coercions.expect(
.tox/python/lib/python3.11/site-packages/sqlalchemy/sql/coercions.py:395: in expect
    resolved = impl._literal_coercion(
.tox/python/lib/python3.11/site-packages/sqlalchemy/sql/coercions.py:1233: in _literal_coercion
    self._raise_for_expected(element, argname)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <sqlalchemy.sql.coercions.JoinTargetImpl object at 0x7fd0c6233080>
element = (<class 'ziggurat_foundations.tests.conftest.GroupResourcePermission'>, <sqlalchemy.sql.elements.BooleanClauseList object at 0x7fd0c445ead0>), argname = None, resolved = None, advice = None
code = None, err = None, kw = {}, got = "(<class 'ziggurat_foundations.tests.conftest.GroupResourcePermission'>, <sqlalchemy.sql.elements.BooleanClauseList object at 0x7fd0c445ead0>)"
msg = "Join target, typically a FROM expression, or ORM relationship attribute expected, got (<class 'ziggurat_foundations.tests.conftest.GroupResourcePermission'>, <sqlalchemy.sql.elements.BooleanClauseList object at 0x7fd0c445ead0>)."

    def _raise_for_expected(
        self,
        element: Any,
        argname: Optional[str] = None,
        resolved: Optional[Any] = None,
        *,
        advice: Optional[str] = None,
        code: Optional[str] = None,
        err: Optional[Exception] = None,
        **kw: Any,
    ) -> NoReturn:
        if resolved is not None and resolved is not element:
            got = "%r object resolved from %r object" % (resolved, element)
        else:
            got = repr(element)
    
        if argname:
            msg = "%s expected for argument %r; got %s." % (
                self.name,
                argname,
                got,
            )
        else:
            msg = "%s expected, got %s." % (self.name, got)
    
        if advice:
            msg += " " + advice
    
>       raise exc.ArgumentError(msg, code=code) from err
E       sqlalchemy.exc.ArgumentError: Join target, typically a FROM expression, or ORM relationship attribute expected, got (<class 'ziggurat_foundations.tests.conftest.GroupResourcePermission'>, <sqlalchemy.sql.elements.BooleanClauseList object at 0x7fd0c445ead0>).

.tox/python/lib/python3.11/site-packages/sqlalchemy/sql/coercions.py:518: ArgumentError

@crooksey

Copy link
Copy Markdown
Contributor Author

@ergo I have had another look, but still can't work out why this is failing, It may be todo with the way "outerjoin" now works in 2.0, but I can't see any issues with breaking changes on the changelog for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants