Fallback to inferring image_members unique constraint name

Fixes bug 1160114

The initial version of the 022_image_member_index migration
hardcoded the default naming convention for unqiue constraints
used by PostgreSQL 9.2, which turns out to different to the
convention used by PostgreSQL 8.4. This causes the CI to fail
(non-voting) against RHEL.

We resolve the issue by falling back to inferring the existing
unique constraint name from the image_members indices.

Change-Id: Iad6db033e63cdfd7a77db44bdeaed2284699b469
This commit is contained in:
Eoghan Glynn 2013-03-27 12:17:28 +00:00
parent 9cb43e03dd
commit e9079d0fd5
1 changed files with 18 additions and 3 deletions

View File

@ -15,12 +15,16 @@
# License for the specific language governing permissions and limitations
# under the License.
import re
from migrate.changeset import UniqueConstraint
from sqlalchemy import and_, func, orm
from sqlalchemy import MetaData, Table
from sqlalchemy.exc import OperationalError, ProgrammingError
NEW_KEYNAME = 'image_members_image_id_member_deleted_at_key'
ORIGINAL_KEYNAME_RE = re.compile('image_members_image_id.*_key')
def upgrade(migrate_engine):
@ -28,9 +32,14 @@ def upgrade(migrate_engine):
if (migrate_engine.name == 'mysql' or
migrate_engine.name == 'postgresql'):
UniqueConstraint('image_id',
name=_get_original_keyname(migrate_engine.name),
table=image_members).drop()
try:
UniqueConstraint('image_id',
name=_get_original_keyname(migrate_engine.name),
table=image_members).drop()
except (OperationalError, ProgrammingError):
UniqueConstraint('image_id',
name=_infer_original_keyname(image_members),
table=image_members).drop()
UniqueConstraint('image_id',
'member',
'deleted_at',
@ -64,6 +73,12 @@ def _get_original_keyname(db):
'postgresql': 'image_members_image_id_member_key'}[db]
def _infer_original_keyname(table):
for i in table.indexes:
if ORIGINAL_KEYNAME_RE.match(i.name):
return i.name
def _sanitize(migrate_engine, table):
"""
Avoid possible integrity error by removing deleted rows