chore(ops): test-data cleanup script and runbook
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
-- One-off: delete ALL incidents (and children) and every user with role <> 'admin'.
|
||||
-- Usage: psql -p 5433 -d ims -v mode=dryrun -f cleanup-test-data.sql (counts only)
|
||||
-- psql -p 5433 -d ims -v mode=run -f cleanup-test-data.sql (deletes, one transaction)
|
||||
-- Evidence files on disk are NOT touched (5-year retention).
|
||||
|
||||
\if :{?mode}
|
||||
\else
|
||||
\echo 'Set -v mode=dryrun or -v mode=run'
|
||||
\quit
|
||||
\endif
|
||||
|
||||
BEGIN;
|
||||
|
||||
\echo '--- admins kept ---'
|
||||
SELECT id, name, email FROM users WHERE role = 'admin' ORDER BY email;
|
||||
|
||||
\echo '--- row counts to delete ---'
|
||||
SELECT 'incidents' AS tbl, count(*) FROM incidents
|
||||
UNION ALL SELECT 'evidence_files', count(*) FROM evidence_files
|
||||
UNION ALL SELECT 'investigations', count(*) FROM investigations
|
||||
UNION ALL SELECT 'capa_actions', count(*) FROM capa_actions
|
||||
UNION ALL SELECT 'dosh_reports', count(*) FROM dosh_reports
|
||||
UNION ALL SELECT 'incident_addenda', count(*) FROM incident_addenda
|
||||
UNION ALL SELECT 'notifications_log', count(*) FROM notifications_log
|
||||
UNION ALL SELECT 'audit_log (all)', count(*) FROM audit_log
|
||||
UNION ALL SELECT 'users (non-admin)', count(*) FROM users WHERE role <> 'admin';
|
||||
|
||||
SELECT (:'mode' = 'run') AS do_delete \gset
|
||||
\if :do_delete
|
||||
-- Closed incidents are locked by triggers; disable them for this transaction only.
|
||||
ALTER TABLE incidents DISABLE TRIGGER incidents_closed_lock_delete;
|
||||
ALTER TABLE incidents DISABLE TRIGGER incidents_closed_lock_update;
|
||||
|
||||
DELETE FROM notifications_log;
|
||||
DELETE FROM incident_addenda;
|
||||
DELETE FROM dosh_reports;
|
||||
DELETE FROM evidence_files;
|
||||
DELETE FROM capa_actions;
|
||||
DELETE FROM investigations;
|
||||
DELETE FROM incidents;
|
||||
|
||||
-- audit_log: remove rows about deleted incident-side tables and about non-admin users.
|
||||
DELETE FROM audit_log
|
||||
WHERE table_name IN ('incidents','evidence_files','investigations','capa_actions','dosh_reports','incident_addenda')
|
||||
OR changed_by IN (SELECT id FROM users WHERE role <> 'admin')
|
||||
OR (table_name = 'users' AND record_id IN (SELECT id FROM users WHERE role <> 'admin'));
|
||||
|
||||
UPDATE app_settings SET updated_by = NULL
|
||||
WHERE updated_by IN (SELECT id FROM users WHERE role <> 'admin');
|
||||
|
||||
DELETE FROM password_reset_tokens WHERE user_id IN (SELECT id FROM users WHERE role <> 'admin');
|
||||
DELETE FROM users WHERE role <> 'admin';
|
||||
|
||||
ALTER TABLE incidents ENABLE TRIGGER incidents_closed_lock_delete;
|
||||
ALTER TABLE incidents ENABLE TRIGGER incidents_closed_lock_update;
|
||||
|
||||
\echo '--- after ---'
|
||||
SELECT 'incidents' AS tbl, count(*) FROM incidents
|
||||
UNION ALL SELECT 'users', count(*) FROM users
|
||||
UNION ALL SELECT 'admins', count(*) FROM users WHERE role = 'admin';
|
||||
COMMIT;
|
||||
\else
|
||||
ROLLBACK;
|
||||
\echo 'dry run: nothing changed'
|
||||
\endif
|
||||
@@ -0,0 +1,24 @@
|
||||
# Cleanup: delete test incidents and non-admin users
|
||||
|
||||
Owner go-ahead required before step 4. Never run during business hours without telling HSE.
|
||||
|
||||
1. **Backup** (on the server, `ssh -p 9321 setia@175.136.239.121`):
|
||||
```bash
|
||||
D=$(date +%Y%m%d-%H%M)
|
||||
pg_dump -p 5433 -Fc ims > /srv/apps/ims/backups/ims-pre-cleanup-$D.dump
|
||||
tar -czf /srv/apps/ims/backups/evidence-pre-cleanup-$D.tgz /var/lib/ims-evidence
|
||||
ls -lh /srv/apps/ims/backups/*pre-cleanup-$D*
|
||||
```
|
||||
Create `/srv/apps/ims/backups` first if missing. Both files must be non-empty.
|
||||
2. **Upload** `db/ops/cleanup-test-data.sql` to `/srv/apps/ims/backups/` (not into `app/`).
|
||||
3. **Dry run** as the DB owner:
|
||||
```bash
|
||||
psql -p 5433 -d ims -v mode=dryrun -f /srv/apps/ims/backups/cleanup-test-data.sql
|
||||
```
|
||||
Send the admins list and the row counts to the owner. Stop here until they confirm.
|
||||
4. **Run** after explicit confirmation:
|
||||
```bash
|
||||
psql -p 5433 -d ims -v ON_ERROR_STOP=1 -v mode=run -f /srv/apps/ims/backups/cleanup-test-data.sql
|
||||
```
|
||||
5. **Verify**: `systemctl is-active ims` is `active`; log in as an admin at https://ims.setia.com.my/login; Incidents page is empty; Users page lists only admins.
|
||||
6. **Rollback** (only if wrong): `pg_restore -p 5433 -d ims --clean --if-exists <dump>`; evidence files were never touched.
|
||||
@@ -32,17 +32,16 @@ Making zone optional; a "General" zone per site.
|
||||
- Allowed: CAPA owner, or hse/admin. Status must be `in_progress`; otherwise 409.
|
||||
- Validates note non-empty and 1 to 5 image files; otherwise 422.
|
||||
- In one `withUser` transaction: store files via `lib/storage/evidence.ts`, insert `evidence_files` rows (`stage = 'capa'`, `capa_id` set, hash recorded), set `owner_notes`, `completed_at`, `status = 'pending_verification'`, and write `audit_log` (include file ids and the note).
|
||||
- Existing in-app notification to HSE on submission is kept.
|
||||
- `PATCH /api/capa/[id]` rejects `status: 'pending_verification'` with 403 for everyone (the new endpoint is the only path). Owner `in_progress` transition is unchanged.
|
||||
- CAPA detail page: "Completion report" section shows the note and a photo gallery (signed `/api/evidence/...` URLs) so HSE sees proof when verifying or reopening.
|
||||
- Evidence rows are never hard-deleted (5-year retention).
|
||||
|
||||
### Schema change (manual, needs owner go-ahead)
|
||||
```sql
|
||||
ALTER TABLE evidence_files ADD COLUMN capa_id uuid REFERENCES capa_actions(id);
|
||||
CREATE INDEX evidence_files_capa_id_idx ON evidence_files(capa_id);
|
||||
ALTER TABLE evidence_files ADD COLUMN IF NOT EXISTS capa_id UUID REFERENCES capa_actions(id) ON DELETE RESTRICT;
|
||||
CREATE INDEX IF NOT EXISTS evidence_files_capa_id_idx ON evidence_files(capa_id);
|
||||
```
|
||||
The `evidence_stage` enum already has `capa`; no enum change. Update `db/schema.sql`, `lib/db/schema.ts`, and check the `evidence_*` RLS policies in `db/rls.sql` cover CAPA owners (insert) and hse/admin (read); add a read policy for the CAPA owner if missing.
|
||||
The `evidence_stage` enum already has `capa`; no enum change. Update `db/schema.sql`, `lib/db/schema.ts`, no RLS change: the PIC is the uploader and already reads own rows.
|
||||
|
||||
### Out of scope
|
||||
Photos on "Mark In Progress"; HSE-side evidence upload; video.
|
||||
|
||||
Reference in New Issue
Block a user