|
| 1 | +"""Add PlayerAccount and PlayerSoldier tables |
| 2 | +
|
| 3 | +Revision ID: 0ac19ea4739e |
| 4 | +Revises: 78098bd1bbb0 |
| 5 | +Create Date: 2025-10-28 15:08:24.036663 |
| 6 | +
|
| 7 | +""" |
| 8 | +from alembic import op |
| 9 | +import sqlalchemy as sa |
| 10 | +from sqlalchemy import text |
| 11 | + |
| 12 | + |
| 13 | +# revision identifiers, used by Alembic. |
| 14 | +revision = '0ac19ea4739e' |
| 15 | +down_revision = '78098bd1bbb0' |
| 16 | +branch_labels = None |
| 17 | +depends_on = None |
| 18 | + |
| 19 | + |
| 20 | +def upgrade(): |
| 21 | + # Create the player_account table |
| 22 | + op.create_table( |
| 23 | + 'player_account', |
| 24 | + sa.Column('id', sa.Integer(), nullable=False), |
| 25 | + sa.Column('playersteamid_id', sa.Integer(), nullable=False), |
| 26 | + sa.Column('name', sa.String(), nullable=True), |
| 27 | + sa.Column('discord_id', sa.String(), nullable=True), |
| 28 | + sa.Column('is_member', sa.Boolean(), nullable=False, default=sa.false()), |
| 29 | + sa.Column('country', sa.String(2), nullable=True), |
| 30 | + sa.Column('lang', sa.String(2), nullable=False, default='en'), |
| 31 | + sa.Column('updated', sa.TIMESTAMP(timezone=True), nullable=False, default=sa.func.now()), |
| 32 | + sa.ForeignKeyConstraint(['playersteamid_id'], ['steam_id_64.id'], ), |
| 33 | + sa.PrimaryKeyConstraint('id'), |
| 34 | + sa.UniqueConstraint('playersteamid_id', name='unique_player_account') |
| 35 | + ) |
| 36 | + |
| 37 | + # Create the player_soldier table |
| 38 | + op.create_table( |
| 39 | + 'player_soldier', |
| 40 | + sa.Column('id', sa.Integer(), nullable=False), |
| 41 | + sa.Column('playersteamid_id', sa.Integer(), nullable=False), |
| 42 | + sa.Column('name', sa.String(), nullable=True), |
| 43 | + sa.Column('level', sa.Integer(), nullable=False, default=0), |
| 44 | + sa.Column('platform', sa.String(), nullable=True), |
| 45 | + sa.Column('clan_tag', sa.String(), nullable=True), |
| 46 | + sa.Column('updated', sa.TIMESTAMP(timezone=True), nullable=False, default=sa.func.now()), |
| 47 | + sa.ForeignKeyConstraint(['playersteamid_id'], ['steam_id_64.id'], ), |
| 48 | + sa.PrimaryKeyConstraint('id'), |
| 49 | + sa.UniqueConstraint('playersteamid_id', name='unique_player_soldier') |
| 50 | + ) |
| 51 | + |
| 52 | + # Populate the player_account & player_soldier tables with data from existing tables |
| 53 | + connection = op.get_bind() |
| 54 | + |
| 55 | + connection.execute(text(""" |
| 56 | + INSERT INTO player_account (playersteamid_id, country, is_member, lang, updated) |
| 57 | + SELECT |
| 58 | + p.id as playersteamid_id, |
| 59 | + CASE |
| 60 | + WHEN si.country = 'private' THEN NULL |
| 61 | + ELSE si.country |
| 62 | + END as country, |
| 63 | + false as is_member, |
| 64 | + 'en' as lang, |
| 65 | + NOW() as updated |
| 66 | + FROM steam_id_64 p |
| 67 | + LEFT JOIN steam_info si ON si.playersteamid_id = p.id |
| 68 | + """)) |
| 69 | + |
| 70 | + connection.execute(text(""" |
| 71 | + INSERT INTO player_soldier (playersteamid_id, name, level, platform, updated) |
| 72 | + SELECT |
| 73 | + p.id as playersteamid_id, |
| 74 | + pn.name as name, |
| 75 | + COALESCE(ps_max.level, 0) as level, |
| 76 | + CASE |
| 77 | + WHEN LENGTH(p.steam_id_64) = 17 AND p.steam_id_64 ~ '^[0-9]+$' THEN 'steam' |
| 78 | + ELSE NULL |
| 79 | + END as platform, |
| 80 | + NOW() as updated |
| 81 | + FROM steam_id_64 p |
| 82 | + LEFT JOIN LATERAL ( |
| 83 | + SELECT name |
| 84 | + FROM player_names pn |
| 85 | + WHERE pn.playersteamid_id = p.id |
| 86 | + ORDER BY pn.last_seen DESC |
| 87 | + LIMIT 1 |
| 88 | + ) pn ON true |
| 89 | + LEFT JOIN LATERAL ( |
| 90 | + SELECT MAX(level) as level |
| 91 | + FROM player_stats ps |
| 92 | + WHERE ps.playersteamid_id = p.id |
| 93 | + AND ps.level > 0 |
| 94 | + ) ps_max ON true |
| 95 | + """)) |
| 96 | + |
| 97 | + |
| 98 | +def downgrade(): |
| 99 | + # Drop the player_profile table |
| 100 | + op.drop_table('player_account') |
| 101 | + op.drop_table('player_soldier') |
0 commit comments