Created
August 19, 2018 15:41
-
-
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.
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
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