Skip to content

Instantly share code, notes, and snippets.

@matt212
Last active August 9, 2017 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matt212/ba55e46d2991786a68a4837c01b3d990 to your computer and use it in GitHub Desktop.
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
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