Created
June 21, 2016 22:32
-
-
Save bf/54d8516f95cea5a411af81ee3e4855d1 to your computer and use it in GitHub Desktop.
PostgreSQL Anonymous Code Block Database Migration
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
-- PostgreSQL database migration to split up one table into two smaller ones and store the new ID with the original table | |
DO $$ | |
DECLARE | |
-- vars | |
insert_id int; | |
BEGIN | |
-- do something | |
FOR r IN | |
-- fetch all slots | |
SELECT tournament_id, user_id FROM tournament_slots | |
LOOP | |
-- create event party | |
INSERT INTO event_parties (squad_id) VALUES (NULL) | |
RETURNING id INTO insert_id; | |
RAISE NOTICE 'insert_id (%)', insert_id; | |
-- insert into event party members | |
INSERT INTO event_party_members | |
(event_party_id, user_id, payment_id, | |
join_timestamp, checkin_timestamp) | |
SELECT insert_id, r.user_id, payment_id, | |
join_timestamp, checkin_timestamp | |
FROM tournament_slots | |
WHERE user_id = r.user_id | |
AND tournament_id = r.tournament_id; | |
-- update original record | |
UPDATE tournament_slots | |
SET event_party_id = insert_id | |
WHERE user_id = r.user_id | |
AND tournament_id = r.tournament_id; | |
RAISE NOTICE '.'; | |
END LOOP; | |
RETURN; | |
END$$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment