-
Notifications
You must be signed in to change notification settings - Fork 892
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Using views to make models for sqlc
-- 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 the dump.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.
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 ✔️
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 this VIEW
too.
Because we already query the owner
, 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 have GetUsersByIDs
, so it should be relatively straightforward to swap out.
A JOIN
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.
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.
DROP VIEW 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.
ALTER TABLE workspace_builds
ADD extra_column int DEFAULT 1 NOT NULL;
CREATE VIEW workspace_build_with_initiator AS
SELECT workspace_builds.*, coalesce(users.username, 'unknown') AS initiator_username
FROM workspace_builds
LEFT JOIN users ON workspace_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 :(.
|
I am going to close this PR in favor of the status quo for 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 |
What this does
All
workspace_builds
now also returninitiator_name
withinitiator_id
. This allows the UI to use the username of the initiator.Uses PG 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 in 8beccfa