42 lines
1.3 KiB
PL/PgSQL
42 lines
1.3 KiB
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();
|
|
|
|
-- Table to store users' favourite study spaces
|
|
CREATE TABLE favourite_study_spaces (
|
|
user_id uuid REFERENCES users(id) ON DELETE CASCADE,
|
|
study_space_id uuid REFERENCES study_spaces(id) ON DELETE CASCADE,
|
|
created_at timestamp with time zone DEFAULT now(),
|
|
updated_at timestamp with time zone DEFAULT now(),
|
|
PRIMARY KEY (user_id, study_space_id)
|
|
);
|
|
|
|
CREATE TRIGGER favourite_study_spaces_updated_at
|
|
AFTER UPDATE ON favourite_study_spaces
|
|
FOR EACH ROW EXECUTE FUNCTION handle_updated_at();
|