Skip to content

Instantly share code, notes, and snippets.

@eliashussary
Created August 19, 2018 15:41
Show Gist options
  • Save eliashussary/29fe7f6653dcac025662fd4bc79f71a0 to your computer and use it in GitHub Desktop.
Save eliashussary/29fe7f6653dcac025662fd4bc79f71a0 to your computer and use it in GitHub Desktop.
Script to generate alter statements for renaming table columns from camelCase to snake_case.
WITH from_table AS (
SELECT unnest(array[
-- tables go here
-- start
'bookSessions',
'bookTimeslots',
'bookUnits',
'carts',
'errorLogs',
'files',
'likes',
'listingSubscriptions',
'places',
'profiles',
'rets',
'searchOptions',
'settings',
'subscriptionEvents',
'tokens',
'uiEvents',
'units',
'userAttributes',
'users'
-- end
])::text AS name
)
SELECT 'ALTER TABLE "' || f.name || '" RENAME COLUMN "' || cols.column_name || '" TO "' || cols.regular_pgstyle || '";' AS stmt
FROM (SELECT column_name,
lower(regexp_replace(column_name, E'([A-Z])', E'\_\\1','g')) As regular_pgstyle
FROM information_schema.columns,
from_table f
WHERE table_name = f.name) cols,
from_table f
WHERE cols.column_name != cols.regular_pgstyle;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment