-- Graceful wrapper for supabase_migrations.schema_migrations.
-- Returns migration records when the table exists; returns empty when it does not.
CREATE OR REPLACE FUNCTION public.list_migrations()
RETURNS TABLE (
  version text,
  name text
)
LANGUAGE plpgsql
STABLE
AS $$
BEGIN
  RETURN QUERY
  SELECT sm.version, sm.name
  FROM supabase_migrations.schema_migrations sm
  ORDER BY sm.version;
EXCEPTION
  WHEN undefined_table THEN
    RETURN;  -- return empty when table/schema does not exist
  WHEN invalid_schema_name THEN
    RETURN;
END;
$$;
