From 97645c3e7c5e214ec8a48811e472fa7da3411d9d Mon Sep 17 00:00:00 2001 From: Michael Mraka Date: Fri, 22 Sep 2023 15:35:06 +0200 Subject: [PATCH] POC: migrate data into {system_package,package_system}_data tables --- ..._package_data_tables_data_migration.up.sql | 41 +++++++++++++++++++ database_admin/schema/create_schema.sql | 2 +- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 database_admin/migrations/116_package_data_tables_data_migration.up.sql diff --git a/database_admin/migrations/116_package_data_tables_data_migration.up.sql b/database_admin/migrations/116_package_data_tables_data_migration.up.sql new file mode 100644 index 000000000..d57a7d56f --- /dev/null +++ b/database_admin/migrations/116_package_data_tables_data_migration.up.sql @@ -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; +$$ +; + diff --git a/database_admin/schema/create_schema.sql b/database_admin/schema/create_schema.sql index aa134dff7..2f90a28cc 100644 --- a/database_admin/schema/create_schema.sql +++ b/database_admin/schema/create_schema.sql @@ -7,7 +7,7 @@ CREATE TABLE IF NOT EXISTS schema_migrations INSERT INTO schema_migrations -VALUES (115, false); +VALUES (116, false); -- --------------------------------------------------------------------------- -- Functions