-
Notifications
You must be signed in to change notification settings - Fork 3
Add migration to backfill terms of use #1203
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
Draft
AndrewKostka
wants to merge
2
commits into
add-terms-migration
Choose a base branch
from
add-terms-backfill
base: add-terms-migration
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+61
−0
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
database/migrations/2026_07_13_075347_backfill_policy_acceptance_existing_users.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| <?php | ||
|
|
||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Support\Facades\DB; | ||
|
|
||
| return new class() extends Migration { | ||
| /** | ||
| * Users created before this timestamp are treated as having accepted | ||
| * our existing terms of use. Users created afterwards go through our | ||
| * new policy flow (T430529) and are excluded from the backfill. | ||
| */ | ||
| private const USER_CREATED_AT_CUTOFF = '2026-07-13 08:00:00'; | ||
|
|
||
| /** | ||
| * Run the migrations. | ||
| */ | ||
| public function up(): void { | ||
| $policyId = $this->getPolicyId(); | ||
| $timestamp = now(); | ||
|
|
||
| DB::table('users') | ||
| ->leftJoin('policy_acceptances', fn ($join) => $join->on('users.id', '=', 'user_id') | ||
| ->where('policy_id', '=', $policyId) | ||
| ) | ||
| ->whereNull('policy_id') | ||
| ->where('users.created_at', '<', self::USER_CREATED_AT_CUTOFF) | ||
| ->orderBy('users.id') | ||
| ->select('users.id', 'users.created_at') | ||
| ->chunkById(100, fn ($users) => DB::table('policy_acceptances')->insert( | ||
| $users->map(fn ($user) => [ | ||
| 'user_id' => $user->id, | ||
| 'policy_id' => $policyId, | ||
| 'accepted_at' => $user->created_at, | ||
| 'created_at' => $timestamp, | ||
| 'updated_at' => $timestamp, | ||
| ])->all() | ||
| ), | ||
| 'users.id', 'id'); | ||
| } | ||
|
|
||
| /** | ||
| * Reverse the migrations. | ||
| */ | ||
| public function down(): void { | ||
| DB::table('policy_acceptances') | ||
| ->where('policy_id', $this->getPolicyId()) | ||
| ->where('accepted_at', '<', self::USER_CREATED_AT_CUTOFF) | ||
| ->whereColumn('created_at', '>', 'accepted_at') | ||
| ->delete(); | ||
| } | ||
|
|
||
| /** | ||
| * @return int The policy ID of our existing terms of use. | ||
| */ | ||
| private function getPolicyId(): int { | ||
| return DB::table('policies') | ||
| ->where('policy_type', 'terms-of-use') | ||
| ->where('active_from', '2022-01-01') | ||
| ->soleValue('id'); | ||
| } | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do Not Merge until T430529 is deployed on production, which will give us an accurate cutoff timestamp.