Forked from estysdesu/create_constraint_if_not_exists.sql
Created
March 17, 2023 09:25
-
-
Save concosminx/e58bb10cdee4a6821b4cc8c8dbe16bec to your computer and use it in GitHub Desktop.
Revisions
-
estysdesu revised this gist
Jul 21, 2019 . No changes.There are no files selected for viewing
-
estysdesu revised this gist
Jul 21, 2019 . 1 changed file with 2 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,5 @@ // vim: syntax=sql CREATE OR REPLACE FUNCTION create_constraint_if_not_exists (t_name text, c_name text, constraint_sql text) RETURNS void AS -
estysdesu renamed this gist
Jul 19, 2019 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
estysdesu revised this gist
May 7, 2019 . No changes.There are no files selected for viewing
-
estysdesu revised this gist
May 7, 2019 . No changes.There are no files selected for viewing
-
estysdesu created this gist
May 7, 2019 .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,16 @@ CREATE OR REPLACE FUNCTION create_constraint_if_not_exists (t_name text, c_name text, constraint_sql text) RETURNS void AS $BODY$ BEGIN -- Look for our constraint IF NOT EXISTS (SELECT constraint_name FROM information_schema.constraint_column_usage WHERE constraint_name = c_name) THEN EXECUTE 'ALTER TABLE ' || t_name || ' ADD CONSTRAINT ' || c_name || ' ' || constraint_sql; END IF; END; $BODY$ LANGUAGE plpgsql VOLATILE; SELECT create_constraint_if_not_exists('foo', 'bar', 'CHECK (foobies < 100);');