From 01d5efa1eb76df5c8fd017a90fd793e86187e2f7 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Tue, 28 Jul 2026 23:54:31 +0530 Subject: [PATCH] Add wipe_hiu_data_for_abha.php: full M3 HIU data wipe (documents+workflows+artifacts) for fresh consent testing --- wipe_hiu_data_for_abha.php | 100 +++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 wipe_hiu_data_for_abha.php diff --git a/wipe_hiu_data_for_abha.php b/wipe_hiu_data_for_abha.php new file mode 100644 index 0000000..d666327 --- /dev/null +++ b/wipe_hiu_data_for_abha.php @@ -0,0 +1,100 @@ + (dry run) + * php83 wipe_hiu_data_for_abha.php --confirm (actually delete) + */ + +$envPath = __DIR__ . '/.env'; +if (! is_file($envPath)) { + fwrite(STDERR, "Could not find .env at {$envPath}\n"); + exit(1); +} + +$lines = file($envPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); +$env = []; +foreach ($lines as $line) { + $line = trim($line); + if ($line === '' || $line[0] === '#' || strpos($line, '=') === false) { + continue; + } + [$k, $v] = explode('=', $line, 2); + $env[trim($k)] = trim($v, " \t\n\r\0\x0B\"'"); +} + +$host = $env['database.default.hostname'] ?? 'localhost'; +$db = $env['database.default.database'] ?? ''; +$user = $env['database.default.username'] ?? ''; +$pass = $env['database.default.password'] ?? ''; +$port = (int) ($env['database.default.port'] ?? 3306); + +$mysqli = @new mysqli($host, $user, $pass, $db, $port ?: 3306); +if ($mysqli->connect_errno) { + fwrite(STDERR, "DB connect failed: " . $mysqli->connect_error . "\n"); + exit(1); +} + +echo "Connected to DB '{$db}' on {$host}.\n"; + +$abha = $argv[1] ?? ''; +$confirm = in_array('--confirm', $argv, true); + +if ($abha === '') { + fwrite(STDERR, "Usage: php83 wipe_hiu_data_for_abha.php [--confirm]\n"); + exit(1); +} + +$tables = [ + 'abdm_hiu_documents' => 'abha_address', + 'abdm_hiu_workflows' => 'abha_address', + 'abdm_hiu_consent_artifacts' => 'abha_address', +]; + +$counts = []; +foreach ($tables as $table => $column) { + $checkTable = $mysqli->query("SHOW TABLES LIKE '{$table}'"); + if (! $checkTable || $checkTable->num_rows === 0) { + echo "\n(table '{$table}' does not exist, skipping)\n"; + continue; + } + + $stmt = $mysqli->prepare("SELECT COUNT(*) AS c FROM {$table} WHERE {$column} = ?"); + $stmt->bind_param('s', $abha); + $stmt->execute(); + $row = $stmt->get_result()->fetch_assoc(); + $counts[$table] = (int) ($row['c'] ?? 0); + + echo "\n=== {$table} rows matching {$column}='{$abha}' ===\n"; + echo "Count: {$counts[$table]}\n"; +} + +$total = array_sum($counts); +if ($total === 0) { + echo "\n(no rows found in any table — nothing to delete)\n"; + exit(0); +} + +if (! $confirm) { + echo "\nDry run only. Total rows across all tables: {$total}.\n"; + echo "Re-run with --confirm to actually delete these rows.\n"; + exit(0); +} + +foreach ($tables as $table => $column) { + if (! isset($counts[$table]) || $counts[$table] === 0) { + continue; + } + $del = $mysqli->prepare("DELETE FROM {$table} WHERE {$column} = ?"); + $del->bind_param('s', $abha); + $del->execute(); + echo "\nDeleted {$mysqli->affected_rows} row(s) from {$table} for {$column}='{$abha}'.\n"; +} + +echo "\nDone. All M3 HIU data wiped for abha_address='{$abha}'. Ready for a fresh consent request test.\n";