Last active
August 9, 2017 08:22
-
-
Save matt212/ba55e46d2991786a68a4837c01b3d990 to your computer and use it in GitHub Desktop.
after data migration, generate script for creating sequence and apply sequence on column in postgresql
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
select 'create sequence ' ||v.sequencename || ' owned by ' || v.table_name::text || '.' ||v.column_name::text || ';' AS createsequence, | |
'alter table ' || v.table_name::text || ' alter column ' || v.column_name::text | |
|| ' set default ' || v.column_default::text || ';' as altercolumnscript | |
from | |
(select | |
v.table_name, v.column_name, v.column_default, | |
replace(replace(replace(REPLACE(v.column_default,'nextval',''),'::regclass',''),'(''',''),''')','') | |
as sequencename | |
from | |
information_schema.columns v | |
INNER JOIN | |
( | |
SELECT table_name FROM information_schema.tables WHERE table_schema='public' | |
)a | |
on a.table_name=v.table_name | |
where column_default is not null | |
and column_default like '%next%' | |
)as v |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment