-
Notifications
You must be signed in to change notification settings - Fork 9
Account switcher #240
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
base: master
Are you sure you want to change the base?
Account switcher #240
Conversation
|
fwiw, create new branches from master so that your branches are isolated from each other. and for your own sanity. since you already have commits in the AccountSwitcher branch, you can learn the interactive rebase, one of the only essential git tools imo: git checkout AccountSwitcher
# back up branch in case you mess it up
git branch account-switcher2
git rebase -i masterThen in the editor that pops up, you can remove the commits from the ic/ooc branch and only keep the commits unique to AccountSwitcher. You can automate this with |
|
Also, we need to deprecate adding routes to |
a36c855 to
71077f3
Compare
|
I think I've decided I hate git ;) Changes made, thanks @danneu. I think this should be good to go. |
|
The I think we can simplify it into: create table alt_group (
id serial primary key
created_at timestamptz not null default now()
)
alter table users add column alt_group_id int null references alt_group (id);Now linking is just group merge, and unlinking is just Or am I not reading the alts sql correctly? |
|
Right, and one-way ownership chains shouldn't be supported (IMO). Yeah, creating ownership groups is probably a more obvious solution. I think that's logically equivalent to what it's already doing: just pre-seeding an ownership group with the user's own ID. I think my motivation when I originally wrote the code was just to not modify the users table. |
|
Looking into your suggested schema, it looks like we'd need pretty complex conditionals to manage the different cases case 1: both users are in different alt groups and we have to merge them So a function like Maybe I'm overlooking a much simpler solution, but although the schema is a little more intuitive, the implementation seems a lot less elegant. Compared to its current implementation: |
|
Yeah, the difference with my schema is:
Linking is a lil more complicated since it handles these cases:
But you can do that in one query: WITH ensure_group AS (
-- Ensure user1 has a group (create if needed)
UPDATE users
SET alt_group_id = COALESCE(
alt_group_id,
(INSERT INTO alt_groups DEFAULT VALUES RETURNING id)
)
WHERE id = $1
RETURNING alt_group_id
)
-- Merge user2 and their entire group into user1's group
UPDATE users
SET alt_group_id = (SELECT alt_group_id FROM ensure_group)
WHERE id = $2 OR alt_group_id = (SELECT alt_group_id FROM users WHERE id = $2);So, I think this works: async function linkUsers(pgClient: PgClient, user1: number, user2: number) {
// Ensure user1 has a group (create if needed)
// Merge user2 and their entire group into user1's group
// If users are in the same group, it does a superfluous update with no change
await pgClient.query(`
WITH ensure_group AS (
UPDATE users
SET alt_group_id = COALESCE(
alt_group_id,
(INSERT INTO alt_groups DEFAULT VALUES RETURNING id)
)
WHERE id = $1
RETURNING alt_group_id
)
UPDATE users
SET alt_group_id = (SELECT alt_group_id FROM ensure_group)
WHERE id = $2 OR alt_group_id = (SELECT alt_group_id FROM users WHERE id = $2);
`, [user1, user2])
} |
|
@danneu This is done now. Had to alter the linker query because COALESCE doesn't support an INSERT statement inside its parameters. Also, does /server/reset_db need to be updated to include all of the new .sql files? I had to run those manually when setting up a fresh instance. |
Completely updated to account for the recent refactor. Works on local instance. Includes new sql schema.