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

refactor: remove user.id use and rework vote logic #401

Merged
merged 7 commits into from
Aug 6, 2023
Merged

Conversation

iuioiua
Copy link
Contributor

@iuioiua iuioiua commented Jul 28, 2023

Vote logic was challenging to deal with, hence the rework. The most involved part of this PR is the migration script. In summary:

  • All notifications will be deleted, which, IMO, is ok.
  • User entries are untouched.
  • Items and votes entries are migrated according to their new respective interfaces.

Reviews, please pay close attention to the migration script. It aims to preserve item, vote and user data. This script has worked multiple times on my local machine, but I still want it checked carefully. To test:

  1. On the main branch, run deno task db:reset and then deno task db:seed.
  2. Run deno task start, sign in, vote and comment on random items, noting the changes.
  3. Switch to this branch and run deno run -A --unstable tools/migrate_kv.ts.
  4. Run deno task start and ensure all data is preserved.

Please review, if willing and able, @brunocorrea23 and @mbhrznr.

Closes #400

@iuioiua iuioiua changed the title refactor: remove user.id use refactor: remove user.id use and rework vote logic Jul 31, 2023
@iuioiua iuioiua changed the title refactor: remove user.id use and rework vote logic refactor: remove user.id use and rework vote logic Jul 31, 2023
@iuioiua iuioiua requested a review from kt3k July 31, 2023 01:06
@iuioiua iuioiua marked this pull request as ready for review July 31, 2023 01:07
Copy link
Contributor Author

@iuioiua iuioiua left a comment

Choose a reason for hiding this comment

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

Some comments to understand the changes. There are other minor changes throughout this PR which are self-explanatory.

routes/_404.tsx Outdated
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Passing-by changes.

routes/_500.tsx Outdated
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Passing-by changes.

await Promise.all(promises1);

// Items
const promises2 = [];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Both arrays of promises, promises1 and promises2, must resolve sequentially. Vote migrations cover all items that have votes. Item migrations cover all remaining items that have no votes.

Using Promise.all() is optimal for performance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note to self: voted_items_by_user and voted_users_by_item indexes should be deleted once this PR is merged, as they are no longer used.

Comment on lines +16 to +20
const [creatorUserRes, voterUserRes] = await kv.getMany<OldUser[]>([
["users", oldItem.userId],
["users", voterUserId],
]);
assertIsEntry(creatorUserRes);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The distinction between these two types of users must remain correct throughout this function.

if (!res.ok) throw new Error(`Failed to set vote: ${vote}`);
}

async function migrateItem(oldItem: OldItem) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is run after migrateVote() and only takes care of those items with no votes.

@mbhrznr
Copy link
Contributor

mbhrznr commented Jul 31, 2023

thank you for providing the comments alongside the changes, which helped a lot in understanding the broader context.
also thank you for providing the steps to get this going!

i was able to get through the steps w/o any issues.
resetting, seeding and running the migration worked like a charm.
the votes i initially added were also persisted and still existed after i run the migration script.

however i've noticed the following:

  1. downvoting an upvoted item removes the vote from the counter, however on refresh the post is still marked as upvoted. this way an item can be downvoted to 0.

  2. upvoting an item, which you already interacted with increments the counter and updates the color in an optimistic update, yet on refresh the vote is gone. initially i thought this could just be the default browser behavior of re-submitting the form, however the same happens whenever i navigate to the page, e.g. via click on the logo.

  3. trying to upvote an existing item, which you haven't interacted with before, throws an AssertionError, which seems to be coming from the assertIsEntry check:

    An error occurred during route handling or page rendering. AssertionError: Expected actual: null not to be: null: KV entry not found
  4. initially upvoting any items which are created after the migration works as expected. downvoting that exact same item still shows the item as upvoted when i navigate to the page (see 1.).

i will also do a more thorough review of the code changes but wanted to drop a note about my findings asap.

@iuioiua
Copy link
Contributor Author

iuioiua commented Aug 3, 2023

I've fixed the migration script by removing what may be unneeded assertions in it. @kt3k and @mbhrznr, can you please re-run the migration script with the new changes? This likely fixes the issues outlined in the previous comment, as the vote migrations weren't completed.

Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

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

Ran migration on prod KV snapshot successfully and updated DB seems working without problem. LGTM

@mbhrznr
Copy link
Contributor

mbhrznr commented Aug 4, 2023

@iuioiua thanks for the update!

just tested the functionality from resetting to seeding to voting and migrating.
yet it looks like i can still reproduce all of the findings in the initial comment.
did any of these occur on your end(s) as well?
Bildschirm­foto 2023-08-04 um 16 15 31

@iuioiua
Copy link
Contributor Author

iuioiua commented Aug 6, 2023

I think the seed task may cause the issue you're experiencing. I just tested using the actual production data for the site, and it works fine! I'll run the migration and go from there. Thanks for your help, guys!

@iuioiua iuioiua merged commit 617fd6f into main Aug 6, 2023
@iuioiua iuioiua deleted the remove-user-id branch August 6, 2023 21:52
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.

proposal: remove use of user.id
3 participants