2019-04-17 05:36:30 +00:00
|
|
|
"""Add captain column to Teams
|
|
|
|
|
|
|
|
Revision ID: b5551cd26764
|
|
|
|
Revises: 4e4d5a9ea000
|
|
|
|
Create Date: 2019-04-12 00:29:08.021141
|
|
|
|
|
|
|
|
"""
|
|
|
|
import sqlalchemy as sa
|
2020-02-17 07:17:25 +00:00
|
|
|
from alembic import op
|
|
|
|
from sqlalchemy.sql import column, table
|
|
|
|
|
|
|
|
from CTFd.models import db
|
2019-04-17 05:36:30 +00:00
|
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
2020-02-17 07:17:25 +00:00
|
|
|
revision = "b5551cd26764"
|
|
|
|
down_revision = "4e4d5a9ea000"
|
2019-04-17 05:36:30 +00:00
|
|
|
branch_labels = None
|
|
|
|
depends_on = None
|
|
|
|
|
2020-02-17 07:17:25 +00:00
|
|
|
teams_table = table("teams", column("id", db.Integer), column("captain_id", db.Integer))
|
2019-04-17 05:36:30 +00:00
|
|
|
|
2020-02-17 07:17:25 +00:00
|
|
|
users_table = table("users", column("id", db.Integer), column("team_id", db.Integer))
|
2019-04-17 05:36:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def upgrade():
|
|
|
|
# ### commands auto generated by Alembic - please adjust! ###
|
2020-02-17 07:17:25 +00:00
|
|
|
op.add_column("teams", sa.Column("captain_id", sa.Integer(), nullable=True))
|
2019-07-03 04:04:50 +00:00
|
|
|
|
|
|
|
bind = op.get_bind()
|
|
|
|
url = str(bind.engine.url)
|
2020-02-17 07:17:25 +00:00
|
|
|
if url.startswith("sqlite") is False:
|
|
|
|
op.create_foreign_key(
|
|
|
|
"team_captain_id", "teams", "users", ["captain_id"], ["id"]
|
|
|
|
)
|
2019-04-17 05:36:30 +00:00
|
|
|
|
|
|
|
connection = op.get_bind()
|
|
|
|
for team in connection.execute(teams_table.select()):
|
|
|
|
users = connection.execute(
|
2020-02-17 07:17:25 +00:00
|
|
|
users_table.select()
|
|
|
|
.where(users_table.c.team_id == team.id)
|
|
|
|
.order_by(users_table.c.id)
|
|
|
|
.limit(1)
|
2019-04-17 05:36:30 +00:00
|
|
|
)
|
|
|
|
for user in users:
|
|
|
|
connection.execute(
|
2020-02-17 07:17:25 +00:00
|
|
|
teams_table.update()
|
|
|
|
.where(teams_table.c.id == team.id)
|
|
|
|
.values(captain_id=user.id)
|
2019-04-17 05:36:30 +00:00
|
|
|
)
|
|
|
|
# ### end Alembic commands ###
|
|
|
|
|
|
|
|
|
|
|
|
def downgrade():
|
|
|
|
# ### commands auto generated by Alembic - please adjust! ###
|
2020-02-17 07:17:25 +00:00
|
|
|
op.drop_constraint("team_captain_id", "teams", type_="foreignkey")
|
|
|
|
op.drop_column("teams", "captain_id")
|
2019-04-17 05:36:30 +00:00
|
|
|
# ### end Alembic commands ###
|