-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
POC: migrate data into {system_package,package_system}_data tables
- Loading branch information
1 parent
be5eacc
commit 97645c3
Showing
2 changed files
with
42 additions
and
1 deletion.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
database_admin/migrations/116_package_data_tables_data_migration.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
$$ | ||
; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters