Skip to content

Instantly share code, notes, and snippets.

@NerdGr8
Last active July 11, 2023 10:28
Show Gist options
  • Save NerdGr8/000fe982fe6731585a561f451a257761 to your computer and use it in GitHub Desktop.
Save NerdGr8/000fe982fe6731585a561f451a257761 to your computer and use it in GitHub Desktop.
Generates Alter Columns statements to change any uncompliant columns to postgress naming conventions.
#Run teh following in the terminal:
Install-Package Npgsql.EntityFrameworkCore.PostgreSQL
-- Generate the SQL Statement required to conform All Columns in the Schema.
-- Will pick up and columns ie:
-- CreatedDate => created
-- Created => created
-- Created Date => created_date
SELECT FORMAT(
'ALTER TABLE %I.%I.%I RENAME COLUMN %I TO %I;',
table_catalog,
table_schema,
table_name,
column_name,
lower(
-- replace all spaces with _, xX and Xx becomes x_x
regexp_replace(
-- First, replace spaces with an _
replace(column_name, ' ', '_'),
'([[:lower:]])([[:upper:]])',
'\1_\2',
'g'
)
)
)
FROM information_schema.columns
WHERE column_name ~ ' |[[:lower:]][[:upper:]]' OR column_name ~ '^[A-Z]';
Add-Migration InitialCreate
Update-Database
//Change the following
services.AddDbContext<YourContext>(options =>
options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment