File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed
library/oss/postgres/prepare/database Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change 5252END
5353$func$;
5454
55+ -- fn_ensure_column_not_exists is a lock-friendly replacement for `ALTER TABLE ... DROP COLUMN IF EXISTS`.
56+ -- WARNING: This function translates all names into lowercase (as plain postgres would).
57+ -- If you want to use lowercase characters, (e.g. through quotation) do not use this funtion.
58+ --
59+ -- Example usage:
60+ --
61+ -- SELECT fn_ensure_column_not_exists('testtable', 'CreatedAt');
62+ CREATE OR REPLACE FUNCTION fn_ensure_column_not_exists (tname TEXT , cname TEXT )
63+ RETURNS void
64+ LANGUAGE plpgsql AS
65+ $func$
66+ BEGIN
67+ IF EXISTS (
68+ SELECT 1 FROM information_schema .columns
69+ WHERE table_name = LOWER (tname) AND column_name = LOWER (cname)
70+ ) THEN
71+ EXECUTE ' ALTER TABLE ' || tname || ' DROP COLUMN IF EXISTS ' || cname || ' ;' ;
72+ END IF;
73+ END
74+ $func$;
75+
5576-- fn_ensure_column_not_null is a lock-friendly replacement for `ALTER TABLE ... ALTER COLUMN ... SET NOT NULL`.
5677-- WARNING: This function translates all names into lowercase (as plain postgres would).
5778-- If you want to use lowercase characters, (e.g. through quotation) do not use this funtion.
You can’t perform that action at this time.
0 commit comments