|
3 | 3 | Revision ID: 2abb9260c6fd |
4 | 4 | Revises: b2122b621d0a |
5 | 5 | Create Date: 2025-04-10 21:03:31.820890 |
6 | | -
|
7 | 6 | """ |
| 7 | + |
8 | 8 | from typing import Sequence, Union |
9 | 9 |
|
10 | | -from alembic import op |
11 | 10 | import sqlalchemy as sa |
| 11 | +from alembic import op |
12 | 12 |
|
13 | 13 | # revision identifiers, used by Alembic. |
14 | 14 | revision: str = "2abb9260c6fd" |
|
18 | 18 |
|
19 | 19 |
|
20 | 20 | def upgrade() -> None: |
21 | | - # ### commands auto generated by Alembic - please adjust! ### |
| 21 | + # Existing migration: add the new column |
22 | 22 | op.add_column("claims", sa.Column("batch_user_id", sa.Text(), nullable=True)) |
23 | | - # ### end Alembic commands ### |
24 | 23 |
|
| 24 | + # Existing migration: create the new table |
25 | 25 | op.execute( |
26 | 26 | """ |
27 | 27 | CREATE TABLE social_media_clients ( |
28 | | - auth0_id VARCHAR PRIMARY KEY REFERENCES users(auth0_id), |
29 | | - platform TEXT NOT NULL |
| 28 | + auth0_id VARCHAR PRIMARY KEY REFERENCES users(auth0_id), |
| 29 | + platform TEXT NOT NULL |
30 | 30 | ); |
31 | | - """ |
| 31 | + """ |
32 | 32 | ) |
33 | 33 |
|
| 34 | + # The change makes the migration safe by preventing inserts into social_media_clients unless the referenced user already exists in users. |
| 35 | + |
| 36 | + # New: only insert BlueSky client if the matching user already exists |
34 | 37 | op.execute( |
35 | 38 | """ |
36 | 39 | INSERT INTO social_media_clients (auth0_id, platform) |
37 | | - VALUES ('I1eyLfAX26wlOMiY4n5SxWOsWrSNXLWU@clients', 'BlueSky'); |
38 | | - """ |
| 40 | + SELECT 'I1eyLfAX26wlOMiY4n5SxWOsWrSNXLWU@clients', 'BlueSky' |
| 41 | + WHERE EXISTS ( |
| 42 | + SELECT 1 FROM users |
| 43 | + WHERE auth0_id = 'I1eyLfAX26wlOMiY4n5SxWOsWrSNXLWU@clients' |
| 44 | + ); |
| 45 | + """ |
39 | 46 | ) |
40 | 47 |
|
| 48 | + # New: only insert X client if the matching user already exists |
41 | 49 | op.execute( |
42 | 50 | """ |
43 | 51 | INSERT INTO social_media_clients (auth0_id, platform) |
44 | | - VALUES ('K46Fnu6E21BG0x3KfNknffbKdTbOHlzw@clients', 'X'); |
45 | | - """ |
| 52 | + SELECT 'K46Fnu6E21BG0x3KfNknffbKdTbOHlzw@clients', 'X' |
| 53 | + WHERE EXISTS ( |
| 54 | + SELECT 1 FROM users |
| 55 | + WHERE auth0_id = 'K46Fnu6E21BG0x3KfNknffbKdTbOHlzw@clients' |
| 56 | + ); |
| 57 | + """ |
46 | 58 | ) |
47 | 59 |
|
| 60 | + # New: only insert Reddit client if the matching user already exists |
48 | 61 | op.execute( |
49 | 62 | """ |
50 | 63 | INSERT INTO social_media_clients (auth0_id, platform) |
51 | | - VALUES ('GbaexhSrWJnbX19M4HYuGH87ROyzwJne@clients', 'Reddit'); |
52 | | - """ |
| 64 | + SELECT 'GbaexhSrWJnbX19M4HYuGH87ROyzwJne@clients', 'Reddit' |
| 65 | + WHERE EXISTS ( |
| 66 | + SELECT 1 FROM users |
| 67 | + WHERE auth0_id = 'GbaexhSrWJnbX19M4HYuGH87ROyzwJne@clients' |
| 68 | + ); |
| 69 | + """ |
53 | 70 | ) |
54 | 71 |
|
55 | 72 |
|
56 | 73 | def downgrade() -> None: |
57 | | - # ### commands auto generated by Alembic - please adjust! ### |
58 | | - op.drop_column("claims", "batch_user_id") |
59 | | - # ### end Alembic commands ### |
60 | | - |
| 74 | + # Existing migration: drop the social_media_clients table |
61 | 75 | op.execute( |
62 | 76 | """ |
63 | 77 | DROP TABLE social_media_clients; |
64 | | - """ |
| 78 | + """ |
65 | 79 | ) |
| 80 | + |
| 81 | + # Existing migration: remove the batch_user_id column |
| 82 | + op.drop_column("claims", "batch_user_id") |
0 commit comments