Not an electrician, but you could use one of these: Leviton 42777-1EB Surface Mount Backbox, Single Gang, Black, 1.45" https://www.amazon.com/dp/B00332F4J4/ref=cm_sw_r_cp_api_glt_fabc_YVSZY35AG7WCZQNCYM4H?_encoding=UTF8&psc=1
But I would NOT rotate it 90 degrees. I would leave the existing box where it is, and add the new box on top of it, sandwiching the shelf between old and new boxes.
Or honestly just notch the shelf and work the trim around it, that would look good and I think be easier all told.