Skip to content

Add unique constraint support for tables and automatic upserts in Supabase connector #198

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

fredpedersen
Copy link

  • Add unique property to Index class to define unique constraints
  • Update Table class with methods to retrieve unique columns and indexes
  • Create UpsertOptions data class for configuring upsert behavior
  • Enhance SupabaseConnector to automatically handle upserts based on schema
    • Detects unique constraints from table schema
    • Sets appropriate onConflict columns for Supabase upsert operations
    • Falls back to primary key conflict resolution when no unique indexes exist
  • Add extension functions and examples for working with unique constraints

This allows developers to define unique constraints in their PowerSync schema and have the Supabase connector automatically handle upserts with proper conflict resolution without manual configuration.

#196

…abase connector

- Add `unique` property to Index class to define unique constraints
- Update Table class with methods to retrieve unique columns and indexes
- Create UpsertOptions data class for configuring upsert behavior
- Enhance SupabaseConnector to automatically handle upserts based on schema
  - Detects unique constraints from table schema
  - Sets appropriate onConflict columns for Supabase upsert operations
  - Falls back to primary key conflict resolution when no unique indexes exist
- Add extension functions and examples for working with unique constraints

This allows developers to define unique constraints in their PowerSync schema
and have the Supabase connector automatically handle upserts with proper
conflict resolution without manual configuration.
Copy link
Contributor

@rkistner rkistner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the toSqlDefinition function is isn't used anymore (not actually sure if it ever was), so this change won't have any effect. The actual indexes are created in the sqlite extension.

Apart from this, client-side unique constraints are problematic with the current sync implementation. If for example you remove a row, then create another one with the same unique columns, the sync process could apply the changes in a different order and end up violating the constraint, which would completely block the sync process.

We are planning some functionality to support syncing to custom tables, in which case you'd have more control over indexes and handling conflicts. You'd still have to be careful around the above behavior.

I do think we need to make the SupabaseConnector more customizable though, to allow having this type of functionality when uploading.

@fredpedersen
Copy link
Author

fredpedersen commented Jun 2, 2025 via email

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