|
| 1 | +"""Update scoped API key uploader id |
| 2 | +
|
| 3 | +Revision ID: 3ba25f23fb7d |
| 4 | +Revises: d212023a8e0b |
| 5 | +Create Date: 2023-08-02 08:03:09.961559 |
| 6 | +
|
| 7 | +""" |
| 8 | +import sqlalchemy as sa |
| 9 | +from alembic import op |
| 10 | + |
| 11 | +# revision identifiers, used by Alembic. |
| 12 | +revision = '3ba25f23fb7d' |
| 13 | +down_revision = 'd212023a8e0b' |
| 14 | +branch_labels = None |
| 15 | +depends_on = None |
| 16 | + |
| 17 | + |
| 18 | +def upgrade(): |
| 19 | + package_versions = sa.sql.table("package_versions", sa.sql.column("uploader_id")) |
| 20 | + conn = op.get_bind() |
| 21 | + # Get all user_id/owner_id from channel scoped API keys |
| 22 | + # (user is anonymous - username is null) |
| 23 | + res = conn.execute( |
| 24 | + sa.text( |
| 25 | + """SELECT api_keys.user_id, api_keys.owner_id FROM api_keys |
| 26 | + INNER JOIN users ON users.id = api_keys.user_id |
| 27 | + WHERE users.username is NULL; |
| 28 | + """ |
| 29 | + ) |
| 30 | + ) |
| 31 | + results = res.fetchall() |
| 32 | + # Replace the uploader with the key owner (real user instead of the anonymous one) |
| 33 | + for result in results: |
| 34 | + op.execute( |
| 35 | + package_versions.update() |
| 36 | + .where(package_versions.c.uploader_id == result[0]) |
| 37 | + .values(uploader_id=result[1]) |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def downgrade(): |
| 42 | + package_versions = sa.sql.table("package_versions", sa.sql.column("uploader_id")) |
| 43 | + conn = op.get_bind() |
| 44 | + # Get all user_id/owner_id from channel scoped API keys |
| 45 | + # (user is anonymous - username is null) |
| 46 | + res = conn.execute( |
| 47 | + sa.text( |
| 48 | + """SELECT api_keys.user_id, api_keys.owner_id FROM api_keys |
| 49 | + INNER JOIN users ON users.id = api_keys.user_id |
| 50 | + WHERE users.username is NULL; |
| 51 | + """ |
| 52 | + ) |
| 53 | + ) |
| 54 | + results = res.fetchall() |
| 55 | + # Replace the uploader with the key anonymous user |
| 56 | + for result in results: |
| 57 | + op.execute( |
| 58 | + package_versions.update() |
| 59 | + .where(package_versions.c.uploader_id == result[1]) |
| 60 | + .values(uploader_id=result[0]) |
| 61 | + ) |
0 commit comments