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

Implemented raw sql replacing sqlalchemy in me endpoint #1334

Merged
merged 5 commits into from
Mar 19, 2024

Conversation

Sujanadh
Copy link
Collaborator

@Sujanadh Sujanadh commented Mar 7, 2024

What type of PR is this? (check all applicable)

  • πŸ• Feature
  • πŸ› Bug Fix
  • πŸ“ Documentation Update
  • 🎨 Style
  • πŸ§‘β€πŸ’» Code Refactor
  • πŸ”₯ Performance Improvements
  • βœ… Test
  • πŸ€– Build
  • πŸ” CI
  • πŸ“¦ Chore (Release)
  • ⏩ Revert

Related Issue

Describe this PR

  • This PR adds user roles along with project_roles, associated project, and organisation if created by the user, which means if the user is the org_admin.
  • It implements raw SQL in order to check and update the user or create the user.
  • Tried to use the CASE statement, but it didn't work as expected since it only allows to return boolean-type results (true, false) but not for update purposes, if I am not wrong.

One problem I encountered while performing the insert action was that I needed to mention all the not null fields even though they have default values, unlike those handled by Alchemy automatically since we do not have the default values set at the database level.

Screenshots

Different Responses:
Screenshot from 2024-03-07 23-20-48
Screenshot from 2024-03-07 23-22-09

Checklist before requesting a review

[optional] What gif best describes this PR or how it makes you feel?

@Sujanadh Sujanadh requested review from nrjadkry and spwoodcock March 7, 2024 18:06
@Sujanadh Sujanadh self-assigned this Mar 7, 2024
@github-actions github-actions bot added the backend Related to backend code label Mar 7, 2024
@Sujanadh
Copy link
Collaborator Author

I will fix the test cases related to the user.

@github-actions github-actions bot added the frontend Related to frontend code label Mar 12, 2024
Comment on lines 149 to 150
INSERT INTO users (
id, username, profile_img, role, mapping_level,
Copy link
Member

Choose a reason for hiding this comment

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

This could possibly be simplified by the suggestion Ping made, with an 'upsert' query.

Something like

INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...)
ON CONFLICT (id, username)
DO UPDATE SET column1 = value1, column2 = value2, ...;

To make this work do we need a migration to ensure the username field has UNIQUE specified? Both changes combined would simplify this query a lot πŸ‘

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, Sure. By the way, the username field was already a unique field.

@Sujanadh
Copy link
Collaborator Author

updated the sql using CONFLICT ON

Copy link
Member

@spwoodcock spwoodcock 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!

Just a few minor points and it can be merged

get_sql = text(
"""
SELECT users.*,
COALESCE(user_roles.project_id) as project_id,
Copy link
Member

Choose a reason for hiding this comment

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

Coalesce could be removed here, as it's for merging two values together.

It's useful to set the default value to mapper below, but not needed for the user role or org manager parts πŸ‘

get_sql,
{"user_id": user_data.id},
)
db_user = result.fetchall()
Copy link
Member

Choose a reason for hiding this comment

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

.first() will return a single value, so that you don't have to index with [0] below. Then you also can avoid needing the for loop too πŸ‘

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, you are right. Great!
Thanks for the feedback!!

@spwoodcock spwoodcock merged commit dc170d4 into development Mar 19, 2024
6 checks passed
@spwoodcock spwoodcock deleted the refactor/me-raw-sql branch March 19, 2024 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Related to backend code frontend Related to frontend code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement user roles in me endpoint
2 participants