From 6425b3f8d334cd7a46de193372db7de85927cbb9 Mon Sep 17 00:00:00 2001 From: Graham Hayes Date: Thu, 2 Apr 2015 18:19:10 +0100 Subject: [PATCH] Fixed sort key to not sort by an un-indexed field by default * Added indices on domain, record and recordset tables for created_at * Removed redundent extra sort param in _find() Partially-Fixes: #1413472 Change-Id: Ic6c4274b0ce3d6c14ce02666df945aec1f838cc7 --- designate/sqlalchemy/base.py | 2 +- .../versions/055_add_created_indices.py | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 designate/storage/impl_sqlalchemy/migrate_repo/versions/055_add_created_indices.py diff --git a/designate/sqlalchemy/base.py b/designate/sqlalchemy/base.py index 90ea5497..cb127711 100644 --- a/designate/sqlalchemy/base.py +++ b/designate/sqlalchemy/base.py @@ -242,7 +242,7 @@ class SQLAlchemy(object): try: query = utils.paginate_query( query, table, limit, - [sort_key, 'id', 'created_at'], marker=marker, + [sort_key, 'id'], marker=marker, sort_dir=sort_dir) resultproxy = self.session.execute(query) diff --git a/designate/storage/impl_sqlalchemy/migrate_repo/versions/055_add_created_indices.py b/designate/storage/impl_sqlalchemy/migrate_repo/versions/055_add_created_indices.py new file mode 100644 index 00000000..dcdb28b8 --- /dev/null +++ b/designate/storage/impl_sqlalchemy/migrate_repo/versions/055_add_created_indices.py @@ -0,0 +1,48 @@ +# Copyright (c) 2014 Rackspace Inc. +# +# Author: Tim Simmons +# +# 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. +from sqlalchemy import Index, MetaData, Table + +meta = MetaData() + + +def index_exists(index): + table = index[1]._get_table() + cols = sorted([str(x).split('.')[1] for x in index[1:]]) + + for idx in table.indexes: + if sorted(idx.columns.keys()) == cols: + return True + return False + + +def upgrade(migrate_engine): + + meta.bind = migrate_engine + + zones_table = Table('domains', meta, autoload=True) + recordsets_table = Table('recordsets', meta, autoload=True) + records_table = Table('records', meta, autoload=True) + + indices = [ + ['zone_created_at', zones_table.c.created_at], + ['recordset_created_at', recordsets_table.c.created_at], + ['record_created_at', records_table.c.created_at] + ] + + for ind in indices: + if not index_exists(ind): + index = Index(*ind) + index.create(migrate_engine)