Last active
August 8, 2020 15:09
Revisions
-
jrf0110 revised this gist
Jan 6, 2014 . 1 changed file with 16 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -19,6 +19,22 @@ begin end; $$ language plpgsql; -- Check constraint existence create or replace function constraint_exists( c_type text, tbl_name text, col_name text ) returns boolean as $$ begin return exists ( select 1 from information_schema.constraint_column_usage usage left join information_schema.table_constraints constraints on constraints.constraint_name = usage.constraint_name where constraints.constraint_type = c_type and usage.table_name = tbl_name and usage.column_name = col_name ); end; $$ language plpgsql; -- Add column to table create or replace function add_column( tbl_name text, col_name text, col_type text ) returns void as $$ -
jrf0110 revised this gist
Jan 6, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ -- Check for table existence create or replace function table_exists( tbl_name text ) returns boolean as $$ begin return exists ( -
jrf0110 revised this gist
Jan 6, 2014 . 1 changed file with 12 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -8,11 +8,22 @@ begin end; $$ language plpgsql; -- Check for column existence create or replace function column_exists( tbl_name text, col_name text ) returns boolean as $$ begin return exists ( select column_name from information_schema.columns where table_name = tbl_name and column_name = col_name ); end; $$ language plpgsql; -- Add column to table create or replace function add_column( tbl_name text, col_name text, col_type text ) returns void as $$ begin if exists ( select * from column_exists( tbl_name, col_name ) where column_exists = false ) then raise notice 'Adding column `%` to table `%`', col_name, tbl_name; execute 'alter table "' || tbl_name || '" add column "' || col_name || '" ' || col_type; end if; -
jrf0110 revised this gist
Jan 6, 2014 . 1 changed file with 10 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,13 @@ -- Check for table existence create or replace function table_exists ( tbl_name text ) returns boolean as $$ begin return exists ( select 1 from pg_catalog.pg_tables where tablename = tbl_name ); end; $$ language plpgsql; -- Add column to table create or replace function add_column( tbl_name text, col_name text, col_type text ) returns void as $$ -
jrf0110 created this gist
Jan 6, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ -- Add column to table create or replace function add_column( tbl_name text, col_name text, col_type text ) returns void as $$ begin if not exists ( select 1 from information_schema.columns where table_name = tbl_name and column_name = col_name ) then raise notice 'Adding column `%` to table `%`', col_name, tbl_name; execute 'alter table "' || tbl_name || '" add column "' || col_name || '" ' || col_type; end if; end; $$ language plpgsql; -- Add type create or replace function add_type( type_name text, type_def text ) returns void as $$ begin if not exists ( select 1 from pg_type where typname = type_name ) then raise notice 'Adding Type `%`', type_name; execute 'create type ' || type_name || ' as ' || type_def; end if; end; $$ language plpgsql;