-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create experimental ability to provision Postgres helper functions
Towards NSL-5018
- Loading branch information
Showing
4 changed files
with
91 additions
and
10 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
-- Collection of optional helper functions | ||
-- To provision these functions add | ||
-- provision_helper_functions: true | ||
-- to the database intent. | ||
|
||
-- `CREATE TABLE IF NOT EXISTS` and `ALTER TABLE … ADD COLUMN IF NOT EXISTS` | ||
-- both require exclusive locks with Postgres, even if the table/column already exists. | ||
-- The functions below provide ensure semantics while only acquiring exclusive locks on mutations. | ||
|
||
-- fn_ensure_table is a lock-friendly replacement for `CREATE TABLE IF NOT EXISTS`. | ||
-- | ||
-- Example usage: | ||
-- | ||
-- SELECT fn_ensure_table('testtable', $$ | ||
-- UserID TEXT NOT NULL, | ||
-- PRIMARY KEY(UserID) | ||
-- $$); | ||
CREATE OR REPLACE FUNCTION fn_ensure_table(tname TEXT, def TEXT) | ||
RETURNS void | ||
LANGUAGE plpgsql AS | ||
$func$ | ||
BEGIN | ||
IF NOT EXISTS ( | ||
SELECT 1 FROM pg_tables | ||
WHERE schemaname = 'public' AND tablename = LOWER(tname) | ||
) THEN | ||
EXECUTE 'CREATE TABLE IF NOT EXISTS ' || tname || ' (' || def || ');'; | ||
END IF; | ||
END | ||
$func$; | ||
|
||
-- fn_ensure_column is a lock-friendly replacement for `ALTER TABLE ... ADD COLUMN IF NOT EXISTS`. | ||
-- | ||
-- Example usage: | ||
-- | ||
-- SELECT fn_ensure_column('testtable', 'CreatedAt', 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP'); | ||
CREATE OR REPLACE FUNCTION fn_ensure_column(tname TEXT, cname TEXT, def TEXT) | ||
RETURNS void | ||
LANGUAGE plpgsql AS | ||
$func$ | ||
BEGIN | ||
IF NOT EXISTS ( | ||
SELECT 1 FROM information_schema.columns | ||
WHERE table_name = LOWER(tname) AND column_name = LOWER(cname) | ||
) THEN | ||
EXECUTE 'ALTER TABLE ' || tname || ' ADD COLUMN IF NOT EXISTS ' || cname || ' ' || def; | ||
END IF; | ||
END | ||
$func$; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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