Skip to content

Commit

Permalink
Set all old keys to deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdbeek committed May 30, 2024
1 parent a774c92 commit 4099fcc
Showing 1 changed file with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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():
Expand Down

0 comments on commit 4099fcc

Please sign in to comment.