diff --git a/sql/test_factory--0.5.0.sql b/sql/test_factory--0.5.0.sql index 636bcb5..340e33f 100644 --- a/sql/test_factory--0.5.0.sql +++ b/sql/test_factory--0.5.0.sql @@ -1,7 +1,11 @@ /* DO NOT EDIT - AUTO-GENERATED FILE */ -CREATE TEMP TABLE original_role ON COMMIT DROP AS SELECT current_user AS original_role; - -GRANT SELECT ON pg_temp.original_role TO public; +/* + * Save the caller's role so we can restore it at the end (we SET LOCAL ROLE + * below to own our objects). A GUC is used instead of a temp table not to + * avoid CREATE EXTENSION breakage (trivial to avoid either way) but because + * it's much lighter weight than creating a table. + */ +SELECT pg_catalog.set_config('test_factory.original_role', current_user, true); DO $body$ BEGIN CREATE ROLE test_factory__owner; @@ -250,15 +254,11 @@ END $body$; --select (tf.get('moo','moo')::moo).*; +-- Restore the caller's role saved at the top of this script. DO $body$ -DECLARE - c_sql CONSTANT text := 'SET ROLE ' || (SELECT original_role FROM pg_temp.original_role); BEGIN - --RAISE WARNING 'c_sql = %', c_sql; - EXECUTE c_sql; + EXECUTE 'SET ROLE ' || pg_catalog.quote_ident(pg_catalog.current_setting('test_factory.original_role')); END $body$; -DROP TABLE pg_temp.original_role; - -- vi: expandtab ts=2 sw=2 diff --git a/sql/test_factory.sql b/sql/test_factory.sql index 3faf586..c44b076 100644 --- a/sql/test_factory.sql +++ b/sql/test_factory.sql @@ -1,6 +1,10 @@ -CREATE TEMP TABLE original_role ON COMMIT DROP AS SELECT current_user AS original_role; - -GRANT SELECT ON pg_temp.original_role TO public; +/* + * Save the caller's role so we can restore it at the end (we SET LOCAL ROLE + * below to own our objects). A GUC is used instead of a temp table not to + * avoid CREATE EXTENSION breakage (trivial to avoid either way) but because + * it's much lighter weight than creating a table. + */ +SELECT pg_catalog.set_config('test_factory.original_role', current_user, true); DO $body$ BEGIN CREATE ROLE test_factory__owner; @@ -249,15 +253,11 @@ END $body$; --select (tf.get('moo','moo')::moo).*; +-- Restore the caller's role saved at the top of this script. DO $body$ -DECLARE - c_sql CONSTANT text := 'SET ROLE ' || (SELECT original_role FROM pg_temp.original_role); BEGIN - --RAISE WARNING 'c_sql = %', c_sql; - EXECUTE c_sql; + EXECUTE 'SET ROLE ' || pg_catalog.quote_ident(pg_catalog.current_setting('test_factory.original_role')); END $body$; -DROP TABLE pg_temp.original_role; - -- vi: expandtab ts=2 sw=2 diff --git a/sql/test_factory_pgtap--0.1.0.sql b/sql/test_factory_pgtap--0.1.0.sql index 67e2a94..9eea098 100644 --- a/sql/test_factory_pgtap--0.1.0.sql +++ b/sql/test_factory_pgtap--0.1.0.sql @@ -1,6 +1,11 @@ /* DO NOT EDIT - AUTO-GENERATED FILE */ -CREATE TEMP TABLE original_role ON COMMIT DROP AS SELECT current_user AS original_role; -GRANT SELECT ON pg_temp.original_role TO public; +/* + * Save the caller's role so we can restore it at the end (we SET LOCAL ROLE + * below to own our objects). A GUC is used instead of a temp table not to + * avoid CREATE EXTENSION breakage (trivial to avoid either way) but because + * it's much lighter weight than creating a table. + */ +SELECT pg_catalog.set_config('test_factory_pgtap.original_role', current_user, true); SET LOCAL ROLE test_factory__owner; @@ -26,16 +31,11 @@ BEGIN END $body$; --- Set role back to original value +-- Set role back to original value (saved at the top of this script). DO $body$ -DECLARE - c_sql CONSTANT text := 'SET ROLE ' || (SELECT original_role FROM pg_temp.original_role); BEGIN - --RAISE WARNING 'c_sql = %', c_sql; - EXECUTE c_sql; + EXECUTE 'SET ROLE ' || pg_catalog.quote_ident(pg_catalog.current_setting('test_factory_pgtap.original_role')); END $body$; -DROP TABLE pg_temp.original_role; - -- vi: expandtab ts=2 sw=2 diff --git a/sql/test_factory_pgtap.sql b/sql/test_factory_pgtap.sql index 5a8cae4..4056d14 100644 --- a/sql/test_factory_pgtap.sql +++ b/sql/test_factory_pgtap.sql @@ -1,5 +1,10 @@ -CREATE TEMP TABLE original_role ON COMMIT DROP AS SELECT current_user AS original_role; -GRANT SELECT ON pg_temp.original_role TO public; +/* + * Save the caller's role so we can restore it at the end (we SET LOCAL ROLE + * below to own our objects). A GUC is used instead of a temp table not to + * avoid CREATE EXTENSION breakage (trivial to avoid either way) but because + * it's much lighter weight than creating a table. + */ +SELECT pg_catalog.set_config('test_factory_pgtap.original_role', current_user, true); SET LOCAL ROLE test_factory__owner; @@ -25,16 +30,11 @@ BEGIN END $body$; --- Set role back to original value +-- Set role back to original value (saved at the top of this script). DO $body$ -DECLARE - c_sql CONSTANT text := 'SET ROLE ' || (SELECT original_role FROM pg_temp.original_role); BEGIN - --RAISE WARNING 'c_sql = %', c_sql; - EXECUTE c_sql; + EXECUTE 'SET ROLE ' || pg_catalog.quote_ident(pg_catalog.current_setting('test_factory_pgtap.original_role')); END $body$; -DROP TABLE pg_temp.original_role; - -- vi: expandtab ts=2 sw=2