Last active
May 22, 2017 13:17
-
-
Save matt212/40e61db1a1ee3fc50f05f336d0737475 to your computer and use it in GitHub Desktop.
after mysql migration to postgres restarting primary key sequences and most importantly retaining older data for referential integrity aspect !
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
--create new column to create sequences | |
alter table modnames add id serial; | |
---update new column serial column with existing value | |
UPDATE modnames m | |
SET id = m0.modnameid | |
FROM modnames m0 | |
where m.modnameid=m0.modnameid | |
---now tricky part is to tell new serial to start from max(existingid) for referential integrity | |
--scenarios | |
---to figure out your serial sequence | |
SELECT adsrc FROM pg_attrdef WHERE adrelid = (SELECT oid FROM pg_class WHERE relname = 'table name goes here'); | |
--and apply new set of serial sequences | |
Select setval('modnames_id_seq', (select max(modnameid)+1 from modname), false) | |
--test you applications | |
INSERT INTO public.modnames | |
(mname, modnameid, createdat, updatedat, recordstate) | |
VALUES('new entry set ', 0, now(), now(), 'active'::character varying); | |
--to find serial sequences | |
--delete old column and rename the older to new one | |
ALTER TABLE modnames RENAME COLUMN id TO modnameid; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment