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

Fix: Supabase updates #2100

Merged
merged 7 commits into from
Jan 11, 2025
Merged

Fix: Supabase updates #2100

merged 7 commits into from
Jan 11, 2025

Conversation

antman1p
Copy link
Contributor

@antman1p antman1p commented Jan 10, 2025

Fixes an issue where missing rooms in the Supabase database caused errors when fetching them using .single(). Additionally, ensures that the database seed values contain valid timestamps and required non-null fields. Also fixes an issue where mixed-case column names were interpreted as lowercase in Supabase PostgreSQL.

Relates to

N/A

Risks

  • Low: The changes improve robustness and prevent errors but do not modify fundamental logic.
  • Affected areas:
    • getRoom() function in adapter-supabase/index.ts.
    • Database seed values in seed.sql.

Background

What does this PR do?

  • Updates getRoom() in the Supabase adapter to use .maybeSingle() instead of .single(), preventing crashes when no room is found.
  • Ensures all mixed-case column names in SQL statements are wrapped in double quotes to prevent PostgreSQL from auto-lowering their case.
  • Modifies the database seed values to include:
    • A valid "createdAt" timestamp for the rooms table.
    • A unique "id" for the participants table.
    • A valid "createdAt" timestamp for the participants table.
    • Ensures "userState" and "last_message_read" are explicitly set to prevent null constraint violations.

What kind of change is this?

  • Bug fixes (non-breaking change which fixes an issue)
  • Improvements (misc. changes to existing features)

Why are we doing this? Any context or related work?

When attempting to use the seeds.sql text in supabase I received several errors related to column names:

ERROR:  42703: column "avatarurl" of relation "accounts" does not exist
LINE 1: INSERT INTO public.accounts (id, name, email, avatarUrl, details) VALUES ('00000000-0000-0000-0000-000000000000', 'Default Agent', '[email protected]', '', '{}');
                                                      ^

... for example.
When running start in Eliza, after creating the supabase db I recieved this error:

file:///home/antman/DEV/eliza/packages/adapter-supabase/dist/index.js:9
            throw new Error(Error getting room: ${error.message});
                  ^

Error: Error getting room: JSON object requested, multiple (or no) rows returned    
    at SupabaseDatabaseAdapter.getRoom (file:///home/me/DEV/eliza/packages/adapter-supabase/dist/index.js:9:19)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)  
    at async AgentRuntime.ensureRoomExists (file:///home/me/DEV/eliza/packages/core/dist/index.js:29864:22)

I needed to fix these error in order to use Supabase/postgres.

Documentation changes needed?

  • My changes do not require a change to the project documentation.

Testing

Where should a reviewer start?

  1. Review the changes in adapter-supabase/index.ts.
  2. Check seed.sql to verify updated default values.
  3. Test the database seed process to ensure proper data insertion.

Detailed testing steps

  • Run the updated seed script:
  INSERT INTO public.accounts ("id", "name", "email", "avatarUrl", "details") 
  VALUES ('00000000-0000-0000-0000-000000000000', 'Default Agent', '[email protected]', '', '{}');

  INSERT INTO public.rooms ("id", "createdAt") 
  VALUES ('00000000-0000-0000-0000-000000000000', NOW());

  INSERT INTO public.participants ("id", "createdAt", "userId", "roomId", "userState", "last_message_read") 
  VALUES ('00000000-0000-0000-0000-000000000000', NOW(), '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000', 'FOLLOWED', NULL);
  • Validate the getRoom() function:
async getRoom(roomId) {
    const { data, error } = await this.supabase
        .from("rooms")
        .select('"id"')  // ✅ Ensure correct case handling
        .eq('"id"', roomId)
        .maybeSingle();  // ✅ Prevents errors when no row exists

    if (error) {
        console.error(`Error getting room: ${error.message}`);
        return null;  // ✅ Returns `null` instead of crashing
    }

    return data ? data.id : null;
}
  • Ensure the getRoom() function returns null instead of crashing when querying a non-existent room.
  • Verify that newly inserted rooms now contain valid "createdAt" timestamps.

Deploy Notes

No special deployment steps required; normal database migrations and API updates should suffice.

Database changes

None

Deployment instructions

None

🚀 Summary of Fixes

✔️ Ensured mixed-case column names are quoted ("columnName") in SQL queries.
✔️ Updated getRoom() to select "id" explicitly with correct casing.
✔️ Modified seed.sql to quote "createdAt", "avatarUrl", and other mixed-case columns.
✔️ Prevented crashes in Supabase when fetching missing rooms.

Discord username

4n7m4n

Fixed unquoted column names.  Added required column values.
Added roomId value.
Allows getRoom() to return NULL if no room exists, and returns single room if multiple rooms exist with the same "roomId" without breaking the program.
@antman1p antman1p changed the title Supabase updates Fix: Supabase updates Jan 10, 2025
monilpat
monilpat previously approved these changes Jan 11, 2025
@monilpat monilpat merged commit 85f5185 into elizaOS:develop Jan 11, 2025
3 checks passed
0xpi-ai pushed a commit to 0xpi-ai/NayariAI that referenced this pull request Jan 15, 2025
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

Successfully merging this pull request may close these issues.

3 participants