Files
drp-48/supabase/migrations/20250612104310_users-admin.sql
Caspar Jojo Asaam 2219f7a3b9 fix: Added the migration of the favourite study space table
Co-Authored-By: Tadios Temesgen <tt2022@ic.ac.uk>
2025-06-13 12:05:39 +01:00

30 lines
771 B
PL/PgSQL

CREATE TABLE users (
id uuid PRIMARY KEY REFERENCES auth.users ON DELETE CASCADE,
is_admin boolean NOT NULL DEFAULT false,
created_at timestamp with time zone NOT NULL DEFAULT now(),
updated_at timestamp with time zone NOT NULL DEFAULT now()
);
CREATE TRIGGER users_handle_updated_at
AFTER UPDATE ON users
FOR EACH ROW EXECUTE FUNCTION handle_updated_at();
-- Auto-create users when auth.users are created
CREATE FUNCTION handle_new_user()
RETURNS trigger
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = ''
AS $$
BEGIN
INSERT INTO public.users (id, contact_email)
VALUES (NEW.id, NEW.email);
RETURN NEW;
END;
$$;
CREATE TRIGGER users_handle_new_user
AFTER INSERT ON auth.users
FOR EACH ROW EXECUTE FUNCTION handle_new_user();