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

Generate a materialized view for gameRatingsSandbox0 #270

Merged
merged 5 commits into from Jan 3, 2024

Conversation

dfabulich
Copy link
Collaborator

@dfabulich dfabulich commented Jan 2, 2024

Now we'll use gameRatingsSandbox0_mv for pretty much all purposes.

Fixes iftechfoundation/ifdb-suggestion-tracker#401 and fixes iftechfoundation/ifdb-suggestion-tracker#407

@salty-horse
Copy link
Collaborator

Is embargopastdate supposed to be used anywhere? I assume it's in the definition of the view that's not part of the repository.

Perhaps both the view and the cron jobs should be documented somewhere.

@dfabulich dfabulich force-pushed the game-ratings-materialized-view branch from 5fd9299 to 307dc87 Compare January 2, 2024 09:34
@dfabulich
Copy link
Collaborator Author

I added more comments about embargopastdate to refresh-embargoed-reviews.

// This URL will be pinged on a regular schedule via a cron job.

// This cron job is really strange.

// The purpose of the cron job is to keep the `gameRatingsSandbox0_mv`
// materialized view up to date as time passes.

// The `gameRatingsSandbox0` view automatically excludes reviews with an
// embargo date in the future; thus, the view is like a `join` of the
// `reviews` table and the current datetime.

// But the trigger that keeps `gameRatingsSandbox0_mv` updated only runs
// when we `update` the `reviews` table. The trigger doesn't naturally
// update the MV as time passes.

// This cron job incorporates the current datetime into the materialized
// view by translating the passage of time into `update` statements on the
// `reviews` table.

// The `reviews` table has an `embargopastdate` column, which serves no
// real purpose other than to be a column that we can update in order to
// activate the MV update trigger. We only read `embargopastdate` here,
// in this query, just in order to find recently unembargoed reviews that
// we haven't incorporated into the MV.

// It's kind of like running `touch` on a file, updating its lastmodified
// date, in order to convince `make` to do something with it. We're
// "touching" the `embargopastdate` in order to activate the post-update
// trigger, refreshing the MV.

The `gameRatingsSandbox0` view is used all over the site, but it's a pure view, built from several nested queries that don't optimize well.

In this commit, we're creating a `gameRatingsSandbox0_mv` materialized-view table, generated from the original `gameRatingsSandbox0` view, updated by triggers on the `reviews` table.
The `gameRatingsSandbox0` view automatically updates to include reviews whose embargo dates have passed, but we need to make an update to the `reviews` table when a review's embargo date passes in order to update the `gameRatingsSandbox0_mv` materialized view.

Here, we're adding an `embargopastdate` column to `reviews`, which we update when the embargo date has actually passed. That update will refresh the materialized view.

And then we're adding an URL that can be managed by a cron job to update `embargopastdate` for any/all reviews that just passed their embargo date.
When a user enters/leaves the sandbox, their reviews will need to be updated in the `gameRatingsSandbox0_mv` materialized view.

This code is kinda inefficient, regenerating the entire materialized view from scratch whenever any user is added/removed from the sandbox, but as of today that still only takes a second or so, and users don't get sandboxed or unsandboxed very often.
"Banning" means clicking the "ban" button in `adminops`, which sets `acctstatus` to `B` (preventing login) and sets `sandbox` to 1.

Each time we update users.sandbox, we have to regenerate `gameRatingsSandbox0_mv`, to exclude sandboxed reviews.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants