Skip to content

Commit

Permalink
POC: migrate data into {system_package,package_system}_data tables
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMraka committed Sep 22, 2023
1 parent be5eacc commit 97645c3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
DO $$
DECLARE
rownum INT;
total INT;
account_id INT;
sys_id BIGINT;
update_list TEXT[] := NULL;
json TEXT := '';
pkgname_id BIGINT;
BEGIN
-- order accounts so that we read from system_package by partitions
FOR rownum, total, account_id IN
SELECT row_number() over (), count(*) over (), id FROM rh_account order by hash_partition_id(id, 128), id
LOOP
RAISE NOTICE 'Migrating account % (%/%)', account_id, rownum, total;
FOR sys_id, update_list IN
SELECT system_id, array_agg(CONCAT('"', name_id, '":', COALESCE(update_data, '[]'))) as update_list
FROM system_package sp
WHERE sp.rh_account_id = account_id
GROUP BY system_id
LOOP
json := '{' || array_to_string(update_list, ',') || '}';
-- RAISE NOTICE 'system_package_data (%, %, %)', account_id, sys_id, json;
INSERT INTO system_package_data VALUES (account_id, sys_id, json::jsonb);
END LOOP;

FOR pkgname_id, update_list IN
SELECT name_id, array_agg(CONCAT('"', system_id, '":', COALESCE(update_data, '[]'))) as update_list
FROM system_package
WHERE rh_account_id = account_id
GROUP BY name_id
LOOP
json := '{' || array_to_string(update_list, ',') || '}';
-- RAISE NOTICE 'package_system_data (%, %, %)', account_id, pkgname_id, json;
INSERT INTO package_system_data VALUES (account_id, pkgname_id, json::jsonb);
END LOOP;
END LOOP;
END;
$$
;

2 changes: 1 addition & 1 deletion database_admin/schema/create_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CREATE TABLE IF NOT EXISTS schema_migrations


INSERT INTO schema_migrations
VALUES (115, false);
VALUES (116, false);

-- ---------------------------------------------------------------------------
-- Functions
Expand Down

0 comments on commit 97645c3

Please sign in to comment.