Magento 2.4. rebuild/recover table catalog_product_entity_media_gallery_value_to_entity

Yep ... that's a long name for a table but it also states what it was built for. However, after a bad server crash I needed to manually rebuild the whole database (innodb_force_recovery from 1 to 6 and back and forth ...). Sadly some of the files were corrupt so I had to either accept it or try to build the data from other source. In the case of the above table some assignments were missing but luckily you can generate the tuples by using this statement:

insert into catalog_product_entity_media_gallery_value_to_entity select b.value_id, a.entity_id from catalog_product_entity_varchar a join catalog_product_entity_media_gallery b on a.value = b.value where a.attribute_id in (87,88,89,135) and a.entity_id not in (select entity_id from catalog_product_entity_media_gallery_value_to_entity order by entity_id) group by a.value, a.entity_id,b.value_id;