Skip to content

Commit

Permalink
MODCONSKC-52: fix consortia data migration by adding safe insert for …
Browse files Browse the repository at this point in the history
…the repeated insertions of the same data (#128)

Co-authored-by: Yaroslav Kiriak <[email protected]>
(cherry picked from commit 304f457)
  • Loading branch information
yaroslav-kiriak authored and yaroslav-epam committed Dec 5, 2024
1 parent 1fbb55a commit a225200
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/resources/db/changelog/changelog-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
<include file="changes/create-sharing-role-table.xml" relativeToChangelogFile="true"/>
<include file="changes/migrate-tenant-data-from-mod-consortia.xml" relativeToChangelogFile="true"/>
<include file="changes/migrate-user-tenant-data-from-mod-consortia.xml" relativeToChangelogFile="true"/>
<include file="changes/migrate-safely-data-from-mod-consortia.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.17.xsd">

<changeSet id="EUREKA-65@@migrate-data-from-mod_consortia.consortia_configuration" author="yaroslavkiriak">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="1">SELECT 0</sqlCheck>
</preConditions>
<sql>
DO
'
Expand All @@ -26,6 +29,9 @@
</changeSet>

<changeSet id="EUREKA-65@@migrate-data-from-mod_consortia.consortium" author="yaroslavkiriak">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="1">SELECT 0</sqlCheck>
</preConditions>
<sql>
DO
'
Expand All @@ -47,6 +53,9 @@
</changeSet>

<changeSet id="EUREKA-65@@migrate-data-from-mod_consortia.pc_state" author="yaroslavkiriak">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="1">SELECT 0</sqlCheck>
</preConditions>
<sql>
DO
'
Expand All @@ -68,6 +77,9 @@
</changeSet>

<changeSet id="EUREKA-65@@migrate-data-from-mod_consortia.pc_tenant_request" author="yaroslavkiriak">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="1">SELECT 0</sqlCheck>
</preConditions>
<sql>
DO
'
Expand All @@ -91,6 +103,9 @@
</changeSet>

<changeSet id="EUREKA-65@@migrate-data-from-mod_consortia.sharing_instance" author="yaroslavkiriak">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="1">SELECT 0</sqlCheck>
</preConditions>
<sql>
DO
'
Expand All @@ -112,6 +127,9 @@
</changeSet>

<changeSet id="EUREKA-65@@migrate-data-from-mod_consortia.sharing_setting" author="yaroslavkiriak">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="1">SELECT 0</sqlCheck>
</preConditions>
<sql>
DO
'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.17.xsd">

<changeSet id="MODCONSKC-52@@migrate-data-from-mod_consortia.consortia_configuration" author="yaroslavkiriak">
<sql>
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;
</sql>
</changeSet>

<changeSet id="MODCONSKC-52@@migrate-data-from-mod_consortia.consortium" author="yaroslavkiriak">
<sql>
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;
</sql>
</changeSet>

<changeSet id="MODCONSKC-52@@migrate-data-from-mod_consortia.pc_state" author="yaroslavkiriak">
<sql>
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;
</sql>
</changeSet>

<changeSet id="MODCONSKC-52@@migrate-data-from-mod_consortia.pc_tenant_request" author="yaroslavkiriak">
<sql>
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;
</sql>
</changeSet>

<changeSet id="MODCONSKC-52@@migrate-data-from-mod_consortia.sharing_instance" author="yaroslavkiriak">
<sql>
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;
</sql>
</changeSet>

<changeSet id="MODCONSKC-52@@migrate-data-from-mod_consortia.sharing_setting" author="yaroslavkiriak">
<sql>
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;
</sql>
</changeSet>


<changeSet id="MODCONSKC-52@@migrate-tenant-data-from-mod_consortia.tenant" author="yaroslavkiriak">
<sql>
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;
</sql>
</changeSet>

<changeSet id="MODCONSKC-52@@migrate-user-tenant-data-from-mod_consortia" author="yaroslavkiriak">
<sql>
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;
</sql>
</changeSet>

</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.17.xsd">

<changeSet id="MODCONSKC-40@@migrate-tenant-data-from-mod_consortia.tenant" author="yaroslavkiriak">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="1">SELECT 0</sqlCheck>
</preConditions>
<sql>
DO
'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.17.xsd">

<changeSet id="MODCONSKC-40@@migrate-user-tenant-data-from-mod_consortia" author="yaroslavkiriak">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="1">SELECT 0</sqlCheck>
</preConditions>
<sql>
DO
'
Expand Down

0 comments on commit a225200

Please sign in to comment.