changeset 8291:802fdeefc8cc

hg: always show and run Mercurial hooks in alphabetical order (Issue #246) Mercurial will generally run hooks in the order they are found in the configuration. For entries found in the database, there is no such order. Instead, always use alphabetical order for these. Since we now want to order things explicitly in the db query, we want an index with a composite key. We do that even though we don't really need it for the few entries in this table, and even though it might/could use the same index as the existing unique constraint. This composite UniqueConstraint was added in b9f4b444a172 where it replaced a wrong UniqueConstraint that could/should have been removed in c25191aadf92. Fix that while touching this area and running a migration script.
author Mads Kiilerich <mads@kiilerich.com>
date Thu, 12 Mar 2020 22:52:22 +0100
parents 9ddb4bd52391
children f83326e2e66c
files kallithea/alembic/versions/a0a1bf09c143_db_add_ui_composite_index_and_drop_.py kallithea/lib/utils.py kallithea/model/db.py
diffstat 3 files changed, 45 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kallithea/alembic/versions/a0a1bf09c143_db_add_ui_composite_index_and_drop_.py	Thu Mar 12 22:52:22 2020 +0100
@@ -0,0 +1,41 @@
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+"""db: add Ui composite index and drop UniqueConstraint on Ui.ui_key
+
+Revision ID: a0a1bf09c143
+Revises: d7ec25b66e47
+Create Date: 2020-03-12 22:41:14.421837
+
+"""
+
+# The following opaque hexadecimal identifiers ("revisions") are used
+# by Alembic to track this migration script and its relations to others.
+revision = 'a0a1bf09c143'
+down_revision = 'd7ec25b66e47'
+branch_labels = None
+depends_on = None
+
+from alembic import op
+
+
+def upgrade():
+    with op.batch_alter_table('ui', schema=None) as batch_op:
+        batch_op.create_index('ui_ui_section_ui_key_idx', ['ui_section', 'ui_key'], unique=False)
+        batch_op.drop_constraint('uq_ui_ui_key', type_='unique')
+
+
+def downgrade():
+    with op.batch_alter_table('ui', schema=None) as batch_op:
+        batch_op.create_unique_constraint('uq_ui_ui_key', ['ui_key'])
+        batch_op.drop_index('ui_ui_section_ui_key_idx')
--- a/kallithea/lib/utils.py	Fri Mar 06 18:02:12 2020 +0100
+++ b/kallithea/lib/utils.py	Thu Mar 12 22:52:22 2020 +0100
@@ -341,7 +341,7 @@
     baseui._tcfg = mercurial.config.config()
 
     sa = meta.Session()
-    for ui_ in sa.query(Ui).all():
+    for ui_ in sa.query(Ui).order_by(Ui.ui_section, Ui.ui_key):
         if ui_.ui_active:
             log.debug('config from db: [%s] %s=%r', ui_.ui_section,
                       ui_.ui_key, ui_.ui_value)
--- a/kallithea/model/db.py	Fri Mar 06 18:02:12 2020 +0100
+++ b/kallithea/model/db.py	Thu Mar 12 22:52:22 2020 +0100
@@ -337,9 +337,7 @@
 class Ui(Base, BaseDbModel):
     __tablename__ = 'ui'
     __table_args__ = (
-        # FIXME: ui_key as key is wrong and should be removed when the corresponding
-        # Ui.get_by_key has been replaced by the composite key
-        UniqueConstraint('ui_key'),
+        Index('ui_ui_section_ui_key_idx', 'ui_section', 'ui_key'),
         UniqueConstraint('ui_section', 'ui_key'),
         _table_args_default_dict,
     )
@@ -372,6 +370,7 @@
         q = cls.query()
         q = q.filter(cls.ui_key.in_([cls.HOOK_UPDATE, cls.HOOK_REPO_SIZE]))
         q = q.filter(cls.ui_section == 'hooks')
+        q = q.order_by(cls.ui_section, cls.ui_key)
         return q.all()
 
     @classmethod
@@ -379,6 +378,7 @@
         q = cls.query()
         q = q.filter(~cls.ui_key.in_([cls.HOOK_UPDATE, cls.HOOK_REPO_SIZE]))
         q = q.filter(cls.ui_section == 'hooks')
+        q = q.order_by(cls.ui_section, cls.ui_key)
         return q.all()
 
     @classmethod