From a2252007c4f6a24fb68c89a4c0887d1fb77377b7 Mon Sep 17 00:00:00 2001 From: yaroslav-kiriak <159446370+yaroslav-kiriak@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:53:23 +0200 Subject: [PATCH] MODCONSKC-52: fix consortia data migration by adding safe insert for the repeated insertions of the same data (#128) Co-authored-by: Yaroslav Kiriak (cherry picked from commit 304f45795eefdd8641050d377e2e9f684a292a8a) --- .../db/changelog/changelog-master.xml | 1 + .../migrate-data-from-mod-consortia.xml | 18 ++ ...migrate-safely-data-from-mod-consortia.xml | 194 ++++++++++++++++++ ...migrate-tenant-data-from-mod-consortia.xml | 3 + ...te-user-tenant-data-from-mod-consortia.xml | 3 + 5 files changed, 219 insertions(+) create mode 100644 src/main/resources/db/changelog/changes/migrate-safely-data-from-mod-consortia.xml diff --git a/src/main/resources/db/changelog/changelog-master.xml b/src/main/resources/db/changelog/changelog-master.xml index 9d584562..1b8303bf 100644 --- a/src/main/resources/db/changelog/changelog-master.xml +++ b/src/main/resources/db/changelog/changelog-master.xml @@ -17,4 +17,5 @@ + diff --git a/src/main/resources/db/changelog/changes/migrate-data-from-mod-consortia.xml b/src/main/resources/db/changelog/changes/migrate-data-from-mod-consortia.xml index 8a0a83ed..382b3ade 100644 --- a/src/main/resources/db/changelog/changes/migrate-data-from-mod-consortia.xml +++ b/src/main/resources/db/changelog/changes/migrate-data-from-mod-consortia.xml @@ -5,6 +5,9 @@ http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.17.xsd"> + + SELECT 0 + DO ' @@ -26,6 +29,9 @@ + + SELECT 0 + DO ' @@ -47,6 +53,9 @@ + + SELECT 0 + DO ' @@ -68,6 +77,9 @@ + + SELECT 0 + DO ' @@ -91,6 +103,9 @@ + + SELECT 0 + DO ' @@ -112,6 +127,9 @@ + + SELECT 0 + DO ' diff --git a/src/main/resources/db/changelog/changes/migrate-safely-data-from-mod-consortia.xml b/src/main/resources/db/changelog/changes/migrate-safely-data-from-mod-consortia.xml new file mode 100644 index 00000000..c65ef1ac --- /dev/null +++ b/src/main/resources/db/changelog/changes/migrate-safely-data-from-mod-consortia.xml @@ -0,0 +1,194 @@ + + + + + + DO + ' + DECLARE + BEGIN + IF (EXISTS (SELECT 1 FROM information_schema.tables + WHERE table_schema = ''${tenantname}_mod_consortia'' AND table_name = ''consortia_configuration'')) + THEN + INSERT INTO ${tenantname}_mod_consortia_keycloak.consortia_configuration ( + id, central_tenant_id, created_by, created_date, updated_by, updated_date + ) SELECT + id, central_tenant_id, created_by, created_date, updated_by, + (CASE WHEN updated_date IS NULL THEN created_date ELSE updated_date END) as updated_date + FROM + ${tenantname}_mod_consortia.consortia_configuration + ON CONFLICT DO NOTHING; + END IF; + END; + ' LANGUAGE plpgsql; + + + + + + DO + ' + DECLARE + BEGIN + IF (EXISTS (SELECT 1 FROM information_schema.tables + WHERE table_schema = ''${tenantname}_mod_consortia'' AND table_name = ''consortium'')) + THEN + INSERT INTO ${tenantname}_mod_consortia_keycloak.consortium ( + id, name, created_by, created_date, updated_by, updated_date + ) SELECT + id, name, created_by, created_date, updated_by, + (CASE WHEN updated_date IS NULL THEN created_date ELSE updated_date END) as updated_date + FROM + ${tenantname}_mod_consortia.consortium + ON CONFLICT DO NOTHING; + END IF; + END; + ' LANGUAGE plpgsql; + + + + + + DO + ' + DECLARE + BEGIN + IF (EXISTS (SELECT 1 FROM information_schema.tables + WHERE table_schema = ''${tenantname}_mod_consortia'' AND table_name = ''pc_state'')) + THEN + INSERT INTO ${tenantname}_mod_consortia_keycloak.pc_state ( + id, status, total_records, created_by, created_date, updated_by, updated_date + ) SELECT + id, status, total_records, created_by, created_date, updated_by, + (CASE WHEN updated_date IS NULL THEN created_date ELSE updated_date END) as updated_date + FROM + ${tenantname}_mod_consortia.pc_state + ON CONFLICT DO NOTHING; + END IF; + END; + ' LANGUAGE plpgsql; + + + + + + DO + ' + DECLARE + BEGIN + IF (EXISTS (SELECT 1 FROM information_schema.tables + WHERE table_schema = ''${tenantname}_mod_consortia'' AND table_name = ''pc_tenant_request'')) + THEN + INSERT INTO ${tenantname}_mod_consortia_keycloak.pc_tenant_request ( + id, pc_id, tenant_id, status, request_url, request_payload, response, response_status_code, + completed_date, created_by, created_date, updated_by, updated_date + ) SELECT + id, pc_id, tenant_id, status, request_url, request_payload, response, response_status_code, + completed_date, created_by, created_date, updated_by, + (CASE WHEN updated_date IS NULL THEN created_date ELSE updated_date END) as updated_date + FROM + ${tenantname}_mod_consortia.pc_tenant_request + ON CONFLICT DO NOTHING; + END IF; + END; + ' LANGUAGE plpgsql; + + + + + + DO + ' + DECLARE + BEGIN + IF (EXISTS (SELECT 1 FROM information_schema.tables + WHERE table_schema = ''${tenantname}_mod_consortia'' AND table_name = ''sharing_instance'')) + THEN + INSERT INTO ${tenantname}_mod_consortia_keycloak.sharing_instance ( + id, instance_id, source_tenant_id, target_tenant_id, status, error, created_by, created_date, updated_by, updated_date + ) SELECT + id, instance_id, source_tenant_id, target_tenant_id, status, error, created_by, created_date, updated_by, + (CASE WHEN updated_date IS NULL THEN created_date ELSE updated_date END) as updated_date + FROM + ${tenantname}_mod_consortia.sharing_instance + ON CONFLICT DO NOTHING; + END IF; + END; + ' LANGUAGE plpgsql; + + + + + + DO + ' + DECLARE + BEGIN + IF (EXISTS (SELECT 1 FROM information_schema.tables + WHERE table_schema = ''${tenantname}_mod_consortia'' AND table_name = ''sharing_setting'')) + THEN + INSERT INTO ${tenantname}_mod_consortia_keycloak.sharing_setting ( + id, setting_id, tenant_id, created_by, created_date, updated_by, updated_date + ) SELECT + id, setting_id, tenant_id, created_by, created_date, updated_by, + (CASE WHEN updated_date IS NULL THEN created_date ELSE updated_date END) as updated_date + FROM + ${tenantname}_mod_consortia.sharing_setting + ON CONFLICT DO NOTHING; + END IF; + END; + ' LANGUAGE plpgsql; + + + + + + + DO + ' + DECLARE + BEGIN + IF (EXISTS (SELECT 1 FROM information_schema.tables + WHERE table_schema = ''${tenantname}_mod_consortia'' AND table_name = ''tenant'')) + THEN + INSERT INTO ${tenantname}_mod_consortia_keycloak.tenant ( + id, name, consortium_id, code, is_central, created_by, created_date, updated_by, updated_date, setup_status, is_deleted + ) SELECT + id, name, consortium_id, code, is_central, created_by, created_date, updated_by, + COALESCE(updated_date, created_date) as updated_date, setup_status::varchar, is_deleted + FROM + ${tenantname}_mod_consortia.tenant + ON CONFLICT DO NOTHING; + END IF; + END; + ' LANGUAGE plpgsql; + + + + + + DO + ' + DECLARE + BEGIN + IF (EXISTS (SELECT 1 FROM information_schema.tables + WHERE table_schema = ''${tenantname}_mod_consortia'' AND table_name = ''user_tenant'')) + THEN + INSERT INTO ${tenantname}_mod_consortia_keycloak.user_tenant ( + id, user_id, username, tenant_id, is_primary, created_by, created_date, updated_by, updated_date + ) SELECT + id, user_id, username, tenant_id, is_primary, created_by, created_date, updated_by, + (CASE WHEN updated_date IS NULL THEN created_date ELSE updated_date END) as updated_date + FROM + ${tenantname}_mod_consortia.user_tenant + ON CONFLICT DO NOTHING; + END IF; + END; + ' LANGUAGE plpgsql; + + + + diff --git a/src/main/resources/db/changelog/changes/migrate-tenant-data-from-mod-consortia.xml b/src/main/resources/db/changelog/changes/migrate-tenant-data-from-mod-consortia.xml index ad822695..8bfc02ed 100644 --- a/src/main/resources/db/changelog/changes/migrate-tenant-data-from-mod-consortia.xml +++ b/src/main/resources/db/changelog/changes/migrate-tenant-data-from-mod-consortia.xml @@ -5,6 +5,9 @@ http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.17.xsd"> + + SELECT 0 + DO ' diff --git a/src/main/resources/db/changelog/changes/migrate-user-tenant-data-from-mod-consortia.xml b/src/main/resources/db/changelog/changes/migrate-user-tenant-data-from-mod-consortia.xml index 2d19a302..7dcf8e36 100644 --- a/src/main/resources/db/changelog/changes/migrate-user-tenant-data-from-mod-consortia.xml +++ b/src/main/resources/db/changelog/changes/migrate-user-tenant-data-from-mod-consortia.xml @@ -5,6 +5,9 @@ http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.17.xsd"> + + SELECT 0 + DO '