diff --git a/script/qa/cleanup-proj16-testdata.php b/script/qa/cleanup-proj16-testdata.php new file mode 100644 index 0000000..413326d --- /dev/null +++ b/script/qa/cleanup-proj16-testdata.php @@ -0,0 +1,84 @@ +." . PHP_EOL); + exit(2); + } +} + +$targetNames = ['qa_proj16_sender', 'qa_proj16_recipient']; + +$users = User::where('name IN (?, ?)', $targetNames[0], $targetNames[1])->take(); +$userIds = []; + +foreach ($users as $user) { + $userIds[] = (int)$user->id; +} + +if (empty($userIds)) { + echo "No PROJ-16 test users found (qa_proj16_sender/qa_proj16_recipient)." . PHP_EOL; + exit(0); +} + +$summary = [ + 'user_ids' => $userIds, + 'user_count' => User::where('id IN (?)', $userIds)->count(), + 'dmail_count' => Dmail::where( + $dmailCondition, + $userIds, + $userIds, + 'PROJ16 QA%' + )->count(), +]; + +echo "PROJ-16 cleanup summary:" . PHP_EOL; +echo json_encode($summary, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL; + +if ($dryRun) { + echo "Dry run only, no data was deleted." . PHP_EOL; + exit(0); +} + +try { + User::transaction(function() use ($userIds, $dmailCondition) { + // Remove QA dmails first to avoid orphaned rows. + Dmail::destroyAll($dmailCondition, $userIds, $userIds, 'PROJ16 QA%'); + + // Remove user-related rows if present in this environment. + foreach (['UserBlacklistedTag', 'UserLog', 'PostVote', 'Favorite', 'Ban'] as $optionalModelClass) { + if (class_exists($optionalModelClass)) { + $optionalModelClass::destroyAll('user_id IN (?)', $userIds); + } + } + + User::destroyAll('id IN (?)', $userIds); + }); +} catch (Throwable $e) { + fwrite(STDERR, "Cleanup failed: " . $e->getMessage() . PHP_EOL); + exit(1); +} + +echo "Cleanup completed." . PHP_EOL;