Skip to content
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

The Schema Doesn't Work #11

Open
2-fly-4-ai opened this issue Apr 6, 2024 · 0 comments
Open

The Schema Doesn't Work #11

2-fly-4-ai opened this issue Apr 6, 2024 · 0 comments

Comments

@2-fly-4-ai
Copy link

2-fly-4-ai commented Apr 6, 2024

As mentioned, the Schema is not working. Try this if you having issues.

`CREATE OR REPLACE FUNCTION moddatetime()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE SCHEMA IF NOT EXISTS public;

CREATE TABLE public.profiles (
id uuid NOT NULL,
updated_at timestamp with time zone NULL,
username text NULL,
full_name text NULL,
avatar_url text NULL,
website text NULL,
CONSTRAINT profiles_pkey PRIMARY KEY (id),
CONSTRAINT profiles_username_key UNIQUE (username),
CONSTRAINT profiles_id_fkey FOREIGN KEY (id) REFERENCES auth.users (id),
CONSTRAINT username_length CHECK ((char_length(username) >= 3))
) TABLESPACE pg_default;

CREATE TABLE public.categories (
id uuid NOT NULL DEFAULT gen_random_uuid(),
title text NULL DEFAULT ''::text,
created_at timestamp with time zone NULL DEFAULT now(),
slug text NULL,
CONSTRAINT category_pkey PRIMARY KEY (id),
CONSTRAINT category_id_key UNIQUE (id)
) TABLESPACE pg_default;

CREATE TABLE public.posts (
id uuid NOT NULL DEFAULT gen_random_uuid(),
category_id uuid NULL,
title text NULL,
image text NULL,
description text NULL,
content text NULL,
created_at timestamp with time zone NULL DEFAULT now(),
updated_at timestamp with time zone NULL,
slug text NULL DEFAULT ''::text,
author_id uuid NULL,
published boolean NULL DEFAULT false,
CONSTRAINT post_pkey PRIMARY KEY (id),
CONSTRAINT post_id_key UNIQUE (id),
CONSTRAINT post_slug_key UNIQUE (slug),
CONSTRAINT posts_author_id_fkey FOREIGN KEY (author_id) REFERENCES profiles (id),
CONSTRAINT posts_category_id_fkey FOREIGN KEY (category_id) REFERENCES categories (id)
) TABLESPACE pg_default;

CREATE TABLE public.comments (
id uuid NOT NULL DEFAULT gen_random_uuid(),
comment text NULL DEFAULT ''::text,
created_at timestamp with time zone NULL DEFAULT now(),
user_id uuid NULL,
post_id uuid NULL,
CONSTRAINT comments_pkey PRIMARY KEY (id),
CONSTRAINT comments_post_id_fkey FOREIGN KEY (post_id) REFERENCES posts (id) ON DELETE CASCADE,
CONSTRAINT comments_user_id_fkey FOREIGN KEY (user_id) REFERENCES profiles (id) ON DELETE CASCADE
) TABLESPACE pg_default;

CREATE TABLE public.bookmarks (
id uuid NOT NULL,
user_id uuid NULL,
created_at timestamp with time zone NULL DEFAULT now(),
CONSTRAINT bookmarks_pkey PRIMARY KEY (id),
CONSTRAINT bookmarks_id_fkey FOREIGN KEY (id) REFERENCES posts (id) ON DELETE CASCADE,
CONSTRAINT bookmarks_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users (id) ON DELETE CASCADE
) TABLESPACE pg_default;

CREATE TRIGGER handle_updated_at
BEFORE UPDATE ON posts
FOR EACH ROW
EXECUTE FUNCTION moddatetime('updated_at');

CREATE TABLE public.drafts (
id uuid NOT NULL DEFAULT gen_random_uuid(),
category_id uuid NULL,
title text NULL DEFAULT 'Untitled'::text,
slug text NULL DEFAULT 'untitled'::text,
image text NULL,
description text NULL,
content text NULL,
created_at timestamp with time zone NOT NULL DEFAULT now(),
updated_at timestamp without time zone NULL,
author_id uuid NULL,
published boolean NULL DEFAULT false,
CONSTRAINT drafts_pkey PRIMARY KEY (id),
CONSTRAINT drafts_author_id_fkey FOREIGN KEY (author_id) REFERENCES profiles (id),
CONSTRAINT drafts_category_id_fkey FOREIGN KEY (category_id) REFERENCES categories (id) ON DELETE CASCADE
) TABLESPACE pg_default;

CREATE TRIGGER handle_updated_at
BEFORE UPDATE ON drafts
FOR EACH ROW
EXECUTE FUNCTION moddatetime('updated_at');

-- Modify the handle_new_user() function
CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS TRIGGER AS $$
BEGIN
RAISE NOTICE 'handle_new_user() called for user ID: %', NEW.id;
INSERT INTO public.profiles (id, updated_at, full_name, avatar_url)
VALUES (
NEW.id,
CURRENT_TIMESTAMP,
COALESCE(jsonb_extract_path_text(NEW.raw_user_meta_data, 'full_name'), NULL),
COALESCE(jsonb_extract_path_text(NEW.raw_user_meta_data, 'picture'), jsonb_extract_path_text(NEW.raw_user_meta_data, 'avatar_url'), NULL)
)
ON CONFLICT (id) DO NOTHING;

RETURN NEW;

END;
$$ LANGUAGE plpgsql SECURITY DEFINER;

-- Create the trigger to handle new user creation
CREATE TRIGGER on_auth_user_created
AFTER INSERT ON auth.users
FOR EACH ROW EXECUTE FUNCTION public.handle_new_user();

-- Grant necessary permissions
GRANT USAGE ON SCHEMA "public" TO anon;
GRANT USAGE ON SCHEMA "public" TO authenticated;

GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA "public" TO authenticated;
GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA "public" TO anon;

GRANT EXECUTE ON FUNCTION public.handle_new_user() TO postgres;
GRANT INSERT ON public.profiles TO authenticated;
GRANT INSERT ON public.profiles TO anon;`

@2-fly-4-ai 2-fly-4-ai changed the title This project doesn't work. It has a huge range of issues at launch with no means to fix. The Schema Doesn't Work Apr 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant