Skip to content

Default tenant has wrong tenant_id (solution provided) #1570

@plutos-trading-sudo

Description

@plutos-trading-sudo

Bug report

Hey guys.
When setting up my supabase for me the tenant_id in realtime is being created with wrong tenant_id which leads to not working realtime.

For me this SQL fixes it I hope I can help some people here would be nice to have this fixed.

DO $$
DECLARE
  old_external_id text := 'realtime-dev';
  new_external_id text := 'supabase-supabase-realtime';
  new_name        text := 'supabase-supabase-realtime';
  v_exists        boolean;
BEGIN
  -- 1) Sanity checks
  SELECT EXISTS (
    SELECT 1 FROM _realtime.tenants WHERE external_id = old_external_id
  ) INTO v_exists;
  IF NOT v_exists THEN
    RAISE EXCEPTION 'Tenant with external_id % not found', old_external_id;
  END IF;

  SELECT EXISTS (
    SELECT 1 FROM _realtime.tenants WHERE external_id = new_external_id
  ) INTO v_exists;
  IF v_exists THEN
    RAISE EXCEPTION 'external_id % is already in use', new_external_id;
  END IF;

  -- 2) Drop FK so we can change the parent key safely
  EXECUTE 'ALTER TABLE _realtime.extensions
           DROP CONSTRAINT IF EXISTS extensions_tenant_external_id_fkey';

  -- 3) Update parent row (tenants)
  UPDATE _realtime.tenants
     SET name = new_name,
         external_id = new_external_id,
         updated_at = now()
   WHERE external_id = old_external_id;

  -- 4) Update child rows (extensions)
  UPDATE _realtime.extensions
     SET tenant_external_id = new_external_id,
         updated_at = now()
   WHERE tenant_external_id = old_external_id;

  -- 5) Re-create FK
  EXECUTE 'ALTER TABLE _realtime.extensions
           ADD CONSTRAINT extensions_tenant_external_id_fkey
           FOREIGN KEY (tenant_external_id)
           REFERENCES _realtime.tenants (external_id)
           ON DELETE CASCADE';
END $$ LANGUAGE plpgsql;

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions