Skip to content

Commit

Permalink
Add deleted column to api_keys table in ToolShed
Browse files Browse the repository at this point in the history
Since the toolshed uses a duplicated `api_keys` table we sadly need to duplicate the migration too...
  • Loading branch information
davelopez committed Sep 30, 2022
1 parent e31d575 commit 3f86751
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/tool_shed/webapp/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class APIKeys(Base, _HasTable):
user_id = Column(ForeignKey("galaxy_user.id"), index=True)
key = Column(TrimmedString(32), index=True, unique=True)
user = relationship("User", back_populates="api_keys")
deleted = Column(Boolean, index=True, default=False)


class User(Base, Dictifiable, _HasTable):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
Migration script to add deleted column to API keys
"""

import datetime
import logging

from sqlalchemy import (
Boolean,
Column,
MetaData,
Table,
)

now = datetime.datetime.utcnow
log = logging.getLogger(__name__)
metadata = MetaData()

TABLE_NAME = "api_keys"
COLUMN_NAME = "deleted"


def upgrade(migrate_engine):
metadata.bind = migrate_engine
print(__doc__)
metadata.reflect()

deleted_column = Column(COLUMN_NAME, Boolean)
__add_column(deleted_column, TABLE_NAME, metadata)


def downgrade(migrate_engine):
metadata.bind = migrate_engine
metadata.reflect()

__drop_column(COLUMN_NAME, TABLE_NAME, metadata)


def __add_column(column, table_name, metadata, **kwds):
try:
table = Table(table_name, metadata, autoload=True)
column.create(table, **kwds)
except Exception:
log.exception("Adding column %s failed.", column)


def __drop_column(column_name, table_name, metadata):
try:
table = Table(table_name, metadata, autoload=True)
getattr(table.c, column_name).drop()
except Exception:
log.exception("Dropping column %s failed.", column_name)

0 comments on commit 3f86751

Please sign in to comment.