- Notifications
You must be signed in to change notification settings - Fork1.1k
feat: initiator_username joined to workspace_build queries#2137
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
| -- This view adds the initiator name to the query for UI purposes. | ||
| -- Showing the initiator user ID is not very friendly. | ||
| CREATE VIEW workspace_build_with_initiator AS | ||
| -- If the user is nil, just use 'unknown' for now. | ||
| SELECT workspace_builds.*, coalesce(users.username, 'unknown') AS initiator_username | ||
| FROM workspace_builds | ||
| LEFT JOIN users ON workspace_builds.initiator_id = users.id; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Will this require adding a migration every time we want to change the query?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
@kylecarbs yes. Another migration will drop the old view and make a new one.
If we change the columns, it will be required too as thedump.sql expands*. I just realized that 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Unless we implement something else to "migrate" views.
A view is a saved query on the database. The alternative is to use temporary views which only last for a single "session".So this alternative thing to "migrate" views could be a set of temporary views we initiate when we open a SQL connection. (maybe, brainstorming here, I think that would work) With connection pooling this might not work.
Kira-Pilot left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
FE ✔️
kylecarbs left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Because this introduces a new paradigm, I'm a lot more hesitant. Adjusting the schema of workspace builds would now have a dependency on updating thisVIEW too.
Because we already query theowner, could we add the initiator to that query instead? Then it'd be the same number of queries, but just an additional row returned. We already haveGetUsersByIDs, so it should be relatively straightforward to swap out.
AJOIN will certainly be more performant, but I'm mostly concerned about the complexity of interfacing with our database at this point, much less the performance overhead of fetching a lil more data.
I'd love to understand your thoughts here@Emyrk, it's very possible I'm misunderstanding how this should be used.
Emyrk commentedJun 8, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Just had a conversation with@kylecarbs in discord. In summary, the views are a method that enables using joins in our SQL queries, which is powerful and can cut down on the number of DB calls we make. The contention to pushing this PR through is maintaining the view, and defaulting to using joins. Con: Maintaining the viewThis PR adds a view ontop of -- We must drop the old view first, then recreate it. We cannot just update the old view-- as the new view must have the same columns.DROPVIEW workspace_build_with_initiator;-- This alter table could be adding a column OR removing a column. In both cases, we will-- need to drop the view first.-- You cannot delete a column that has a dependency on it, and a view is a dependency.ALTERTABLE workspace_builds ADD extra_columnint DEFAULT1NOT NULL;CREATEVIEWworkspace_build_with_initiatorASSELECT workspace_builds.*, coalesce(users.username,'unknown')AS initiator_usernameFROM workspace_buildsLEFT JOIN usersONworkspace_builds.initiator_id=users.id; The biggest downside is that is adds some complexity to our migrations. The developer writing the migration needs to know which views to also update. They will likely have to copy the This is just the cost of the views. Con: Defaulting to using joinsOne downside is that all our queries now use joins. In the example above, the data being joined is only used by the UI. So internal usage doesn't require the join (at the moment). However, having the additional context could be nice for logging purposes, so it might not be a total waste. I don't think join performance will be so bad that this is really an issue, but it can cause over-fetching. The upside
Alternative (Status Quo)The alternative is to do the status quo, where we make multiple db calls and merge the resulting structs.
Extra linksSQLc implementations that could help. But gated since these features do not exist yet :(.
|
Emyrk commentedJun 8, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I am going to close this PR in favor of the status quofor now. I will work on this PR to include some other views that could benefit and get a larger team decision about this. This is a repo design and maintenance change, so will be a discussion. Until then, we still need the feature, so I'll do it in another PR |
Uh oh!
There was an error while loading.Please reload this page.
What this does
All
workspace_buildsnow also returninitiator_namewithinitiator_id. This allows the UI to use the username of the initiator.UsesPG views to make models for sqlc.
Future Work
Currently autostart related entries use the workspace owner as the initator. We should change this to show some "autostart" or "system" user.
coder/coderd/autobuild/executor/lifecycle_executor.go
Line 238 in8beccfa