diff --git a/database/migrations/2026_07_08_080249_create_initial_terms_of_use_policy.php b/database/migrations/2026_07_08_080249_create_initial_terms_of_use_policy.php new file mode 100644 index 00000000..77f392a5 --- /dev/null +++ b/database/migrations/2026_07_08_080249_create_initial_terms_of_use_policy.php @@ -0,0 +1,36 @@ +insert([ + 'policy_type' => self::POLICY_TYPE, + 'active_from' => self::ACTIVE_FROM, + 'content_vue_file' => 'terms-of-use/version-1.vue', + 'created_at' => $now, + 'updated_at' => $now, + ]); + } + + /** + * Reverse the migrations. + */ + public function down(): void { + DB::table(self::TABLE_NAME)->where([ + 'policy_type' => self::POLICY_TYPE, + 'active_from' => self::ACTIVE_FROM, + ])->delete(); + } +}; diff --git a/database/migrations/2026_07_13_075347_backfill_policy_acceptance_existing_users.php b/database/migrations/2026_07_13_075347_backfill_policy_acceptance_existing_users.php new file mode 100644 index 00000000..c48bbc27 --- /dev/null +++ b/database/migrations/2026_07_13_075347_backfill_policy_acceptance_existing_users.php @@ -0,0 +1,61 @@ +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'); + } +};