Fix various errors in Upgrade process (other/upgrade_2-1_mysql.sql)

I have been running the upgrade.php tool on a real forum dataset. This process revealed two serious issues: PM Labels cause big problems for the upgrade, and existing attachments don't work after the upgrade.

This request now includes three commits to deal with this:

1. e73bd602fb900a736eaeb39e85c9c4efea06dcbc:
This works around the fatal errors created by commit 754c249ef32948df6fe17bae0a03c3379a7a2afe. This needed to be done before I could actually test/fix the other problems. Obviously it can be ignored if a real fix is added.

2. cd453e1f718281a96858929007c0f58389ca530a
This fixes several problems with the handling of PM Labels. It also fixes a query error in the attachment handling.

3. 37e977b2bafc58905f7254c764ef6ba2e026a0f7
This fixes the attachment rename processing. The biggest problem in this code was missing braces around the *Check if the av is an attachment* hook.