Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use separate PostgreSQL schemas for sprocs (#4412)
* Add schema support to sql-db * Add random schema setting * Fix function definitions * Fix substring() name * Change replaceAll() -> replace() * Fix module.export(s) * Change to underscores for schema name * Actually create the new schema * Fix an actual bug found by the linter! * Only create schema if it doesn't exist * Use client.escapeIdentifier() for schema * Document use of done() * Document length limit of schema prefix * Fix setting with null schema * Fix schema prefix truncation * Fix to create non-null schema before using in search_path * Shift new functions to end-of-file * Force schema name to lowercase for convenience with psql * Fix sprocs/array_and_number.sql to not check EXISTS * Rewrite query() to use queryWithClient() * Add docs and comments about our schema system * Clarify "local" in the docs Co-authored-by: Nathan Walters <nathan@prairielearn.com> * Rewrite type creation comment Co-authored-by: Nathan Walters <nathan@prairielearn.com> * More comment updates about type creation * Change all sprocs to plain CREATE * Fix tests to actually use setRandomSearchSchema() * Remove sprocs/random_string.sql because migrations make it in public schema * Use more character types in setRandomSearchSchema() Co-authored-by: Nathan Walters <nathan@prairielearn.com>
- Loading branch information
1 parent
27e279f
commit d5c37b3
Showing
158 changed files
with
304 additions
and
422 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
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
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
CREATE OR REPLACE FUNCTION | ||
CREATE FUNCTION | ||
access_tokens_delete( | ||
id bigint, | ||
user_id bigint | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
CREATE OR REPLACE FUNCTION | ||
CREATE FUNCTION | ||
access_tokens_insert( | ||
user_id bigint, | ||
name text, | ||
|
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
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
CREATE OR REPLACE FUNCTION | ||
CREATE FUNCTION | ||
administrators_insert_by_user_uid( | ||
uid text, | ||
authn_user_id bigint | ||
|
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 |
---|---|---|
@@ -1,7 +1,10 @@ | ||
--create types | ||
DO $$ | ||
BEGIN | ||
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'array_and_number') THEN | ||
CREATE TYPE array_and_number AS (arr DOUBLE PRECISION[], number INTEGER); | ||
END IF; | ||
END$$; | ||
-- Ideally, we'd use something like `CREATE TYPE IF NOT EXISTS` here, but Postgres doesn't offer that. | ||
-- We used to approximate this by checking `IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = ...)`. | ||
-- However, now that we're creating sprocs and types in a local per-process schema, this would no longer work, | ||
-- as the type would exist in `pg_type` but not necessarily in the schemas on the search path. | ||
-- So, instead, we blindly create the type without checking if it first exists, as it should never exist since we're | ||
-- creating it in a fresh, empty schema. If we needed to check, a good query is: | ||
-- DO $$ BEGIN CREATE TYPE my_type AS (...); EXCEPTION WHEN duplicate_object THEN null; END $$; | ||
-- See https://stackoverflow.com/questions/7624919/check-if-a-user-defined-type-already-exists-in-postgresql/48382296#48382296 | ||
|
||
CREATE TYPE array_and_number AS (arr DOUBLE PRECISION[], number INTEGER); |
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
CREATE OR REPLACE FUNCTION | ||
CREATE FUNCTION | ||
array_dot( | ||
IN x DOUBLE PRECISION[], | ||
IN y DOUBLE PRECISION[], | ||
|
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
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
CREATE OR REPLACE FUNCTION | ||
CREATE FUNCTION | ||
array_product( | ||
IN x DOUBLE PRECISION[], | ||
IN y DOUBLE PRECISION[], | ||
|
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
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
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
Oops, something went wrong.