-- name: provider_operation__insert :one
INSERT INTO provider_hub_operations (
    id,
    command_id,
    actor_id,
    external_account_id,
    provider_slug,
    operation_type,
    target_ref,
    status,
    result_ref,
    provider_object_id,
    repository_full_name,
    error_code,
    error_message,
    rate_limit_snapshot_id,
    operation_policy_context_json,
    approval_gate_ref_json,
    provider_version,
    base_branch,
    started_at,
    finished_at,
    version,
    created_at,
    updated_at
) VALUES (
    @id,
    @command_id,
    @actor_id,
    @external_account_id,
    @provider_slug,
    @operation_type,
    @target_ref,
    @status,
    @result_ref,
    @provider_object_id,
    @repository_full_name,
    @error_code,
    @error_message,
    @rate_limit_snapshot_id,
    @operation_policy_context_json::jsonb,
    @approval_gate_ref_json::jsonb,
    @provider_version,
    @base_branch,
    @started_at,
    @finished_at,
    @version,
    @created_at,
    @updated_at
)
ON CONFLICT (operation_type, command_id) WHERE command_id <> '' DO NOTHING
RETURNING
    id,
    command_id,
    actor_id,
    external_account_id,
    provider_slug,
    operation_type,
    target_ref,
    status,
    result_ref,
    provider_object_id,
    repository_full_name,
    error_code,
    error_message,
    rate_limit_snapshot_id,
    operation_policy_context_json,
    approval_gate_ref_json,
    provider_version,
    base_branch,
    started_at,
    finished_at,
    version,
    created_at,
    updated_at;
