diff --git a/ceilometer/storage/sqlalchemy/alembic/versions/2c3ccda5a3ad_fix_uniq_name.py b/ceilometer/storage/sqlalchemy/alembic/versions/2c3ccda5a3ad_fix_uniq_name.py new file mode 100644 index 000000000..a99f0194b --- /dev/null +++ b/ceilometer/storage/sqlalchemy/alembic/versions/2c3ccda5a3ad_fix_uniq_name.py @@ -0,0 +1,71 @@ +# -*- encoding: utf-8 -*- +# +# Copyright 2010-2011 OpenStack Foundation +# Copyright 2012-2013 IBM Corp. # +# +# Authors: Svetlana Shturm +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +"""Fix name of UniqueConstraint according to OpenStack naming convention + +Revision ID: 2c3ccda5a3ad +Revises: b6ae66d05e3 +Create Date: 2013-08-19 18:06:03.409584 + +""" + +# revision identifiers, used by Alembic. +revision = '2c3ccda5a3ad' +down_revision = 'b6ae66d05e3' + +from alembic import op + + +TABLE_NAME = 'sourceassoc' +OLD_NAME = 'uniq_sourceassoc0meter_id' +NEW_NAME = 'uniq_sourceassoc0meter_id0user_id' +COLUMNS = ('meter_id', 'user_id') + + +def change_uniq(table_name, old_name, new_name, columns): + engine = op.get_bind().engine + if engine.name == 'sqlite': + return + if engine.name == 'mysql': + # For mysql dialect all dependent FK should be removed + # before renaming of constraint. + op.drop_constraint('fk_sourceassoc_meter_id', + table_name, + type_='foreignkey') + op.drop_constraint('fk_sourceassoc_user_id', + table_name, + type_='foreignkey') + try: + # For some versions of dialects constraint can be skipped. + op.drop_constraint(old_name, table_name=table_name, type_='unique') + except Exception: + pass + op.create_unique_constraint(new_name, table_name, columns) + if engine.name == 'mysql': + op.create_foreign_key('fk_sourceassoc_meter_id', table_name, 'meter', + ['meter_id'], ['id']) + op.create_foreign_key('fk_sourceassoc_user_id', table_name, 'user', + ['user_id'], ['id']) + + +def upgrade(): + change_uniq(TABLE_NAME, OLD_NAME, NEW_NAME, COLUMNS) + + +def downgrade(): + change_uniq(TABLE_NAME, NEW_NAME, OLD_NAME, COLUMNS) diff --git a/ceilometer/storage/sqlalchemy/models.py b/ceilometer/storage/sqlalchemy/models.py index 89012b016..0d777c1a8 100644 --- a/ceilometer/storage/sqlalchemy/models.py +++ b/ceilometer/storage/sqlalchemy/models.py @@ -23,7 +23,7 @@ import urlparse from oslo.config import cfg from sqlalchemy import Column, Integer, String, Table, ForeignKey, DateTime, \ - Index + Index, UniqueConstraint from sqlalchemy import Float, Boolean, Text from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import backref @@ -104,6 +104,8 @@ Index('idx_sp', sourceassoc.c['source_id'], sourceassoc.c['project_id']), Index('idx_sr', sourceassoc.c['source_id'], sourceassoc.c['resource_id']), Index('idx_sm', sourceassoc.c['source_id'], sourceassoc.c['meter_id']), Index('ix_sourceassoc_source_id', sourceassoc.c['source_id']) +UniqueConstraint(sourceassoc.c['meter_id'], sourceassoc.c['user_id'], + name='uniq_sourceassoc0meter_id0user_id') class Source(Base):