Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Closed
wants to merge 4 commits into from

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Jun 7, 2022

What this does

All workspace_builds now also return initiator_name with initiator_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.

InitiatorID: workspace.OwnerID,

@Emyrk Emyrk marked this pull request as ready for review June 7, 2022 20:56
@Emyrk Emyrk requested a review from a team as a code owner June 7, 2022 20:56
Comment on lines +1 to +7
-- 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;
Copy link
Member

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?

Copy link
Member Author

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 🤔

Copy link
Member Author

@Emyrk Emyrk Jun 8, 2022

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.

Copy link
Member

@Kira-Pilot Kira-Pilot left a comment

Choose a reason for hiding this comment

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

FE ✔️

Copy link
Member

@kylecarbs kylecarbs left a 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.

@Emyrk
Copy link
Member Author

Emyrk commented Jun 8, 2022

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 view

This PR adds a view ontop of workspace_builds called workspace_build_with_initiator. If a column is added to workspace_build, that column is not automatically added to the view. So your migration will look like this:

-- 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 CREATE VIEW ... code block from migration to migration.

This is just the cost of the views.

Con: Defaulting to using joins

One 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

  • Less db calls as we can merge them into 1 with a join
  • Still 1 database.Type shared by all sqlc queries
  • Join queries are reused, and only typed once.

Alternative (Status Quo)

The alternative is to do the status quo, where we make multiple db calls and merge the resulting structs.

  • Query for workspace build
  • Query for owner & initiator user's
  • Merge in a go convertWorkspaceBuild function.
    • Handling errors in convert is currently inconsistent. The SQL joins handle these more consistently.

Extra links

SQLc implementations that could help. But gated since these features do not exist yet :(.

@Emyrk
Copy link
Member Author

Emyrk commented Jun 8, 2022

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

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