-
Notifications
You must be signed in to change notification settings - Fork 114
Fix: Updated SQL schema to resolve missing tables errors #181
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,65 +1,116 @@ | ||
| -- Table for storing organization registrations | ||
| CREATE TABLE IF NOT EXISTS organization_integrations ( | ||
| -- ========================================================= | ||
| -- STEP 1: FULL CLEANUP (Re-initialization) | ||
| -- ========================================================= | ||
| DROP TABLE IF EXISTS public.indexed_repositories CASCADE; | ||
| DROP TABLE IF EXISTS public.interactions CASCADE; | ||
| DROP TABLE IF EXISTS public.conversation_context CASCADE; | ||
| DROP TABLE IF EXISTS public.organization_integrations CASCADE; | ||
| DROP TABLE IF EXISTS public.users CASCADE; | ||
|
|
||
| -- ========================================================= | ||
| -- STEP 2: CREATE USERS TABLE | ||
| -- ========================================================= | ||
| CREATE TABLE public.users ( | ||
| id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
| user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE, | ||
| discord_id TEXT UNIQUE, | ||
| email TEXT, | ||
| full_name TEXT, | ||
| avatar_url TEXT, | ||
| discord_username TEXT, | ||
| display_name TEXT, | ||
| preferred_languages TEXT[], | ||
| last_active_discord TIMESTAMPTZ DEFAULT NOW(), | ||
|
|
||
| -- Verification Columns | ||
| verification_token TEXT, | ||
| verification_token_expires_at TIMESTAMPTZ, | ||
|
|
||
| -- GitHub Data Columns | ||
| github_id TEXT, | ||
| github_username TEXT, | ||
| is_verified BOOLEAN DEFAULT FALSE, | ||
| verified_at TIMESTAMPTZ, | ||
|
|
||
| created_at TIMESTAMPTZ DEFAULT NOW(), | ||
| updated_at TIMESTAMPTZ DEFAULT NOW() | ||
| ); | ||
|
|
||
| ALTER TABLE public.users ENABLE ROW LEVEL SECURITY; | ||
| CREATE POLICY "Public Access Users" ON public.users FOR ALL USING (true) WITH CHECK (true); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: RLS policies expose all user data to all users. All row-level security policies use permissive These policies should scope access to the authenticated user, not grant blanket public access. For example, replace line 39 with policies that check the user's identity. - CREATE POLICY "Public Access Users" ON public.users FOR ALL USING (true) WITH CHECK (true);
+ CREATE POLICY "User Self Access" ON public.users
+ FOR SELECT USING (auth.uid()::uuid = id)
+ FOR UPDATE USING (auth.uid()::uuid = id)
+ FOR DELETE USING (auth.uid()::uuid = id)
+ FOR INSERT WITH CHECK (auth.uid()::uuid = id);Apply similar scoped access patterns to all other tables ( Also applies to: 58-58, 80-80, 83-83, 109-109 π€ Prompt for AI Agents |
||
|
|
||
| -- ========================================================= | ||
| -- STEP 3: INTEGRATIONS TABLE | ||
| -- ========================================================= | ||
| CREATE TABLE public.organization_integrations ( | ||
| id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
| user_id UUID REFERENCES public.users(id) ON DELETE CASCADE, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Major: Foreign key columns lack NOT NULL constraints. Lines 46, 71, and 90 define - user_id UUID REFERENCES public.users(id) ON DELETE CASCADE,
+ user_id UUID NOT NULL REFERENCES public.users(id) ON DELETE CASCADE,Apply this fix to Also applies to: 71-71, 90-90 |
||
| platform VARCHAR(50) NOT NULL CHECK (platform IN ('github', 'discord', 'slack', 'discourse')), | ||
| organization_name VARCHAR(255) NOT NULL, | ||
| is_active BOOLEAN NOT NULL DEFAULT true, | ||
| config JSONB DEFAULT '{}', -- Stores org link, discord_guild_id, etc. | ||
| config JSONB DEFAULT '{}', | ||
| created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), | ||
| updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), | ||
|
|
||
| -- Ensure one integration per user per platform | ||
| UNIQUE(user_id, platform) | ||
| ); | ||
|
|
||
| -- Create indexes for better query performance | ||
| CREATE INDEX IF NOT EXISTS idx_org_integrations_user_id ON organization_integrations(user_id); | ||
| CREATE INDEX IF NOT EXISTS idx_org_integrations_platform ON organization_integrations(platform); | ||
| CREATE INDEX IF NOT EXISTS idx_org_integrations_is_active ON organization_integrations(is_active); | ||
| ALTER TABLE public.organization_integrations ENABLE ROW LEVEL SECURITY; | ||
| CREATE POLICY "Public Access Orgs" ON organization_integrations FOR ALL USING (true) WITH CHECK (true); | ||
|
|
||
| -- Create function to automatically update updated_at timestamp | ||
| CREATE OR REPLACE FUNCTION update_updated_at_column() | ||
| RETURNS TRIGGER AS $$ | ||
| BEGIN | ||
| NEW.updated_at = NOW(); | ||
| RETURN NEW; | ||
| END; | ||
| $$ LANGUAGE plpgsql; | ||
|
|
||
| -- Create triggers to automatically update updated_at | ||
| CREATE TRIGGER update_organization_integrations_updated_at | ||
| BEFORE UPDATE ON organization_integrations | ||
| FOR EACH ROW | ||
| EXECUTE FUNCTION update_updated_at_column(); | ||
|
|
||
| -- Enable Row Level Security (RLS) | ||
| ALTER TABLE organization_integrations ENABLE ROW LEVEL SECURITY; | ||
| -- ========================================================= | ||
| -- STEP 4: MEMORY TABLES (Context & Interactions) | ||
| -- ========================================================= | ||
| CREATE TABLE public.conversation_context ( | ||
| user_id UUID REFERENCES public.users(id) ON DELETE CASCADE PRIMARY KEY, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Major: Line 64 defines If supporting multiple conversations is required: - CREATE TABLE public.conversation_context (
- user_id UUID REFERENCES public.users(id) ON DELETE CASCADE PRIMARY KEY,
+ CREATE TABLE public.conversation_context (
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
+ user_id UUID NOT NULL REFERENCES public.users(id) ON DELETE CASCADE,
context JSONB DEFAULT '{}',
updated_at TIMESTAMPTZ DEFAULT NOW()
+ );
+ CREATE INDEX IF NOT EXISTS idx_conversation_context_user_id ON conversation_context(user_id);If limiting to one context per user is intentional, document this constraint clearly.
π€ Prompt for AI Agents
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @coderabbitai Thanks for the review. Since this script is intended for local development setup to fix the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @HamzaNasiem Thanks for the context! I understand the RLS policies are permissive for local development, and the DROP TABLE statements ensure a clean setupβthat makes sense for the use case. My original comment was specifically about the My question is: Is this intentional?
Could you clarify which behavior you're aiming for? That way, we can ensure the schema aligns with your requirements. π |
||
| context JSONB DEFAULT '{}', | ||
| updated_at TIMESTAMPTZ DEFAULT NOW() | ||
| ); | ||
|
|
||
| -- Create RLS policies for organization_integrations | ||
| -- Users can only see and manage their own integrations | ||
| CREATE POLICY "Users can view their own integrations" | ||
| ON organization_integrations | ||
| FOR SELECT | ||
| USING (auth.uid() = user_id); | ||
| CREATE TABLE public.interactions ( | ||
| id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
| user_id UUID REFERENCES public.users(id) ON DELETE CASCADE, | ||
| session_id TEXT, | ||
| input_text TEXT, | ||
| output_text TEXT, | ||
| tool_usage JSONB DEFAULT '[]', | ||
| created_at TIMESTAMPTZ DEFAULT NOW() | ||
| ); | ||
|
|
||
| CREATE POLICY "Users can create their own integrations" | ||
| ON organization_integrations | ||
| FOR INSERT | ||
| WITH CHECK (auth.uid() = user_id); | ||
| ALTER TABLE public.conversation_context ENABLE ROW LEVEL SECURITY; | ||
| CREATE POLICY "Public Context Access" ON conversation_context FOR ALL USING (true) WITH CHECK (true); | ||
|
|
||
| CREATE POLICY "Users can update their own integrations" | ||
| ON organization_integrations | ||
| FOR UPDATE | ||
| USING (auth.uid() = user_id) | ||
| WITH CHECK (auth.uid() = user_id); | ||
| ALTER TABLE public.interactions ENABLE ROW LEVEL SECURITY; | ||
| CREATE POLICY "Public Interaction Access" ON interactions FOR ALL USING (true) WITH CHECK (true); | ||
|
|
||
| CREATE POLICY "Users can delete their own integrations" | ||
| ON organization_integrations | ||
| FOR DELETE | ||
| USING (auth.uid() = user_id); | ||
| -- ========================================================= | ||
| -- STEP 5: INDEXED REPOSITORIES (Complete Definition) | ||
| -- ========================================================= | ||
| CREATE TABLE public.indexed_repositories ( | ||
| id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
| user_id UUID REFERENCES public.users(id) ON DELETE CASCADE, | ||
| repository_full_name TEXT NOT NULL, | ||
| graph_name TEXT NOT NULL, | ||
|
|
||
| -- β All Critical Columns Included Here | ||
| indexed_by_discord_id TEXT, | ||
| last_error TEXT, | ||
| indexed_at TIMESTAMPTZ DEFAULT NOW(), | ||
|
|
||
| branch TEXT DEFAULT 'main', | ||
| node_count INTEGER DEFAULT 0, | ||
| edge_count INTEGER DEFAULT 0, | ||
| indexing_status TEXT DEFAULT 'pending', | ||
| is_deleted BOOLEAN DEFAULT FALSE, | ||
| created_at TIMESTAMPTZ DEFAULT NOW(), | ||
| updated_at TIMESTAMPTZ DEFAULT NOW(), | ||
| UNIQUE(user_id, repository_full_name) | ||
| ); | ||
|
|
||
| -- Add helpful comments | ||
| COMMENT ON TABLE organization_integrations IS 'Stores registered organizations (just metadata, no tokens)'; | ||
| COMMENT ON COLUMN organization_integrations.config IS 'Platform-specific data: organization_link, discord_guild_id, etc.'; | ||
| CREATE INDEX IF NOT EXISTS idx_indexed_repos_user ON indexed_repositories(user_id); | ||
| ALTER TABLE public.indexed_repositories ENABLE ROW LEVEL SECURITY; | ||
| CREATE POLICY "Public Index Access" ON indexed_repositories FOR ALL USING (true) WITH CHECK (true); | ||
|
|
||
| -- ========================================================= | ||
| -- STEP 6: FINAL REFRESH | ||
| -- ========================================================= | ||
| NOTIFY pgrst, 'reload schema'; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Major: Missing indexes on UNIQUE identifier columns.
Lines 15 and 29 define
discord_idandgithub_idas UNIQUE but don't create explicit indexes. While Postgres auto-indexes UNIQUE constraints, explicit indexes improve query performance for lookups. Additionally,discord_idis nullable but appears to be a user identifierβthis is inconsistent.Add explicit indexes and clarify the design:
Consider whether
discord_idshould beNOT NULLif it's the primary user identifier. If users can register without Discord, document the onboarding flow.π€ Prompt for AI Agents