[1.7.x] Fixed #22350 -- Consistently serialize bytes and text in migrations.

Thanks to @treyhunner and Loïc for their suggestions and review.

Backport of 72d3889db4 from master