From fec54bea05f227afee91798e0c51c96b06b3f4ee Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Wed, 14 Oct 2015 12:14:39 -0400 Subject: [PATCH] Add migration to cleanup and add additional indexes This commit adds a migration which adds an additional composite index on id, test_id in the tests table. This is needed to help with the performance of queries that are being generated by the openstack-health dashboard's rest api. As part of this migration though some additional cruft in the test_run indexes is cleaned up. We accidently were ending up with duplicate indexes on the id columns in the test_runs table. This removes the duplicate and fixes the check to ensure new DBs don't end up with duplicate indexes. Change-Id: I9d4d42aaa6232db0b2ddfaf8d1a5f079cbeb6b31 --- .../b96122f780_cleanup_and_improve_indexes.py | 50 +++++++++++++++++++ .../tests/migrations/test_migrations.py | 17 +++++++ 2 files changed, 67 insertions(+) create mode 100644 subunit2sql/migrations/versions/b96122f780_cleanup_and_improve_indexes.py diff --git a/subunit2sql/migrations/versions/b96122f780_cleanup_and_improve_indexes.py b/subunit2sql/migrations/versions/b96122f780_cleanup_and_improve_indexes.py new file mode 100644 index 0000000..a25161c --- /dev/null +++ b/subunit2sql/migrations/versions/b96122f780_cleanup_and_improve_indexes.py @@ -0,0 +1,50 @@ +# 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. + +"""Cleanup and Improve Indexes + +Revision ID: b96122f780 +Revises: 2fb76f1a1393 +Create Date: 2015-10-14 12:03:26.965724 + +""" + +# revision identifiers, used by Alembic. +revision = 'b96122f780' +down_revision = '2fb76f1a1393' + +from alembic import context +from alembic import op +from sqlalchemy.engine import reflection + + +def upgrade(): + migration_context = context.get_context() + insp = reflection.Inspector(migration_context.bind) + test_run_indx = insp.get_indexes('test_runs') + test_run_indx_names = [x['name'] for x in test_run_indx] + # Cleanup any duplicate indexes on test_runs + if 'ix_test_runs_test_id' in test_run_indx_names: + if 'ix_test_run_test_id' in test_run_indx_names: + op.drop_index('ix_test_run_test_id', 'test_runs') + if 'ix_test_runs_run_id' in test_run_indx_names: + if 'ix_test_run_run_id' in test_run_indx_names: + op.drop_index('ix_test_run_run_id', 'test_runs') + + # Add an index for test, test_id + op.create_index('ix_test_ids', 'tests', ['id', 'test_id'], mysql_length=30) + + +def downgrade(): + pass diff --git a/subunit2sql/tests/migrations/test_migrations.py b/subunit2sql/tests/migrations/test_migrations.py index 288fd7a..47b29e3 100644 --- a/subunit2sql/tests/migrations/test_migrations.py +++ b/subunit2sql/tests/migrations/test_migrations.py @@ -22,6 +22,7 @@ import os from alembic import config from alembic import script import sqlalchemy +from sqlalchemy.engine import reflection from subunit2sql import exceptions as exc from subunit2sql.tests import base @@ -381,3 +382,19 @@ class TestWalkMigrations(base.TestCase): res = list(test_metadata.select().execute())[0] self.assertEqual(res.id, data['id']) self.assertEqual(res.test_id, data['test_run_id']) + + def _pre_upgrade_b96122f780(self, engine): + # NOTE(mtreinish) Return fake data to ensure we run check, this + # is needed because the framework normall assumes you're preseeded + # data is in the correct state post migration. But this time all we + # want to do is ensure a single index exists so that isn't needed + return 'data' + + def _check_b96122f780(self, engine, data): + insp = reflection.Inspector(engine) + indxs = insp.get_indexes('test_runs') + # Check that we don't duplicate indexes anymore + tests = [indx for indx in indxs if ['test_id'] == indx['column_names']] + runs = [indx for indx in indxs if indx['column_names'] == ['run_id']] + self.assertEqual(1, len(tests)) + self.assertEqual(1, len(runs))