From a774c9234c4ff2cde49400d7d559b9962ee7bd65 Mon Sep 17 00:00:00 2001 From: John Davis Date: Wed, 29 May 2024 22:18:26 -0400 Subject: [PATCH 1/4] Add ts db migration: deleted column to api_keys tbl --- ...add_non_nullable_column_deleted_to_api_.py | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 lib/tool_shed/webapp/model/migrations/alembic/versions/1b5bf427db25_add_non_nullable_column_deleted_to_api_.py diff --git a/lib/tool_shed/webapp/model/migrations/alembic/versions/1b5bf427db25_add_non_nullable_column_deleted_to_api_.py b/lib/tool_shed/webapp/model/migrations/alembic/versions/1b5bf427db25_add_non_nullable_column_deleted_to_api_.py new file mode 100644 index 000000000000..6eefd98ff4b3 --- /dev/null +++ b/lib/tool_shed/webapp/model/migrations/alembic/versions/1b5bf427db25_add_non_nullable_column_deleted_to_api_.py @@ -0,0 +1,43 @@ +"""add non-nullable column deleted to API keys + +Revision ID: 1b5bf427db25 +Revises: 969bbf7bcc29 +Create Date: 2024-05-29 21:53:53.516506 + +""" +import sqlalchemy as sa +from sqlalchemy import ( + Boolean, + Column, +) + +from galaxy.model.database_object_names import build_index_name +from galaxy.model.migrations.util import ( + add_column, + drop_column, + drop_index, + transaction, +) + +# revision identifiers, used by Alembic. +revision = "1b5bf427db25" +down_revision = "969bbf7bcc29" +branch_labels = None +depends_on = None + +# database object names used in this revision +table_name = "api_keys" +column_name = "deleted" +index_name = build_index_name(table_name, column_name) + + +def upgrade(): + add_column( + table_name, Column(column_name, Boolean(), default=False, index=True, nullable=False, server_default=sa.false()) + ) + + +def downgrade(): + with transaction(): + drop_index(index_name, table_name) + drop_column(table_name, column_name) From 4099fcc15ac3821f202dbde829ec10dc16349bd8 Mon Sep 17 00:00:00 2001 From: mvdbeek Date: Thu, 30 May 2024 11:45:12 +0200 Subject: [PATCH 2/4] Set all old keys to deleted --- ...add_non_nullable_column_deleted_to_api_.py | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/tool_shed/webapp/model/migrations/alembic/versions/1b5bf427db25_add_non_nullable_column_deleted_to_api_.py b/lib/tool_shed/webapp/model/migrations/alembic/versions/1b5bf427db25_add_non_nullable_column_deleted_to_api_.py index 6eefd98ff4b3..4feafbffe2db 100644 --- a/lib/tool_shed/webapp/model/migrations/alembic/versions/1b5bf427db25_add_non_nullable_column_deleted_to_api_.py +++ b/lib/tool_shed/webapp/model/migrations/alembic/versions/1b5bf427db25_add_non_nullable_column_deleted_to_api_.py @@ -6,14 +6,21 @@ """ import sqlalchemy as sa +from alembic import op from sqlalchemy import ( Boolean, Column, + ForeignKey, + Integer, + false, + select, ) +from sqlalchemy import func from galaxy.model.database_object_names import build_index_name from galaxy.model.migrations.util import ( add_column, + alter_column, drop_column, drop_index, transaction, @@ -32,9 +39,23 @@ def upgrade(): - add_column( - table_name, Column(column_name, Boolean(), default=False, index=True, nullable=False, server_default=sa.false()) - ) + with transaction(): + # Add a nullable deleted column + add_column(table_name, Column(column_name, Boolean(), nullable=True, index=True)) + table = sa.sql.table( + table_name, + Column("id", Integer, primary_key=True), + Column("user_id", ForeignKey("galaxy_user.id"), index=True), + Column(column_name, Boolean, index=True, default=False), + ) + # Set everything to deleted + op.execute(table.update().where(table.c.deleted.is_(None)).values(deleted=True)) + # Select the latest api keys + s = select(func.max(table.c.id)).group_by(table.c.user_id) + # Set all of these api keys to not deleted + op.execute(table.update().where(table.c.id.in_(s)).values(deleted=False)) + # Make column not nullable and default to false + alter_column(table_name, column_name, existing_type=Boolean(), nullable=False, server_default=false()) def downgrade(): From 063d9856506694da633cc34f588a9b53dad3b44f Mon Sep 17 00:00:00 2001 From: mvdbeek Date: Thu, 30 May 2024 11:50:43 +0200 Subject: [PATCH 3/4] Also set model to nullable=False --- lib/tool_shed/webapp/model/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tool_shed/webapp/model/__init__.py b/lib/tool_shed/webapp/model/__init__.py index 1a6761156dd6..a62bb52e39f7 100644 --- a/lib/tool_shed/webapp/model/__init__.py +++ b/lib/tool_shed/webapp/model/__init__.py @@ -92,7 +92,7 @@ class APIKeys(Base): 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) + deleted = Column(Boolean, index=True, default=False, nullable=False) class User(Base, Dictifiable): From d0d16ebf49f73f62ad5b242048313a8986161dae Mon Sep 17 00:00:00 2001 From: John Davis Date: Thu, 30 May 2024 09:34:38 -0400 Subject: [PATCH 4/4] Make column non-nullable --- ...7db25_add_non_nullable_column_deleted_to_api_.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/tool_shed/webapp/model/migrations/alembic/versions/1b5bf427db25_add_non_nullable_column_deleted_to_api_.py b/lib/tool_shed/webapp/model/migrations/alembic/versions/1b5bf427db25_add_non_nullable_column_deleted_to_api_.py index 4feafbffe2db..390402f37b53 100644 --- a/lib/tool_shed/webapp/model/migrations/alembic/versions/1b5bf427db25_add_non_nullable_column_deleted_to_api_.py +++ b/lib/tool_shed/webapp/model/migrations/alembic/versions/1b5bf427db25_add_non_nullable_column_deleted_to_api_.py @@ -10,17 +10,16 @@ from sqlalchemy import ( Boolean, Column, + false, ForeignKey, + func, Integer, - false, select, ) -from sqlalchemy import func from galaxy.model.database_object_names import build_index_name from galaxy.model.migrations.util import ( add_column, - alter_column, drop_column, drop_index, transaction, @@ -40,8 +39,8 @@ def upgrade(): with transaction(): - # Add a nullable deleted column - add_column(table_name, Column(column_name, Boolean(), nullable=True, index=True)) + # Add a deleted column + add_column(table_name, Column(column_name, Boolean(), nullable=False, index=True, server_default=false())) table = sa.sql.table( table_name, Column("id", Integer, primary_key=True), @@ -49,13 +48,11 @@ def upgrade(): Column(column_name, Boolean, index=True, default=False), ) # Set everything to deleted - op.execute(table.update().where(table.c.deleted.is_(None)).values(deleted=True)) + op.execute(table.update().values(deleted=True)) # Select the latest api keys s = select(func.max(table.c.id)).group_by(table.c.user_id) # Set all of these api keys to not deleted op.execute(table.update().where(table.c.id.in_(s)).values(deleted=False)) - # Make column not nullable and default to false - alter_column(table_name, column_name, existing_type=Boolean(), nullable=False, server_default=false()) def downgrade():