-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MODDICONV-365: Automatically migration preparation for already migrat…
…ed envs. (#49) * MODDICONV-365: Automatically migration preparation for already migrated envs.
- Loading branch information
1 parent
4dcf7a5
commit cfcd308
Showing
9 changed files
with
146 additions
and
18 deletions.
There are no files selected for viewing
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
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
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
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
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
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
69 changes: 69 additions & 0 deletions
69
...src/main/resources/templates/db_scripts/associations-migration/clean_profile_wrappers.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,69 @@ | ||
/** | ||
To be able to check the migration results in the future. Could be remove after migration. | ||
*/ | ||
drop table if exists snapshots_old; | ||
create table snapshots_old as | ||
select job_profile_id, s.get_profile_snapshot ->> 'association_id' as association_id, s.get_profile_snapshot as snapshot | ||
from (select jp.id job_profile_id, get_profile_snapshot(jp.id, 'JOB_PROFILE', 'job_profiles', jp.id::TEXT) from job_profiles jp) s; | ||
|
||
/* | ||
Migration will start automatically when profile_wrappers table is empty. | ||
This function cleans this table: removes FKeys, disables triggers, | ||
truncates data, enables triggers and restores FKeys. | ||
*/ | ||
DO $$ | ||
DECLARE | ||
r record; | ||
BEGIN | ||
RAISE NOTICE '==='; | ||
RAISE NOTICE 'Preparing mod_di_converter_storage for migration.'; | ||
DROP TABLE IF EXISTS foreign_keys; | ||
|
||
RAISE NOTICE 'Find FKeys for deletion.'; | ||
CREATE TEMP TABLE foreign_keys AS | ||
SELECT conrelid :: regclass AS table_name, conname AS foreign_key | ||
FROM pg_constraint | ||
WHERE connamespace = (SELECT current_setting('SEARCH_PATH')) :: regnamespace | ||
AND contype = 'f' AND conrelid :: regclass :: text like '%_to_%' | ||
ORDER BY conrelid :: regclass :: text, contype desc; | ||
|
||
RAISE NOTICE 'Removing FKeys:'; | ||
FOR r in | ||
SELECT table_name, foreign_key | ||
FROM foreign_keys | ||
LOOP | ||
RAISE NOTICE ' Delete FKey:: % : %', r.table_name, r.foreign_key; | ||
EXECUTE 'ALTER TABLE ' || r.table_name || ' DROP CONSTRAINT ' || r.foreign_key || ';'; | ||
END LOOP; | ||
RAISE NOTICE 'FKeys were removed.'; | ||
|
||
RAISE NOTICE 'Disable triggers.'; | ||
SET session_replication_role = replica; | ||
|
||
RAISE NOTICE 'Removing old associations.'; | ||
FOR r in | ||
SELECT distinct table_name | ||
FROM foreign_keys | ||
LOOP | ||
EXECUTE 'UPDATE ' || r.table_name || ' SET masterwrapperid=null, detailwrapperid=null;'; | ||
END LOOP; | ||
|
||
RAISE NOTICE 'Removing old wrappers.'; | ||
TRUNCATE profile_wrappers; | ||
|
||
RAISE NOTICE 'Enabling triggers back.'; | ||
SET session_replication_role = DEFAULT; | ||
|
||
RAISE NOTICE 'Creating FKeys back:'; | ||
FOR r in | ||
SELECT table_name, foreign_key | ||
FROM foreign_keys | ||
LOOP | ||
RAISE NOTICE ' Create FKey:: % : %', r.table_name, r.foreign_key; | ||
EXECUTE 'ALTER TABLE ' || r.table_name || ' ADD CONSTRAINT ' || r.foreign_key || ' FOREIGN KEY (' || | ||
left(r.foreign_key, strpos(r.foreign_key, '_') -1)|| ') REFERENCES profile_wrappers(id) ON DELETE CASCADE;'; | ||
END LOOP; | ||
|
||
DROP TABLE IF EXISTS foreign_keys; | ||
RAISE NOTICE 'DB ready for migration.'; | ||
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
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