diff --git a/subunit2sql/migrations/versions/2fb76f1a1393_rename_test_id_column_in_test_metadata_.py b/subunit2sql/migrations/versions/2fb76f1a1393_rename_test_id_column_in_test_metadata_.py new file mode 100644 index 0000000..81e773b --- /dev/null +++ b/subunit2sql/migrations/versions/2fb76f1a1393_rename_test_id_column_in_test_metadata_.py @@ -0,0 +1,58 @@ +# Copyright 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. + +"""Rename test_id column in test_metadata table + +Revision ID: 2fb76f1a1393 +Revises: 1ff737bef438 +Create Date: 2015-09-22 10:12:03.820855 + +""" + +# revision identifiers, used by Alembic. +revision = '2fb76f1a1393' +down_revision = '1ff737bef438' + +from alembic import context +from alembic import op +import sqlalchemy as sa +from sqlalchemy.engine import reflection + + +def upgrade(): + migration_context = context.get_context() + insp = reflection.Inspector(migration_context.bind) + indx_names = [x['name'] for x in insp.get_indexes('test_metadata')] + # Prempt duplicate index creation on sqlite + if migration_context.dialect.name == 'sqlite': + if 'ix_test_key_value' in indx_names: + op.drop_index('ix_test_key_value', 'test_metadata') + # NOTE(mtreinish) on some mysql versions renaming the column with a fk + # constraint errors out so, delete it before the rename and add it back + # after + if migration_context.dialect.name == 'mysql': + op.drop_constraint('test_metadata_ibfk_1', 'test_metadata', + 'foreignkey') + with op.batch_alter_table('test_metadata') as batch_op: + batch_op.alter_column('test_run_id', + existing_type=sa.String(36), + existing_nullable=False, + new_column_name='test_id') + if migration_context.dialect.name == 'mysql': + op.create_foreign_key('test_metadata_ibfk_1', 'test_metadata', + 'tests', ["test_id"], ['id']) + + +def downgrade(): + pass diff --git a/subunit2sql/tests/migrations/test_migrations.py b/subunit2sql/tests/migrations/test_migrations.py index b0f6377..288fd7a 100644 --- a/subunit2sql/tests/migrations/test_migrations.py +++ b/subunit2sql/tests/migrations/test_migrations.py @@ -359,3 +359,25 @@ class TestWalkMigrations(base.TestCase): else: self.assertEqual(start_micro, row.start_time_microsecond) self.assertEqual(row.stop_time_microsecond, stop_micro) + + def _pre_upgrade_2fb76f1a1393(self, engine): + test_metadata = get_table(engine, 'test_metadata') + tests = get_table(engine, 'tests') + test = {'id': 'fake_test_with_metadata.id', + 'test_id': 'fake_project.tests.FakeTestClass.fake_test_meta', + 'run_count': 1, + 'success': 1, + 'failure': 0} + tests.insert().values(test).execute() + data = {'id': 'AUUID', + 'key': 'AKey', + 'value': 42, + 'test_run_id': 'fake_test_with_metadata.id'} + test_metadata.insert().values(data).execute() + return data + + def _check_2fb76f1a1393(self, engine, data): + test_metadata = get_table(engine, 'test_metadata') + res = list(test_metadata.select().execute())[0] + self.assertEqual(res.id, data['id']) + self.assertEqual(res.test_id, data['test_run_id'])