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

Add database migration for the user_invites table #3536

Merged
merged 9 commits into from
Jun 7, 2024
Merged

Conversation

rdimitrov
Copy link
Member

@rdimitrov rdimitrov commented Jun 5, 2024

Summary

The following adds a user_invites table.

The role column is a string where the expected values are some of 'admin', 'editor', 'viewer', 'policy_writer', 'permissions_manager' (at least for now).

Fixes: #3533

Change Type

Mark the type of change your PR introduces:

  • Bug fix (resolves an issue without affecting existing features)
  • Feature (adds new functionality without breaking changes)
  • Breaking change (may impact existing functionalities or require documentation updates)
  • Documentation (updates or additions to documentation)
  • Refactoring or test improvements (no bug fixes or new functionality)

Testing

Outline how the changes were tested, including steps to reproduce and any relevant configurations.
Attach screenshots if helpful.

Review Checklist:

  • Reviewed my own code for quality and clarity.
  • Added comments to complex or tricky code sections.
  • Updated any affected documentation.
  • Included tests that validate the fix or feature.
  • Checked that related changes are merged.

@coveralls
Copy link

Coverage Status

coverage: 53.127%. remained the same
when pulling 4a540fd on invites-db
into 936892a on main.

@coveralls
Copy link

Coverage Status

coverage: 53.189% (-0.005%) from 53.194%
when pulling da82fa3 on invites-db
into cded599 on main.

Comment on lines 28 to 29
invitee INTEGER NULL REFERENCES users(id) ON DELETE CASCADE,
invited_by INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
Copy link
Member Author

@rdimitrov rdimitrov Jun 5, 2024

Choose a reason for hiding this comment

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

Noticed that the users ID is SERIAL (not UUID) thus why the INTEGER type. I guess the other approach was to use the subject_identity (UUID saved as text)

Copy link
Contributor

Choose a reason for hiding this comment

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

Might it make sense to add a UUID ID column to the users table, populate with autogenerated values, and then replace use of the integer ID with a UUID one?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's out of the scope of this effort but it's a good idea, i.e. I feel much better seeing uuids as IDs then 7 for example 😃

@rdimitrov rdimitrov force-pushed the invites-db branch 2 times, most recently from 728f179 to bb19dd9 Compare June 5, 2024 11:30
@coveralls
Copy link

Coverage Status

coverage: 53.199% (+0.02%) from 53.184%
when pulling bb19dd9 on invites-db
into 3d23d1e on main.

@coveralls
Copy link

Coverage Status

coverage: 53.189% (+0.005%) from 53.184%
when pulling bb19dd9 on invites-db
into 3d23d1e on main.

@coveralls
Copy link

Coverage Status

coverage: 53.189% (+0.005%) from 53.184%
when pulling bb19dd9 on invites-db
into 3d23d1e on main.

@coveralls
Copy link

Coverage Status

coverage: 53.194% (+0.01%) from 53.184%
when pulling bb19dd9 on invites-db
into 3d23d1e on main.

Copy link
Member

@evankanderson evankanderson left a comment

Choose a reason for hiding this comment

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

A few comments to hopefully reduce the amount of work. 😁

database/migrations/000061_user_invites.up.sql Outdated Show resolved Hide resolved
CREATE TABLE IF NOT EXISTS user_invites(
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
email TEXT NOT NULL,
status invite_status NOT NULL DEFAULT 'pending',
Copy link
Member

Choose a reason for hiding this comment

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

What are other values of status would we use? (Do we need this column?)

Copy link
Member

Choose a reason for hiding this comment

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

To explain further, I figured that we'd remove the row when an invitation is accepted / declined / revoked, and we'd periodically remove expired invitations.

If we do want to track how invitations are dispositioned, it feels like we should keep a separate audit log, rather than combining it with the live data.

For example, if we keep these as a log, then the unique index on (email, project) will block re-inviting someone to a project if they accidentally are removed from the project, or will delete the audit log entry.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was thinking of the use case where you list the invitations and see some that are pending, some expired, but it makes more sense to make that as separate audit log. Thanks!

Comment on lines 33 to 34
-- ensure there's one invite for this email per project
UNIQUE (email, project),
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need to ensure that this is unique? What if I wanted to invite a user as both viewer and permission manager?

Copy link
Member Author

Choose a reason for hiding this comment

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

I had the assumption that a user can have only one role per project or this was incorrect?

database/migrations/000061_user_invites.up.sql Outdated Show resolved Hide resolved
database/migrations/000061_user_invites.up.sql Outdated Show resolved Hide resolved
Comment on lines 48 to 49
-- create an index on the invited_by column
CREATE INDEX idx_user_invites_sponsor ON user_invites(sponsor);
Copy link
Member

Choose a reason for hiding this comment

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

What do we need this index for?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was thinking of helping the use case for "give me all invitations I've received from X".

database/migrations/000061_user_invites.up.sql Outdated Show resolved Hide resolved
Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com>
Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com>
Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com>
@rdimitrov
Copy link
Member Author

@evankanderson - Thanks for the tips! 🙏 I like how much cleaner it got after applying your feedback 👍

@coveralls
Copy link

Coverage Status

coverage: 53.256% (+0.02%) from 53.241%
when pulling dccb268 on invites-db
into d986b0c on main.

@coveralls
Copy link

Coverage Status

coverage: 53.256% (+0.02%) from 53.241%
when pulling dccb268 on invites-db
into d986b0c on main.

@coveralls
Copy link

Coverage Status

coverage: 53.108% (+0.004%) from 53.104%
when pulling 2c1623f on invites-db
into 1ec9ffc on main.

@coveralls
Copy link

Coverage Status

coverage: 53.114% (+0.01%) from 53.104%
when pulling 2c1623f on invites-db
into 1ec9ffc on main.

@coveralls
Copy link

Coverage Status

coverage: 53.137% (-0.01%) from 53.148%
when pulling 1edfbf6 on invites-db
into a21f1dd on main.


CREATE TABLE IF NOT EXISTS user_invites (
code TEXT NOT NULL PRIMARY KEY,
email TEXT NOT NULL,
Copy link
Member

Choose a reason for hiding this comment

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

Removing the index on (email, project) means that we no longer have an index on email. I'd be tempted to re-add this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ugh, you're right! I must have deleted it along the other stuff 🤦‍♂️

Copy link
Member

Choose a reason for hiding this comment

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

I pointed out that a UNIQUE(email, project) index would prevent inviting someone to multiple roles in a project, so it was sort of my fault.

Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com>
Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com>
@coveralls
Copy link

Coverage Status

coverage: 53.143%. remained the same
when pulling e37c6a5 on invites-db
into 59c9839 on main.

@rdimitrov rdimitrov merged commit 52a5da6 into main Jun 7, 2024
22 checks passed
@rdimitrov rdimitrov deleted the invites-db branch June 7, 2024 06:00
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.

Add tables for invitations
4 participants