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

use second db handle for only for user admin and writes #1184

Merged
merged 6 commits into from
Jun 26, 2023

Conversation

melange396
Copy link
Collaborator

change 'user_engine' to a 'WriteSession' instead, so the master db connection is used for writes only

dmytrotsko
dmytrotsko previously approved these changes May 31, 2023
…tag engines with id and log those too, and superfluous user method cleanup
@melange396
Copy link
Collaborator Author

cherry-picked and force updated to set these changes against the current dev branch

@melange396 melange396 marked this pull request as ready for review June 20, 2023 21:16
@melange396
Copy link
Collaborator Author

updated change summary:

  • changed user_engine to WriteSession.

  • made sure sql statements and timing are logged for all engines, and engines are tagged with identifiers.

  • added eager-loading of roles when fetching users from db via orm.

  • removed some unnecessary methods -- those used very few times, or some made irrelevant by the eager-loaded roles.

  • added @default_session provider decorator.

    • allows a default (and hopefully appropriate) session for the decorated method, which will be used as a context manager if an already open session is not passed instead.
  • removed individual session context managers from individual User ORM method calls, and replaced them with the @default_session decorator.

    • i tried to add the 'session' argument to @default_session decorated functions as the last non-defaulted argument, so we didnt need to put 'session=None', but it looks kinda weird and isnt a very standardizing convention. suggestions for alternatives are welcome.
  • added session context managers to the admin page implementation, so one session can be used for a whole (single) admin operation.

dmytrotsko
dmytrotsko previously approved these changes Jun 21, 2023
Copy link
Contributor

@dmytrotsko dmytrotsko left a comment

Choose a reason for hiding this comment

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

Nice work!

@@ -25,7 +25,7 @@ def _default_date_now():
class User(Base):
__tablename__ = "api_user"
id = Column(Integer, primary_key=True, autoincrement=True)
roles = relationship("UserRole", secondary=association_table)
roles = relationship("UserRole", secondary=association_table, lazy="joined") # last arg does an eager load of this property from foreign tables
Copy link
Contributor

Choose a reason for hiding this comment

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

Wow, that is awesome. Didn't know about that param.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

🌊 🏄

@krivard krivard self-requested a review June 21, 2023 14:05
@melange396 melange396 changed the title [WIP] use second db handle for writes only use second db handle for writes only Jun 21, 2023
@melange396 melange396 changed the title use second db handle for writes only use second db handle for only for user admin and writes Jun 21, 2023
krivard
krivard previously approved these changes Jun 21, 2023
Copy link
Contributor

@krivard krivard left a comment

Choose a reason for hiding this comment

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

Some questions about TODOs and when to/not-to use the decorators, but no dealbreakers. Beautiful work!

src/server/_common.py Show resolved Hide resolved
src/server/_db.py Show resolved Hide resolved
src/server/admin/models.py Show resolved Hide resolved
Comment on lines +62 to +64
session.commit()
# retrieve the newly updated User object
return session.query(User).filter(User.id == user.id).first()
Copy link
Contributor

Choose a reason for hiding this comment

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

is this still needed if we're using WriteSession for both writes and reads in admin?

oh i see; this is adding back the commit removed from 99, and lets you return directly instead of needing a separate return at 100

carry on

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yap!

src/server/admin/models.py Show resolved Hide resolved
)
session.execute(update_stmt)
return User._assign_roles(user, roles, session)
# TODO: else: raise Exception() ??
Copy link
Contributor

Choose a reason for hiding this comment

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

does this TODO need to be resolved before merge, or should we file a ticket for it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

¯\_(ツ)_/¯ im ok with neither because our usage of this seems consistent enough right now. a future caller of this can get the same information by checking for a None return value instead of catching an exception. but its easy enough to do, so ill just do it. change incoming shortly...

src/server/admin/models.py Show resolved Hide resolved
session.commit()
""")
session.commit()
# TODO: look up and return new role
Copy link
Contributor

Choose a reason for hiding this comment

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

does this TODO need to be resolved before merge, or should we file a ticket for it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

¯\_(ツ)_/¯ im ok with neither, esp because create_role() isnt even used anywhere at the moment. but its easy enough to do, so ill just do it. change incoming shortly...

src/server/endpoints/admin.py Show resolved Hide resolved
src/server/endpoints/admin.py Show resolved Hide resolved
TODOs done: raise Exception when trying to update non-existent User, return UserRole on creation.
also use more appropriate reciever for static method call, and expand comment on static vs bound methods in User.
@melange396 melange396 dismissed stale reviews from krivard and dmytrotsko via 1c926e7 June 23, 2023 18:53
@sonarcloud
Copy link

sonarcloud bot commented Jun 23, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 4 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Copy link
Contributor

@krivard krivard left a comment

Choose a reason for hiding this comment

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

👍 🚀

@melange396 melange396 merged commit 6fe6e7a into dev Jun 26, 2023
@melange396 melange396 deleted the master_db_cnxn_write_only branch June 26, 2023 15:56
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.

3 participants