Skip to content

Commit fead5b0

Browse files
tadasantclaude
andcommitted
Add detailed logging when safety checks fail
When the migration detects unexpected data, it now logs: - Each server that would be deleted, with name, version, and reason - Each server that would have status updated, with current status This makes it easy to identify new problematic data and update the migration accordingly if production data changes. Example output on failure: ``` NOTICE: Servers that would be deleted: NOTICE: - unexpected.server/[email protected] (reason: Invalid name format) NOTICE: - another.server@ (reason: Empty version) ERROR: Safety check failed: Expected to delete exactly 5 servers but would delete 7 ``` 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 6f55476 commit fead5b0

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

internal/database/migrations/008_clean_invalid_data.sql

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ DECLARE
1212
duplicate_count INTEGER;
1313
total_to_delete INTEGER;
1414
total_servers INTEGER;
15+
r RECORD;
1516
BEGIN
1617
-- Get total server count
1718
SELECT COUNT(*) INTO total_servers FROM servers;
@@ -69,11 +70,42 @@ BEGIN
6970
-- - 5 servers to delete (1 invalid name + 4 empty versions)
7071
-- - 1 server status to update
7172
IF total_to_delete != 5 THEN
72-
RAISE EXCEPTION 'Safety check failed: Expected to delete exactly 5 servers but would delete %. Aborting to prevent data loss.', total_to_delete;
73+
-- Log the specific servers that would be deleted
74+
RAISE NOTICE 'Servers that would be deleted:';
75+
FOR r IN
76+
SELECT value->>'name' as name, value->>'version' as version,
77+
CASE
78+
WHEN value->>'name' NOT SIMILAR TO '[a-zA-Z0-9][a-zA-Z0-9.-]*[a-zA-Z0-9]/[a-zA-Z0-9][a-zA-Z0-9._-]*[a-zA-Z0-9]' THEN 'Invalid name format'
79+
WHEN value->>'version' IS NULL THEN 'NULL version'
80+
WHEN value->>'version' = '' THEN 'Empty version'
81+
END as reason
82+
FROM servers
83+
WHERE value->>'name' NOT SIMILAR TO '[a-zA-Z0-9][a-zA-Z0-9.-]*[a-zA-Z0-9]/[a-zA-Z0-9][a-zA-Z0-9._-]*[a-zA-Z0-9]'
84+
OR value->>'version' IS NULL
85+
OR value->>'version' = ''
86+
ORDER BY value->>'name'
87+
LOOP
88+
RAISE NOTICE ' - %@% (reason: %)', r.name, r.version, r.reason;
89+
END LOOP;
90+
91+
RAISE EXCEPTION 'Safety check failed: Expected to delete exactly 5 servers but would delete %. Check the log above for details. Aborting to prevent data loss.', total_to_delete;
7392
END IF;
7493

7594
IF invalid_status_count != 1 THEN
76-
RAISE EXCEPTION 'Safety check failed: Expected to update exactly 1 server status but would update %. Aborting to prevent data corruption.', invalid_status_count;
95+
-- Log the specific servers that would have status updated
96+
RAISE NOTICE 'Servers that would have status updated:';
97+
FOR r IN
98+
SELECT value->>'name' as name, value->>'version' as version, value->>'status' as status
99+
FROM servers
100+
WHERE value->>'status' IS NOT NULL
101+
AND value->>'status' != ''
102+
AND value->>'status' NOT IN ('active', 'deprecated', 'deleted')
103+
ORDER BY value->>'name'
104+
LOOP
105+
RAISE NOTICE ' - %@% (current status: %)', r.name, r.version, r.status;
106+
END LOOP;
107+
108+
RAISE EXCEPTION 'Safety check failed: Expected to update exactly 1 server status but would update %. Check the log above for details. Aborting to prevent data corruption.', invalid_status_count;
77109
END IF;
78110
END $$;
79111

0 commit comments

Comments
 (0)