diff --git a/releasenotes/notes/add-metadata-unique-constraints-dbee79c2ffedc365.yaml b/releasenotes/notes/add-metadata-unique-constraints-dbee79c2ffedc365.yaml new file mode 100644 index 0000000..ab60441 --- /dev/null +++ b/releasenotes/notes/add-metadata-unique-constraints-dbee79c2ffedc365.yaml @@ -0,0 +1,3 @@ +--- +upgrade: + - A new migration is added to add unique constraints on the metadata tables. diff --git a/subunit2sql/db/models.py b/subunit2sql/db/models.py index 476e4e5..1de38af 100644 --- a/subunit2sql/db/models.py +++ b/subunit2sql/db/models.py @@ -110,7 +110,9 @@ class TestRun(BASE, SubunitBase): class RunMetadata(BASE, SubunitBase): __tablename__ = 'run_metadata' __table_args__ = (sa.Index('ix_run_key_value', 'key', 'value'), - sa.Index('ix_run_id', 'run_id')) + sa.Index('ix_run_id', 'run_id'), + sa.UniqueConstraint('run_id', 'key', 'value', + name='uq_run_metadata')) id = sa.Column(sa.BigInteger, primary_key=True) key = sa.Column(sa.String(255)) @@ -123,7 +125,9 @@ class RunMetadata(BASE, SubunitBase): class TestRunMetadata(BASE, SubunitBase): __tablename__ = 'test_run_metadata' __table_args__ = (sa.Index('ix_test_run_key_value', 'key', 'value'), - sa.Index('ix_test_run_id', 'test_run_id')) + sa.Index('ix_test_run_id', 'test_run_id'), + sa.UniqueConstraint('test_run_id', 'key', 'value', + name='uq_test_run_metadata')) id = sa.Column(sa.BigInteger, primary_key=True) key = sa.Column(sa.String(255)) @@ -138,7 +142,9 @@ class TestRunMetadata(BASE, SubunitBase): class TestMetadata(BASE, SubunitBase): __tablename__ = 'test_metadata' __table_args__ = (sa.Index('ix_test_key_value', 'key', 'value'), - sa.Index('ix_test_id', 'test_id')) + sa.Index('ix_test_id', 'test_id'), + sa.UniqueConstraint('test_id', 'key', 'value', + name='uq_test_metadata')) id = sa.Column(sa.BigInteger, primary_key=True) key = sa.Column(sa.String(255)) diff --git a/subunit2sql/migrations/versions/10a2b6d4b06e_add_even_more_indexes.py b/subunit2sql/migrations/versions/10a2b6d4b06e_add_even_more_indexes.py new file mode 100644 index 0000000..76c52ef --- /dev/null +++ b/subunit2sql/migrations/versions/10a2b6d4b06e_add_even_more_indexes.py @@ -0,0 +1,44 @@ +# Copyright (c) 2015 Hewlett-Packard Development Company, L.P. +# +# 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. + +"""Add even more indexes + +Revision ID: 10a2b6d4b06e +Revises: 35cd45895e56 +Create Date: 2015-12-01 18:19:11.328298 + +""" + +# revision identifiers, used by Alembic. +revision = '10a2b6d4b06e' +down_revision = '35cd45895e56' + +from alembic import op + + +def upgrade(): + with op.batch_alter_table('run_metadata') as batch_op: + batch_op.create_unique_constraint('uq_run_metadata', + ['run_id', 'key', 'value']) + with op.batch_alter_table('test_metadata') as batch_op: + batch_op.create_unique_constraint('uq_test_metadata', + ['test_id', 'key', 'value']) + with op.batch_alter_table('test_run_metadata') as batch_op: + batch_op.create_unique_constraint('uq_test_run_metadata', + ['test_run_id', 'key', 'value']) + + +def downgrade(): + NotImplementedError()