changeset 7964:d2a97f73fa1f

db: migration step after 95c01895c006 failed to add usk_public_key_idx in alembic step b74907136bc1 Use meta reflection to check if the indices exists before running the upgrade step. The upgrade step only has to be run if it is an old database - not if it has been created after the schema changes were introduced.
author Mads Kiilerich <mads@kiilerich.com>
date Sun, 24 Nov 2019 02:52:51 +0100
parents 46681ae86693
children 5647dd555ac3
files kallithea/alembic/versions/4851d15bc437_db_migration_step_after_95c01895c006_.py
diffstat 1 files changed, 44 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kallithea/alembic/versions/4851d15bc437_db_migration_step_after_95c01895c006_.py	Sun Nov 24 02:52:51 2019 +0100
@@ -0,0 +1,44 @@
+# 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: migration step after 95c01895c006 failed to add usk_public_key_idx in alembic step b74907136bc1
+
+Revision ID: 4851d15bc437
+Revises: 151b4a4e8c48
+Create Date: 2019-11-24 02:51:14.029583
+
+"""
+
+# The following opaque hexadecimal identifiers ("revisions") are used
+# by Alembic to track this migration script and its relations to others.
+revision = '4851d15bc437'
+down_revision = '151b4a4e8c48'
+branch_labels = None
+depends_on = None
+
+import sqlalchemy as sa
+from alembic import op
+
+
+def upgrade():
+    meta = sa.MetaData()
+    meta.reflect(bind=op.get_bind())
+
+    if not any(i.name == 'usk_public_key_idx' for i in meta.tables['user_ssh_keys'].indexes):
+        with op.batch_alter_table('user_ssh_keys', schema=None) as batch_op:
+            batch_op.create_index('usk_public_key_idx', ['public_key'], unique=False)
+
+
+def downgrade():
+    with op.batch_alter_table('user_ssh_keys', schema=None) as batch_op:
+        batch_op.drop_index('usk_public_key_idx')