-
Notifications
You must be signed in to change notification settings - Fork 6
ENG-1718 A database table to pass arbitrary payload between processes #1009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
maparent
merged 2 commits into
main
from
eng-1718-a-database-table-to-pass-arbitrary-payload-between-processes
May 10, 2026
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 characters
50 changes: 50 additions & 0 deletions
50
packages/database/supabase/migrations/20260507150056_database_secret_tokens.sql
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 characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| CREATE TABLE IF NOT EXISTS public.secret_token ( | ||
| id varchar PRIMARY KEY DEFAULT encode(extensions.gen_random_bytes(12), 'base64'), | ||
| creator UUID NOT NULL DEFAULT auth.uid(), | ||
| payload JSONB NOT NULL, | ||
| expiry_date timestamp without time zone, | ||
| one_time_use boolean DEFAULT true | ||
| ); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS secret_token_expiry_idx ON public.secret_token (expiry_date) WHERE expiry_date IS NOT null; | ||
|
|
||
| CREATE OR REPLACE FUNCTION public.create_secret_token(v_payload JSONB, v_one_time_use BOOLEAN DEFAULT true, expiry_interval INTERVAL DEFAULT '30d') RETURNS VARCHAR | ||
| SECURITY DEFINER | ||
| SET search_path = '' | ||
| LANGUAGE sql AS $$ | ||
| INSERT INTO public.secret_token (payload, expiry_date, one_time_use) VALUES (v_payload, now()+expiry_interval, v_one_time_use) RETURNING id; | ||
| $$; | ||
|
|
||
| CREATE OR REPLACE FUNCTION public.get_secret_token(token VARCHAR) RETURNS JSONB | ||
| SECURITY DEFINER | ||
| SET search_path = '' | ||
| LANGUAGE plpgsql AS $$ | ||
| DECLARE | ||
| v_payload JSONB; | ||
| v_one_time_use BOOLEAN; | ||
| BEGIN | ||
| DELETE FROM public.secret_token WHERE expiry_date < now(); | ||
| SELECT payload, one_time_use INTO v_payload, v_one_time_use FROM public.secret_token WHERE id=token; | ||
| IF v_one_time_use = true THEN | ||
| DELETE FROM public.secret_token WHERE id=token; | ||
| END IF; | ||
| RETURN v_payload; | ||
| END; | ||
| $$; | ||
|
|
||
| ALTER TABLE secret_token OWNER TO "postgres"; | ||
|
|
||
| REVOKE ALL ON TABLE public.secret_token FROM anon; | ||
| GRANT ALL ON TABLE public.secret_token TO authenticated; | ||
| GRANT ALL ON TABLE public.secret_token TO service_role; | ||
|
|
||
| ALTER TABLE public.secret_token ENABLE ROW LEVEL SECURITY; | ||
| DROP POLICY IF EXISTS concept_policy ON public.secret_token; | ||
| DROP POLICY IF EXISTS concept_select_policy ON public.secret_token; | ||
| CREATE POLICY concept_select_policy ON public.secret_token FOR SELECT USING (creator = auth.uid()); | ||
| DROP POLICY IF EXISTS concept_delete_policy ON public.secret_token; | ||
| CREATE POLICY concept_delete_policy ON public.secret_token FOR DELETE USING (creator = auth.uid()); | ||
| DROP POLICY IF EXISTS concept_insert_policy ON public.secret_token; | ||
| -- Do not allow insert except through create_secret_token | ||
| DROP POLICY IF EXISTS concept_update_policy ON public.secret_token; | ||
| CREATE POLICY concept_update_policy ON public.secret_token FOR UPDATE USING (creator = auth.uid()); | ||
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 characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| CREATE TABLE IF NOT EXISTS public.secret_token ( | ||
| id varchar PRIMARY KEY DEFAULT encode(extensions.gen_random_bytes(12), 'base64'), | ||
| creator UUID NOT NULL DEFAULT auth.uid(), | ||
| payload JSONB NOT NULL, | ||
| expiry_date timestamp without time zone, | ||
| one_time_use boolean DEFAULT true | ||
| ); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS secret_token_expiry_idx ON public.secret_token (expiry_date) WHERE expiry_date IS NOT null; | ||
|
|
||
| CREATE OR REPLACE FUNCTION public.create_secret_token(v_payload JSONB, v_one_time_use BOOLEAN DEFAULT true, expiry_interval INTERVAL DEFAULT '30d') RETURNS VARCHAR | ||
| SECURITY DEFINER | ||
| SET search_path = '' | ||
| LANGUAGE sql AS $$ | ||
| INSERT INTO public.secret_token (payload, expiry_date, one_time_use) VALUES (v_payload, now()+expiry_interval, v_one_time_use) RETURNING id; | ||
| $$; | ||
|
|
||
| CREATE OR REPLACE FUNCTION public.get_secret_token(token VARCHAR) RETURNS JSONB | ||
| SECURITY DEFINER | ||
| SET search_path = '' | ||
| LANGUAGE plpgsql AS $$ | ||
| DECLARE | ||
| v_payload JSONB; | ||
| v_one_time_use BOOLEAN; | ||
| BEGIN | ||
| DELETE FROM public.secret_token WHERE expiry_date < now(); | ||
| SELECT payload, one_time_use INTO v_payload, v_one_time_use FROM public.secret_token WHERE id=token; | ||
| IF v_one_time_use = true THEN | ||
| DELETE FROM public.secret_token WHERE id=token; | ||
| END IF; | ||
| RETURN v_payload; | ||
| END; | ||
| $$; | ||
|
|
||
| ALTER TABLE secret_token OWNER TO "postgres"; | ||
|
|
||
| REVOKE ALL ON TABLE public.secret_token FROM anon; | ||
| GRANT ALL ON TABLE public.secret_token TO authenticated; | ||
| GRANT ALL ON TABLE public.secret_token TO service_role; | ||
|
|
||
| ALTER TABLE public.secret_token ENABLE ROW LEVEL SECURITY; | ||
| DROP POLICY IF EXISTS concept_policy ON public.secret_token; | ||
| DROP POLICY IF EXISTS concept_select_policy ON public.secret_token; | ||
| CREATE POLICY concept_select_policy ON public.secret_token FOR SELECT USING (creator = auth.uid()); | ||
| DROP POLICY IF EXISTS concept_delete_policy ON public.secret_token; | ||
| CREATE POLICY concept_delete_policy ON public.secret_token FOR DELETE USING (creator = auth.uid()); | ||
| DROP POLICY IF EXISTS concept_insert_policy ON public.secret_token; | ||
| -- Do not allow insert except through create_secret_token | ||
| DROP POLICY IF EXISTS concept_update_policy ON public.secret_token; | ||
| CREATE POLICY concept_update_policy ON public.secret_token FOR UPDATE USING (creator = auth.uid()); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.