Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
DO $$
DECLARE
rec RECORD;
BEGIN
IF NOT EXISTS (
SELECT 1 FROM information_schema.views
WHERE table_schema = 'inventory' AND table_name = 'hosts'
) THEN
RAISE NOTICE 'inventory.hosts view does not exist, skipping migration';
RETURN;
END IF;

FOR rec IN
SELECT
sp.id AS sp_id,
sp.inventory_id,
ih.system_profile->>'owner_id' AS ih_owner_id
FROM system_platform sp
JOIN inventory.hosts ih ON sp.inventory_id = ih.id
WHERE sp.owner_id IS NULL
AND ih.system_profile->>'owner_id' IS NOT NULL
ORDER BY sp.id
LOOP
BEGIN
UPDATE system_platform SET
owner_id = (rec.ih_owner_id)::UUID
WHERE id = rec.sp_id;
EXCEPTION WHEN OTHERS THEN
RAISE WARNING 'Migration failed for inventory_id=%, owner_id=%: %', rec.inventory_id, rec.ih_owner_id, SQLERRM;
Comment on lines +28 to +29
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Catching WHEN OTHERS globally can hide non-casting issues

EXCEPTION WHEN OTHERS turns all failures (including permissions, constraints, etc.) into warnings, so the migration can falsely appear successful. If you only expect cast issues, narrow this to specific errors like invalid_text_representation (and related cast errors) so unexpected problems still fail the migration while bad owner_id values are logged and handled explicitly.

END;
END LOOP;
END $$;
2 changes: 1 addition & 1 deletion database/schema/ve_db_postgresql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ CREATE TABLE IF NOT EXISTS db_version (
) TABLESPACE pg_default;

-- set the schema version directly in the insert statement here!!
INSERT INTO db_version (name, version) VALUES ('schema_version', 163);
INSERT INTO db_version (name, version) VALUES ('schema_version', 164);
-- INSERT INTO db_version (name, version) VALUES ('schema_version', :schema_version);


Expand Down
Loading