|
| 1 | +Issue summary |
| 2 | + |
| 3 | +cloudsync_init('users') fails in Supabase postgres with: |
| 4 | +"column reference \"id\" is ambiguous". |
| 5 | +Both public.users and auth.users exist. Several PostgreSQL SQL templates use only table_name (no schema), so information_schema lookups and dynamic SQL see multiple tables and generate ambiguous column references. |
| 6 | + |
| 7 | +Proposed fixes (options) |
| 8 | + |
| 9 | +1) Minimal fix (patch specific templates) |
| 10 | +- Add table_schema = current_schema() to information_schema queries. |
| 11 | +- Keep relying on search_path. |
| 12 | +- Resolves Supabase default postgres collisions without changing the API. |
| 13 | + |
| 14 | +2) Robust fix (explicit schema support) |
| 15 | +- Allow schema-qualified inputs, e.g. cloudsync_init('public.users'). |
| 16 | +- Parse schema/table and propagate through query builders. |
| 17 | +- Always generate fully-qualified table names ("schema"."table"). |
| 18 | +- Apply schema-aware filters in information_schema queries. |
| 19 | +- Removes ambiguity regardless of search_path or duplicate table names across schemas. |
| 20 | +- Note: payload compatibility requires cloudsync_changes.tbl to remain unqualified; PG apply should resolve schema via cloudsync_table_settings (not search_path) when applying payloads. |
| 21 | + |
| 22 | +Bugged query templates |
| 23 | + |
| 24 | +Already fixed: |
| 25 | +- SQL_PRAGMA_TABLEINFO_PK_COLLIST |
| 26 | +- SQL_PRAGMA_TABLEINFO_PK_DECODE_SELECTLIST |
| 27 | + |
| 28 | +Still vulnerable (missing schema filter): |
| 29 | +- SQL_BUILD_SELECT_NONPK_COLS_BY_ROWID |
| 30 | +- SQL_PRAGMA_TABLEINFO_LIST_NONPK_NAME_CID |
| 31 | +- SQL_CLOUDSYNC_DELETE_COLS_NOT_IN_SCHEMA_OR_PKCOL |
| 32 | +- SQL_PRAGMA_TABLEINFO_PK_QUALIFIED_COLLIST_FMT |
| 33 | + |
| 34 | +Robust fix implementation plan |
| 35 | + |
| 36 | +Goals |
| 37 | +- Support cloudsync_init('users') and cloudsync_init('public.users') |
| 38 | +- Default schema to current_schema() when not provided |
| 39 | +- Persist schema so future connections are independent of search_path |
| 40 | +- Generate fully qualified table names in all PostgreSQL SQL builders |
| 41 | + |
| 42 | +1) Parse schema/table at init |
| 43 | +- In cloudsync_init_table() (cloudsync.c), parse the input table_name: |
| 44 | + - If it contains a dot, split schema/table |
| 45 | + - Else schema = current_schema() (query once) |
| 46 | +- Normalize case to match existing behavior |
| 47 | + |
| 48 | +2) Persist schema in settings |
| 49 | +- Store schema in cloudsync_table_settings using key='schema' |
| 50 | +- Keep tbl_name as unqualified table name |
| 51 | +- On first run, if schema is not stored, write it |
| 52 | + |
| 53 | +3) Store schema in context |
| 54 | +- Add char *schema to cloudsync_table_context |
| 55 | +- Populate on table creation and when reloading from settings |
| 56 | +- Use schema when building SQL |
| 57 | + |
| 58 | +4) Restore schema on new connections |
| 59 | +- During context rebuild, read schema from cloudsync_table_settings |
| 60 | +- If missing, fallback to current_schema(), optionally persist it |
| 61 | + |
| 62 | +5) Qualify SQL everywhere (Postgres) |
| 63 | +- Use "schema"."table" in generated SQL |
| 64 | +- Add table_schema filters to information_schema queries: |
| 65 | + - SQL_BUILD_SELECT_NONPK_COLS_BY_ROWID |
| 66 | + - SQL_PRAGMA_TABLEINFO_LIST_NONPK_NAME_CID |
| 67 | + - SQL_CLOUDSYNC_DELETE_COLS_NOT_IN_SCHEMA_OR_PKCOL |
| 68 | + - SQL_PRAGMA_TABLEINFO_PK_QUALIFIED_COLLIST_FMT |
| 69 | + - Any other information_schema templates using only table_name |
| 70 | + |
| 71 | +6) Compatibility |
| 72 | +- Existing DBs without schema setting continue to work via current_schema() |
| 73 | +- No API changes required for unqualified names |
0 commit comments