Clean constraints in hashmap fields table

Remove unused constraint in hashmap fields, the constraint is implicit
thanks to unicity by service_id and name.
Rename an unique constraint to use a better defining name.

Change-Id: I074fd8cf578d1099c99a250eb69f4f8852eb2dc9
This commit is contained in:
Stéphane Albert 2016-05-19 18:30:41 +02:00
parent 83f98ce60e
commit 669a316d81
2 changed files with 63 additions and 5 deletions

View File

@ -0,0 +1,62 @@
"""Clean hashmap fields constraints.
Revision ID: c88a06b1cfce
Revises: f8c799db4aa0
Create Date: 2016-05-19 18:06:43.315066
"""
# revision identifiers, used by Alembic.
revision = 'c88a06b1cfce'
down_revision = 'f8c799db4aa0'
from alembic import op
import sqlalchemy as sa
def upgrade():
with op.batch_alter_table(
'hashmap_fields',
# NOTE(sheeprine): Forced reflection is needed because of SQLAlchemy's
# SQLite backend limitation reflecting ON DELETE clauses.
reflect_args=[
sa.Column(
'service_id',
sa.Integer,
sa.ForeignKey(
'hashmap_services.id',
ondelete='CASCADE',
name='fk_hashmap_fields_service_id_hashmap_services'),
nullable=False)]) as batch_op:
batch_op.drop_constraint(
u'uniq_field',
type_='unique')
batch_op.create_unique_constraint(
'uniq_field_per_service',
['service_id', 'name'])
batch_op.drop_constraint(
'uniq_map_service_field',
type_='unique')
def downgrade():
with op.batch_alter_table(
'hashmap_fields',
reflect_args=[
sa.Column(
'service_id',
sa.Integer,
sa.ForeignKey(
'hashmap_services.id',
ondelete='CASCADE',
name='fk_hashmap_fields_service_id_hashmap_services'),
nullable=False)]) as batch_op:
batch_op.create_unique_constraint(
u'uniq_field',
['field_id', 'name'])
batch_op.create_unique_constraint(
'uniq_map_service_field',
['service_id', 'name'])
batch_op.drop_constraint(
'uniq_field_per_service',
type_='unique')

View File

@ -117,14 +117,10 @@ class HashMapField(Base, HashMapBase):
@declarative.declared_attr
def __table_args__(cls):
args = (
schema.UniqueConstraint(
'field_id',
'name',
name='uniq_field'),
schema.UniqueConstraint(
'service_id',
'name',
name='uniq_map_service_field'),
name='uniq_field_per_service'),
HashMapBase.__table_args__,)
return args